Thanks Richard as always I will try your solution too, but I have found the solution and i wrote this complete function It does this operation, accept in input The Class, The Method and a string formatted.
During execution check if method needs parameters, if yes, check in the parameters string if found corrent association insert the value in that parameters, and then run the command.
This function with all methods present in SoftwareLicensingService I made it to be general purpose.
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
HRESULT RunWMIMethod(LPCWSTR pszClass, LPCWSTR pszMethod, LPCWSTR pszParameters)
{
HRESULT hr = S_OK;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr) && hr != S_FALSE)
return hr;
hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hr) && hr != RPC_E_TOO_LATE)
{
CoUninitialize();
return hr;
}
IWbemLocator* pLocator = NULL;
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLocator);
if (SUCCEEDED(hr))
{
IWbemServices* pService = NULL;
hr = pLocator->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pService);
if (SUCCEEDED(hr))
{
hr = CoSetProxyBlanket(pService, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (SUCCEEDED(hr))
{
IEnumWbemClassObject* pEnumClassInstances;
hr = pService->CreateInstanceEnum(_bstr_t(pszClass), WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumClassInstances);
if (SUCCEEDED(hr))
{
IWbemClassObject* pEnumClassInstance;
ULONG uInstancesCount = 0;
hr = pEnumClassInstances->Next(10000, 1, &pEnumClassInstance, &uInstancesCount);
if (SUCCEEDED(hr))
{
VARIANT ClassPath;
VariantInit(&ClassPath);
ClassPath.vt = VT_BSTR;
hr = pEnumClassInstance->Get(_bstr_t("__Path"), 0, &ClassPath, 0, 0);
if (SUCCEEDED(hr))
{
IWbemClassObject* pClassObJect = NULL;
hr = pService->GetObject(_bstr_t(pszClass), 0, NULL, &pClassObJect, NULL);
if (SUCCEEDED(hr))
{
IWbemClassObject* pInParamsDefinition = NULL;
IWbemClassObject* pObjectParamsClassInstance = NULL;
hr = pClassObJect->GetMethod(_bstr_t(pszMethod), 0, &pInParamsDefinition, NULL);
if (SUCCEEDED(hr) && pInParamsDefinition != NULL && pszParameters != NULL)
{
hr = pInParamsDefinition->SpawnInstance(0, &pObjectParamsClassInstance);
if (SUCCEEDED(hr))
{
long lLower, lUpper, lCount;
SAFEARRAY* psaNames = NULL;
CString strPropertyName, strValueName;
CString strParameters(pszParameters);
int nPos = 0;
hr = pObjectParamsClassInstance->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, NULL, &psaNames);
if (SUCCEEDED(hr))
{
if (SUCCEEDED(SafeArrayGetLBound(psaNames, 1, &lLower)) && SUCCEEDED(SafeArrayGetUBound(psaNames, 1, &lUpper)))
{
for (lCount = lLower; lCount <= lUpper; lCount++)
{
BSTR PropName = NULL;
hr = SafeArrayGetElement(psaNames, &lCount, &PropName);
strPropertyName = PropName;
nPos = strParameters.Find(strPropertyName);
if (nPos != -1)
{
nPos += strPropertyName.GetLength();
strValueName = strParameters.Tokenize(_T("= ;"), nPos);
VARIANT CommandLine;
VariantInit(&CommandLine);
CommandLine.vt = VT_BSTR;
CommandLine.bstrVal = _bstr_t(strValueName);
hr = pObjectParamsClassInstance->Put(strPropertyName, 0, &CommandLine, 0);
VariantClear(&CommandLine);
}
}
}
SafeArrayDestroy(psaNames);
}
}
pInParamsDefinition->Release();
}
IWbemClassObject* pResultsObject = NULL;
hr = pService->ExecMethod(ClassPath.bstrVal, _bstr_t(pszMethod), 0, NULL, pObjectParamsClassInstance, &pResultsObject, NULL);
VariantClear(&ClassPath);
if (SUCCEEDED(hr))
{
VARIANT ReturnValue;
VariantInit(&ReturnValue);
hr = pResultsObject->Get(_bstr_t(L"ReturnValue"), 0, &ReturnValue, NULL, 0);
VariantClear(&ReturnValue);
}
if (pResultsObject)
pResultsObject->Release();
if (pObjectParamsClassInstance)
pObjectParamsClassInstance->Release();
pClassObJect->Release();
}
}
pEnumClassInstance->Release();
}
pEnumClassInstances->Release();
}
}
pService->Release();
}
pLocator->Release();
}
CoUninitialize();
return hr;
}
int main()
{
CString strParameters(_T("ProductKey=AAAAA-BBBBB-CCCC-DDDD-EEEEE;"));
RunWMIMethod(L"SoftwareLicensingService", L"InstallProductKey", strParameters);
return 0;
}