我正在为Open Hardware Monitor制作插件。但是我在使用Windows wmi数据库时遇到问题。我可以运行查询,并从中获取值。但是我无法将值转换为正确的类型。因为我在运行时创建查询,所以我不知道WMI数据库必须返回哪种类型才能正确运行。就我而言,我可以得到一个浮点数或一个字符串。但是,当我从数据库中获取一个浮点数并将其转换为字符串时,出现了内存错误。而且,如果我将字符串转换为浮点数,则不会出现任何错误。现在如何创建BSTR值无效的检查?或者如何检查我必须使用哪种类型?我已经用一些陈述或尝试抓住了它进行了测试。但是他们都不在工作。
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);
}
您可以type of the WMI property
使用IWbemClassObject::Get
方法检索。然后使用VARIANT变量的正确字段访问值。
试试这个样本
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句