Search code examples
c#wmi

WMI methods using ManagementObject.InvokeMethod()


I am having issues when tyring to invoke the SoftwareLicensingProduct.GetTokenActivationGrants WMI method using a .NET ManagementObject object. GetTokenActivationGrants has the following signature:

uint32 GetTokenActivationGrants(
  [out]  string Grants[]
);

I'm using the following C# code:

class Program
{
    private const string GRANTS_METHOD = "GetTokenActivationGrants";

    static void Main(string[] args)
    {

        ManagementObjectSearcher productSearch = new ManagementObjectSearcher("SELECT ID, Name, ApplicationId, PartialProductKey, Description, LicenseIsAddon " +
            "FROM SoftwareLicensingProduct WHERE ApplicationId = '55c92734-d682-4d71-983e-d6ec3f16059f' AND PartialProductKey <> NULL " +
            "AND LicenseIsAddon = FALSE");

        foreach (ManagementObject product in productSearch.Get())
        {
            ManagementBaseObject inParams = product.GetMethodParameters(GRANTS_METHOD);
            ManagementBaseObject outParams = product.InvokeMethod(GRANTS_METHOD, inParams, null);
        }

    }
}

This fails with a System.Runtime.InteropServices.COMException which I believe is occurring because the GetMethodParameters returns null. Does anyone know how to properly invoke a method like this that has a referenced argument?


Solution

  • As @Hans commented, this is an expected error. The problem is that none of the expected or possible errors are documented anywhere in the MSDN library. With a little digging you can find them in %SystemRoot%\System32\slmgr\0409\slmgr.ini or http://gallery.technet.microsoft.com/office/68b80aba-130d-4ad4-aa45-832b1ee49602.