Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WMI return type convert

Tags:

c++

wmi

I'm making an plugin for Open Hardware Monitor. But I have a problem using the Windows wmi database. I can run a query and I get an value from it. But I can't convert the value to the correct type. Because I create a query on runtime I can't know what type the WMI database must return to run without errors. In my case i can get a float or a string. But when i get an float from the database and i convert it to a string i got an memory error. And if i convert a string to the float I doesn't get any errors. Now how can i create a check if the BSTR value is invalid? Or how can I check which type i must use? I have tested it with some if statment or with a try catch. But non of them are working.

try
{
    wstring ws(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
    returnValue = string(ws.begin(), ws.end());
    ws.clear();
}
catch (...)
{
    float temp = vtProp.fltVal;
    returnValue = to_string(temp);
}
like image 442
JimmyD Avatar asked Sep 18 '26 17:09

JimmyD


1 Answers

You can retrieve the type of the WMI property using the IWbemClassObject::Get method. and then use the proper field of the VARIANT variable to access the value.

Try this sample

VARIANT vtProp;
CIMTYPE pType;

//here put the real property Name
hr = pclsObj->Get(L"PropertyName", 0, &vtProp, &pType, 0);
if (!FAILED(hr))
{
 //check if the property is a string 
 if (pType == CIM_STRING && pType != CIM_EMPTY && pType!= CIM_ILLEGAL)
 {
      wcout << "Value : " << vtProp.bstrVal << endl;
 }
 else
 //check if the property is a float
 if (pType == CIM_REAL32 && pType != CIM_EMPTY && pType!= CIM_ILLEGAL)
 {
      wcout << "Value : " << vtProp.fltVal << endl;
 }

}
VariantClear(&vtProp);
like image 97
RRUZ Avatar answered Sep 20 '26 07:09

RRUZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!