C++ WMI Registry GetStringValue

  • Thread starter Thread starter blackalegator
  • Start date Start date
B

blackalegator

Guest
Hello. I am trying to use WMI to get String values from registry.


Method Reference: GetStringValue method of the StdRegProv class

Unfortunately I have 2 problems - first one is that I dont understand on how to set a uint32 (hDefKey) parameter and the second one - I cant recieve the actual registry value (sValue). Here is the code which I have now, assuming that p_defWbemServices is the pointer to correctly initialised root\default namespace

std::wstring CCTests::GetRegistryString(const UINT32 defKey, const std::wstring &path, const std::wstring &key){
BSTR ClassPath = SysAllocString(L"StdRegProv");
BSTR MethodName = SysAllocString(L"GetStringValue");
BSTR ArgName1 = SysAllocString(L"hDefKey");
BSTR ArgName2 = SysAllocString(L"sSubKeyName");
BSTR ArgName3 = SysAllocString(L"sValueName");
BSTR Text;
IWbemClassObject * pClass = NULL;
IWbemClassObject * pOutInst = NULL;
IWbemClassObject * pInClass = NULL;
IWbemClassObject * pInInst = NULL;

std::wstring result = L"ERROR";

// Get the class object for the method definition.
HRESULT hr;
hr = p_defWbemServices->GetObject(ClassPath, 0,
NULL, &pClass, NULL);

// Get the input-argument class object and
// create an instance.

hr = pClass->GetMethod(MethodName, 0,
&pInClass, NULL);
hr = pInClass->SpawnInstance(0, &pInInst);

// Set the property.

VARIANT varArg1;
varArg1.vt = VT_UINT;
varArg1.uintVal = (ULONG_PTR)HKEY_CURRENT_USER;
hr = pInInst->Put(ArgName1, 0, &varArg1, 0);
VariantClear(&varArg1);

VARIANT varArg2;
varArg2.vt = VT_BSTR;
varArg2.bstrVal = SysAllocString(path.c_str());
hr = pInInst->Put(ArgName2, 0, &varArg2, 0);
VariantClear(&varArg2);

VARIANT varArg3;
varArg3.vt = VT_BSTR;
varArg3.bstrVal = SysAllocString(key.c_str());
hr = pInInst->Put(ArgName3, 0, &varArg3, 0);
VariantClear(&varArg3);

// Call the method.

hr = p_defWbemServices->ExecMethod(ClassPath,
MethodName, 0, NULL,
pInInst, &pOutInst, NULL);

// Display the results. Note that the return
// value is in the property "ReturnValue"
// and the returned string is in the
// property "sOutArg".

hr = pInInst->GetObjectText(0, &Text);

if (hr == S_OK){
result = Text;
}

// Free up resources.

SysFreeString(ClassPath);
SysFreeString(MethodName);
SysFreeString(ArgName1);
SysFreeString(ArgName2);
SysFreeString(ArgName3);
SysFreeString(Text);
pClass->Release();
pInInst->Release();
pInClass->Release();
pOutInst->Release();

return result;
}




result contains only ReturnValue in the output. no sValue is returned. Even if I use the default HKEY.

Continue reading...
 
Back
Top