1

I'm trying to invoke a certain WMI method using C++: Msvm_VirtualSystemManagementService::GetVirtualSystemThumbnailImage (https://learn.microsoft.com/en-us/previous-versions/windows/desktop/virtual/getvirtualsystemthumbnailimage-msvm-virtualsystemmanagementservice).

The method itself requires three parameters: a reference to an instance of CIM_VirtualSystemSettingData, and two uint16s.

In C++, invoking a WMI method first requires you to obtain the the parameters for a method via IWbemClassObject::GetMethod and SpawnInstance; at this point, you can then start using the IWbemClassObject::Put method to begin filling out the parameters, i.e.:

CComVariant varCommand2;
varCommand2.vt = VT_BSTR;
varCommand2.bstrVal = _bstr_t("32");
hres = pClassInstance->Put(L"WidthPixels", 0, &varCommand2, CIM_UINT16);

This works fine. However, I am unable to find any documentation whatsoever with regard to passing an instance of a CIM class. The documentation on on IWbemClassObject::Put states:

If pVal is to contain an embedded IWbemClassObject, the caller must call IWbemClassObject::QueryInterface for IID_IUnknown and place the resulting pointer in the VARIANT using a type of VT_UNKNOWN.

As such, this is what I am trying to do:

CComVariant varCommand;
varCommand.vt = VT_UNKNOWN;
vVirtualSystemSettingData[0].getClassInstance()->QueryInterface(IID_IUnknown, (void**)&varCommand); //getClassInstance returns a reference to the CComPtr that contains the VirtualSystemSettingData I want to use

hres = pClassInstance->Put(L"TargetSystem", 0, &varCommand, 0);

Unfortunately, I simply get a mismatch error, so I know I am not doing this correctly. Ergo, does anyone know how to pass references of CIM objects to the IWbemClassObject::Put method?

Kind regards!

1
  • Did you ever get this to work? I'm having the very same problem. Thanks! Aug 10, 2021 at 10:40

1 Answer 1

0

If your instance is already stored in the wmi repository you can use his path for creating reference.

For example:

VARIANT v;
VariantInit(&v);
V_VT(&v) = VT_BSTR;
V_BSTR(&v) = SysAllocString(L"{SOME_CLASS}.Name=\"{YOUR_INSTNACE_NAME}\"");

hres = pClassInstance->Put(L"TargetSystem", 0, &v, CIM_REFERENCE);
VariantClear(&v);

Access to instance from another namespace will be look like:

SysAllocString(L"\\\\.\\root\\{SOME_NAMESPACE}:{SOME_CLASS}.Name=\"{YOUR_INSTACE_NAME}\"")

And some full code example: https://github.com/spmana/WMIWrapper/tree/master

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.