File: Utilities\Wmi\IWbemServices.cs
Web Access
Project: ..\..\..\src\Framework\Microsoft.Build.Framework.csproj (Microsoft.Build.Framework)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
// Manually defined WMI COM struct following CsWin32 struct-based COM patterns.
// WMI interfaces are not in Win32 metadata, so they cannot be generated by CsWin32.
// Pattern follows dotnet/sdk ISetupConfiguration.cs (src/Cli/dotnet/Microsoft/VisualStudio/Setup/Configuration/).
//
// Only the methods actually used (ExecQuery) are defined with typed signatures.
// Unused vtable slots are kept as placeholders to maintain correct indices.
 
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Windows.Win32;
using Windows.Win32.Foundation;
 
namespace Microsoft.Build.Shared.Win32.Wmi;
 
/// <summary>
///  Used to make calls to WMI. This is the primary WMI interface.
/// </summary>
/// <remarks>
///  <see href="https://learn.microsoft.com/windows/win32/api/wbemcli/nn-wbemcli-iwbemservices"/>
/// </remarks>
[SupportedOSPlatform("windows6.0")]
internal unsafe struct IWbemServices : IComIID
{
    public static Guid Guid { get; } = new(0x9556DC99, 0x828C, 0x11CF, 0xA3, 0x7E, 0x00, 0xAA, 0x00, 0x32, 0x40, 0xC7);
 
    static ref readonly Guid IComIID.Guid
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get
        {
            ReadOnlySpan<byte> data =
            [
                0x99, 0xDC, 0x56, 0x95,
                0x8C, 0x82,
                0xCF, 0x11,
                0xA3, 0x7E, 0x00, 0xAA, 0x00, 0x32, 0x40, 0xC7
            ];
 
            return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
        }
    }
 
    private readonly void** _lpVtbl;
 
    // IUnknown methods (vtable indices 0-2)
 
    public HRESULT QueryInterface(Guid* riid, void** ppvObject)
    {
        fixed (IWbemServices* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemServices*, Guid*, void**, HRESULT>)_lpVtbl[0])(pThis, riid, ppvObject);
        }
    }
 
    public uint AddRef()
    {
        fixed (IWbemServices* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemServices*, uint>)_lpVtbl[1])(pThis);
        }
    }
 
    public uint Release()
    {
        fixed (IWbemServices* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemServices*, uint>)_lpVtbl[2])(pThis);
        }
    }
 
    // IWbemServices vtable layout (indices 3-25):
    //   3  = OpenNamespace
    //   4  = CancelAsyncCall
    //   5  = QueryObjectSink
    //   6  = GetObject
    //   7  = GetObjectAsync
    //   8  = PutClass
    //   9  = PutClassAsync
    //   10 = DeleteClass
    //   11 = DeleteClassAsync
    //   12 = CreateClassEnum
    //   13 = CreateClassEnumAsync
    //   14 = PutInstance
    //   15 = PutInstanceAsync
    //   16 = DeleteInstance
    //   17 = DeleteInstanceAsync
    //   18 = CreateInstanceEnum
    //   19 = CreateInstanceEnumAsync
    //   20 = ExecQuery              <-- Used
    //   21 = ExecQueryAsync
    //   22 = ExecNotificationQuery
    //   23 = ExecNotificationQueryAsync
    //   24 = ExecMethod
    //   25 = ExecMethodAsync
 
    /// <summary>
    ///  Executes a WQL query to retrieve objects.
    /// </summary>
    /// <remarks>
    ///  <see href="https://learn.microsoft.com/windows/win32/api/wbemcli/nf-wbemcli-iwbemservices-execquery"/>
    /// </remarks>
    public HRESULT ExecQuery(
        char* strQueryLanguage,
        char* strQuery,
        int lFlags,
        void* pCtx,
        IEnumWbemClassObject** ppEnum)
    {
        fixed (IWbemServices* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemServices*, char*, char*, int, void*, IEnumWbemClassObject**, HRESULT>)_lpVtbl[20])(
                pThis, strQueryLanguage, strQuery, lFlags, pCtx, ppEnum);
        }
    }
}