【问题标题】:WMI C++ IWbemServices . ExecMethod for WBEM_E_INVALID_METHOD_PARAMETERSWMI C++ IWbemServices 。 WBEM_E_INVALID_METHOD_PARAMETERS 的执行方法
【发布时间】:2016-09-30 15:52:16
【问题描述】:

我使用了WMI的机制。通过dsdt.dsl的修改和MOF文件的制作,我用C#实现了自定义WMI功能。但是有一个问题,当我想用​​ C++ - MFC 与 MOF 文件进行通信时。 当代码运行到 IWbemServices 时。 ExecMethod函数,它显示错误信息:WBEM_E_INVALID_METHOD_PARAMETERS(0x8004102F)。我认为原因是输入参数:boolean......希望大家可以提供一些建议! 非常感谢!

acpimof.mof:

class WMIEvent : __ExtrinsicEvent
{
};

[WMI,
 Dynamic,
 Provider("WmiProv"),
 Locale("MS\\0x409"),
 Description("Acpi_Commands"),
 guid("{ABBC0F6D-8EA1-11d1-00A0-C90629100000}")
]
class Acpi_Commands
{
    [key, read]
     string InstanceName;
    [read] boolean Active;

    [WmiMethodId(1),
     Implemented,
     read, write,
     Description("setReadLight")] 
     void setReadLight([in, Description("Status")] boolean Status);
};

acpi.cpp:

复制 MSDN – 示例:调用提供程序方法 (https://msdn.microsoft.com/en-us/library/aa390421(v=vs.85).aspx)。第 1、2、3、5 步与示例完全一致,代码不再赘述。我修改了第 4 步和第 6 步。

// Step 4: ---------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method

IWbemServices *pSvc = NULL;

// Connect to the local namespace
// and obtain pointer pSvc to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\WMI"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);

if (FAILED(hres))
{
    cout << "Could not connect. Error code = 0x"
    << hex << hres << endl;
    pLoc->Release();
    CoUninitialize();
    return 1;                // Program has failed.
}


// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----

// set up to call the Win32_Process::Create method
BSTR ClassName = SysAllocString(L"Acpi_Commands");
BSTR MethodName = SysAllocString(L"setReadLight");

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, NULL);

IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

// Create the values for the in parameters
VARIANT varCommand;
varCommand.vt = VT_BOOL;
varCommand.boolVal = VARIANT_TRUE;

// Store the value for the in parameters
hres = pClassInstance->Put(L"Status", 0, &varCommand, CIM_BOOLEAN);

// Execute Method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0, NULL, pClassInstance, &pOutParams, NULL);
//Get error message: WBEM_E_INVALID_METHOD_PARAMETERS(0x8004102f)

if (FAILED(hres))
{
    cout << "Could not execute method. Error code = 0x" << hex << hres << endl;
    // Clean up(don't show here)
    return 1;               // Program has failed.
}

// Clean up(don't show here)

system("pause");
return 0;

【问题讨论】:

    标签: c++ mfc wmi


    【解决方案1】:

    我自己找到了解决方案,分享给有需要的人:)

    问题出现在IWbemServices::ExecMethod的第一个参数需要指定的属性值而不是简单的类名。所以它需要一些设置来获取指定的属性值。(但是“示例:调用提供程序方法”仅设置参数的类名并且它有效......我猜原因发生在名称空间中(原始:“ROOT\CIMV2”=>这是Windows的标准模型),并且我使用“ROOT\WMI”连接WMI,所以它需要指定属性值。如果我错了,请纠正我!)

    请修改上面acpi.cpp的第6步。

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----
    BSTR ClassName = SysAllocString(L"Acpi_Commands");
    BSTR MethodName = SysAllocString(L"setReadLight");
    BSTR bstrQuery = SysAllocString(L"Select * from Acpi_Commands");
    
    //The IEnumWbemClassObject interface is used to enumerate Common Information Model (CIM) objects 
    //and is similar to a standard COM enumerator.
    IEnumWbemClassObject *pEnum = NULL;
       hres = pSvc->ExecQuery(_bstr_t(L"WQL"), //Query Language
       bstrQuery, //Query to Execute  
       WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, //Make a semi-synchronous call  
       NULL, //Context  
       &pEnum /*Enumeration Interface*/);
    
    hres = WBEM_S_NO_ERROR;
    
    ULONG ulReturned;
    IWbemClassObject *pObj;
    DWORD retVal = 0;
    
    //Get the Next Object from the collection  
    hres = pEnum->Next(WBEM_INFINITE, //Timeout  
        1, //One of object requested  
        &pObj, //Returned Object  
        &ulReturned /*No of object returned*/);
    
    IWbemClassObject* pClass = NULL;
    hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
    
    IWbemClassObject* pInParamsDefinition = NULL;
    hres = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, NULL);
    
    IWbemClassObject* pClassInstance = NULL;
    hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
    
    VARIANT var1;
    VariantInit(&var1);
    BSTR ArgName0 = SysAllocString(L"Status");
    
    V_VT(&var1) = VT_BOOL;
    V_BOOL(&var1) = VARIANT_FALSE;
    
    hres = pClassInstance->Put(ArgName0, 0, &var1, CIM_BOOLEAN);  
    printf("\nPut ArgName0 returned 0x%x:", hres);
    VariantClear(&var1);
    
    // Call the method  
    VARIANT pathVariable;
    VariantInit(&pathVariable);
    
    //The IWbemClassObject::Get method retrieves the specified property value, if it exists.
    hres = pObj->Get(_bstr_t(L"__PATH"),
        0,
        &pathVariable,
        NULL,
        NULL);
    printf("\npObj Get returned 0x%x:", hres);
    
    hres = pSvc->ExecMethod(pathVariable.bstrVal,
        MethodName,
        0,
        NULL,
        pClassInstance,
        NULL,
        NULL);
    VariantClear(&pathVariable);
    printf("\nExecMethod returned 0x%x:", hres);
    
    printf("Terminating normally\n");
    
    
    // Clean up
    SysFreeString(ClassName);
    SysFreeString(MethodName);
    pClass->Release();
    pClassInstance->Release();
    pInParamsDefinition->Release();
    pLoc->Release();
    pSvc->Release();
    CoUninitialize();
    
    system("pause");
    return 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多