I am trying to read a registry value using WMI. Here is what I have now:
BSTR methodName = SysAllocString(L"GetStringValue");
BSTR className = SysAllocString(L"StdRegProv");
IWbemClassObject* pInParamsDefinition = nullptr;
std::wstring errStr = L"ERROR";
HRESULT hRes = p_regWbemClassObj->GetMethod(methodName, 0, &pInParamsDefinition, NULL);
IWbemClassObject* pClassInstance = NULL;
hRes = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
// Create the values for the in parameters
VARIANT sSubKeyName;
sSubKeyName.vt = VT_BSTR;
sSubKeyName.bstrVal = BSTR(path.c_str());
VARIANT sValueName;
sValueName.vt = VT_BSTR;
sValueName.bstrVal = BSTR(key.c_str());
// Store the value for the in parameters
hRes = pClassInstance->Put(L"sSubKeyName", 0,
&sSubKeyName, 0);
hRes = pClassInstance->Put(L"sValueName", 0,
&sSubKeyName, 0);
// Execute Method
IWbemClassObject* pOutParams = NULL;
hRes = p_defWbemServices->ExecMethod(className, methodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hRes))
{
VariantClear(&sSubKeyName);
VariantClear(&sValueName);
SysFreeString(className);
SysFreeString(methodName);
pInParamsDefinition->Release();
pClassInstance->Release();
pOutParams->Release();
return errStr;
}
VARIANT varReturnValue;
hRes = pOutParams->Get(BSTR(L"sValue"), 0, &varReturnValue, NULL, NULL);
std::wstring result = varReturnValue.bstrVal;
For some reason I never get a correct varReturnValue. Here is the function: http://msdn.microsoft.com/en-us/library/aa390788(v=vs.85).aspx