File: Utilities\Wmi\IWbemLocator.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/).
 
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 obtain the initial namespace pointer to the IWbemServices interface for WMI
///  on a particular host computer. Wraps the native IWbemLocator COM interface.
/// </summary>
/// <remarks>
///  <see href="https://learn.microsoft.com/windows/win32/api/wbemcli/nn-wbemcli-iwbemlocator"/>
/// </remarks>
[SupportedOSPlatform("windows6.0")]
internal unsafe struct IWbemLocator : IComIID
{
    public static Guid Guid { get; } = new(0xDC12A687, 0x737F, 0x11CF, 0x88, 0x4D, 0x00, 0xAA, 0x00, 0x4B, 0x2E, 0x24);
 
    static ref readonly Guid IComIID.Guid
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get
        {
            ReadOnlySpan<byte> data =
            [
                0x87, 0xA6, 0x12, 0xDC,
                0x7F, 0x73,
                0xCF, 0x11,
                0x88, 0x4D, 0x00, 0xAA, 0x00, 0x4B, 0x2E, 0x24
            ];
 
            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 (IWbemLocator* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemLocator*, Guid*, void**, HRESULT>)_lpVtbl[0])(pThis, riid, ppvObject);
        }
    }
 
    public uint AddRef()
    {
        fixed (IWbemLocator* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemLocator*, uint>)_lpVtbl[1])(pThis);
        }
    }
 
    public uint Release()
    {
        fixed (IWbemLocator* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemLocator*, uint>)_lpVtbl[2])(pThis);
        }
    }
 
    // IWbemLocator methods (vtable index 3)
 
    /// <summary>
    ///  Connects to the WMI service on the specified host.
    /// </summary>
    /// <remarks>
    ///  <see href="https://learn.microsoft.com/windows/win32/api/wbemcli/nf-wbemcli-iwbemlocator-connectserver"/>
    /// </remarks>
    public HRESULT ConnectServer(
        char* strNetworkResource,
        char* strUser,
        char* strPassword,
        char* strLocale,
        int lSecurityFlags,
        char* strAuthority,
        void* pCtx,
        IWbemServices** ppNamespace)
    {
        fixed (IWbemLocator* pThis = &this)
        {
            return ((delegate* unmanaged[Stdcall]<IWbemLocator*, char*, char*, char*, char*, int, char*, void*, IWbemServices**, HRESULT>)_lpVtbl[3])(
                pThis, strNetworkResource, strUser, strPassword, strLocale, lSecurityFlags, strAuthority, pCtx, ppNamespace);
        }
    }
 
    /// <summary>
    ///  CLSID for WbemLocator COM class.
    /// </summary>
    public static Guid CLSID { get; } = new(0x4590F811, 0x1D3A, 0x11D0, 0x89, 0x1F, 0x00, 0xAA, 0x00, 0x4B, 0x2E, 0x24);
}