我正在尝试使用SoftwareLicensingService :: InstallProductKey通过服务中的WMI / C ++在Windows 7上安装产品密钥。但是,每次我尝试通过IWbemServices :: ExecMethod调用该方法时,都会得到0x8004102f,即WBEM_E_INVALID_METHOD_PARAMETERS。我以为这与我传递的产品密钥有关,但是从那时起,我一直尝试对Win32_WindowsProductActivation :: ActivateOnline [在XP上没有可用的参数方法]使用类似的代码,但有相同的错误。有谁知道下面我的代码片段中有什么可疑的内容(我跳过了一些简短的清理代码)?但是,相同的代码序列可以成功调用其他WMI方法。

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = S_OK;
    IWbemLocator *pLoc = NULL;
    IWbemServices *pServices = NULL;
    IWbemClassObject *pInputParamsClass = NULL;
    IWbemClassObject *pInputParams = NULL;
    IWbemClassObject *pOutputParams = NULL;
    IWbemClassObject *pLicensingClsObj = NULL;
    VARIANT vtProductKey = {0};
    VARIANT vtPath = {0};


    hr =  CoInitializeEx(0, COINIT_MULTITHREADED);
    if(FAILED(hr))
        goto cleanup;

hr =  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
            RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
    goto cleanup;

hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
            IID_IWbemLocator, (LPVOID *)&pLoc);
_ASSERT(SUCCEEDED(hr) && (NULL != pLoc));
if(FAILED(hr) || (NULL == pLoc))
    goto cleanup;

hr = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL,
            0, 0, &pServices);
_ASSERT(SUCCEEDED(hr) && (NULL != pServices));
if(FAILED(hr) || (NULL == pServices))
    goto cleanup;

hr = CoSetProxyBlanket(pServices, RPC_C_AUTHN_WINNT,
   RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE,
   NULL, EOAC_NONE);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
    goto cleanup;

hr = pServices->GetObject(_bstr_t(L"SoftwareLicensingService"),
                        0, NULL, &pLicensingClsObj, NULL);
_ASSERT(SUCCEEDED(hr) && (NULL != pLicensingClsObj));
if(FAILED(hr) || (NULL == pLicensingClsObj))
    goto cleanup;

hr = pLicensingClsObj->Get(L"__Path", 0, &vtPath, 0, 0);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
    goto cleanup;

hr = pLicensingClsObj->GetMethod(L"InstallProductKey", 0,
            &pInputParamsClass, NULL);
_ASSERT(SUCCEEDED(hr) && (NULL != pInputParamsClass));
if(FAILED(hr) || (NULL == pInputParamsClass))
    goto cleanup;

hr = pInputParamsClass->SpawnInstance(0, &pInputParams);
_ASSERT(SUCCEEDED(hr) && (NULL != pInputParams));
if(FAILED(hr) || (NULL == pInputParams))
    goto cleanup;

vtProductKey.vt = VT_BSTR;
vtProductKey.bstrVal = SysAllocString(L"XXXXXXXXXXXXXXXXXXXXXXXXX");
hr = pInputParams->Put(L"ProductKey", 0, &vtProductKey, 0);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
    goto cleanup;

hr = pServices->ExecMethod(vtPath.bstrVal,
            _bstr_t(L"InstallProductKey"),
            0, NULL, pInputParams,
            &pOutputParams, NULL);
_ASSERT(SUCCEEDED(hr) && (NULL != pOutputParams));
if(FAILED(hr) || (NULL == pOutputParams))
    goto cleanup;

hr = S_OK;//all success

cleanup:
if(NULL != pLoc)
{
    pLoc->Release();
    pLoc = NULL;
}
if(NULL != pServices)
{
    pServices->Release();
    pServices = NULL;
}
(VOID)CoUninitialize();
return hr;


}

最佳答案

从那以后我就知道了问题所在。方法调用是在类而不是实例上进行的。

关于c++ - 来自ExecMethod的错误WBEM_E_INVALID_METHOD_PARAMETERS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30221837/

10-13 00:00