Executing a WMI method from C++ fails with WBEM_E_INVALID_METHOD_PARAMETERS

0

I'm trying to call a method from WMI using C++. But the pNamespace->callMethod(..) fails with an error WBEM_E_INVALID_METHOD_PARAMETERS (0x8004102F).

I'm trying to call a method from.. Namespace: ROOT\WMI Class: CLEVO_GET Method: SetKBLED (Static = False) In Params: ID: 0 / Name: Data / Type: uint32 Out Params: None

MOF: [WmiMethodId(103), Implemented, read, write, Description("SetKBLED")] void SetKBLED([in, Description("SetKBLED")] uint32 Data);

This is my code, but as said before, the "callMethod" function never works and returns a 0x8004102F error. Anybody knows what is wrong?

#include <Windows.h>  
#include <iostream>  
#include <string> 
#include <WbemCli.h>  

#pragma comment(lib, "wbemuuid.lib")  

int main()
{
    IWbemLocator *pLocator = NULL;
    IWbemServices *pNamespace = 0;
    IWbemClassObject * pClass = NULL;
    IWbemClassObject * pOutInst = NULL;
    IWbemClassObject * pInClass = NULL;
    IWbemClassObject * pInInst = NULL;

    BSTR path = SysAllocString(L"root\\wmi");
    BSTR ClassPath = SysAllocString(L"CLEVO_GET");
    BSTR MethodName = SysAllocString(L"SetKBLED");
    BSTR ArgName = SysAllocString(L"Data");

    // Initialize COM and connect to WMI.

    HRESULT hr = CoInitialize(0);
    hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
        RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
    hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
        IID_IWbemLocator, (LPVOID *)&pLocator);
    hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace);

    // Get the class object for the method definition.
    hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL);
    std::cout << "NAMESPACE-CLASS:: " << std::hex << hr << std::endl;

    // Get the input-argument class object and 
    // create an instance.
    hr = pClass->GetMethod(MethodName, 0, &pInClass, NULL);
    std::cout << "GET METHOD:: " << std::hex << hr << std::endl;
    hr = pInClass->SpawnInstance(0, &pInInst);
    std::cout << "SPAWN INSTANCE:: " << std::hex << hr << std::endl;

    // Set the property.
    VARIANT var;
    var.vt = VT_I4;
    var.iVal = 4096;
    //var.iVal = std::stoul("0x00001000", NULL, 16);

    hr = pInInst->Put(ArgName, 0, &var, 0);
    std::cout << "ADD VARIABLE:: " << std::hex << hr << std::endl;
    VariantClear(&var);

    // Call the method.
    //hr = pNamespace->ExecMethod(L"CLEVO_GET", L"SetKBLED", 0, NULL, NULL, NULL, NULL);
    hr = pNamespace->ExecMethod(ClassPath, MethodName, 0, NULL, pInInst, NULL, NULL);
    //hr = pNamespace->ExecMethod(ClassPath, MethodName, 0, NULL, NULL, NULL, NULL);
    std::cout << "EXEC RESULT:: " << std::hex << hr << std::endl;

    // Free up resources.
    SysFreeString(path);
    SysFreeString(ClassPath);
    SysFreeString(MethodName);
    SysFreeString(ArgName);
    pClass->Release();
    pInInst->Release();
    pInClass->Release();
    pLocator->Release();
    pNamespace->Release();
    CoUninitialize();


    // Wait for finish
    std::cout << "DONE! :)";
    std::cin.ignore();

    return 0;
}

I'm trying to achieve this using C++ (This code snippet is written in Powershell and it works) :

$clevo = get-wmiobject -query "select * from CLEVO_GET" -namespace "root\WMI"
$clevo.SetKBLED( [Convert]::ToUInt32("00001000", 16)  )

Hope somebody can help me. Any help is highly apretiated. Thanks.

c++
windows
powershell
wmi
wmi-query

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0