gpt4 book ai didi

c++ - 无法在 WMI 中检索对象属性 (c++)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:12 26 4
gpt4 key购买 nike

我想用 WMI 做一些事情(接收一些事件通知)所以我从 MSDN 网站上的简单示例开始:

Receiving Event Notifications Through WMI

本程序通过WMI接收事件通知(进程创建),接收到事件后调用函数EventSink::Indicate。

我在上面的链接(复制/过去)中使用了相同的代码,但有一处更改:在 EventSink 类中,函数

HRESULT EventSink::Indicate(long lObjectCount, IWbemClassObject **apObjArray)

我添加了几行来检索对象的属性(对象在 apObjArray 中返回):

 for (int i = 0; i < lObjectCount; i++)
{
VARIANT varName;
hres = apObjArray[i]->Get(_bstr_t(L"Name"),
0, &varName, 0, 0);
//...
}

现在 Get(...) 函数返回 WBEM_E_NOT_FOUND(未找到指定的属性),无论我寻找什么(从文档中确定属性在那里...)

请让我知道我错过了什么?!任何帮助表示赞赏。

最佳答案

Name属性是 TargetInstance 的一部分对象,因此您必须获取 TargetInstance 对象的值,然后检索 Name 的值属性(property)。

试试这个例子

HRESULT EventSink::Indicate(long lObjectCount,
IWbemClassObject **apObjArray)
{
HRESULT hr = S_OK;
_variant_t vtProp;

for (int i = 0; i < lObjectCount; i++)
{

hr = apObjArray[i]->Get(_bstr_t(L"TargetInstance"), 0, &vtProp, 0, 0);
if (!FAILED(hr))
{
IUnknown* str = vtProp;
hr = str->QueryInterface( IID_IWbemClassObject, reinterpret_cast< void** >( &apObjArray[i] ) );
if ( SUCCEEDED( hr ) )
{
_variant_t cn;
hr = apObjArray[i]->Get( L"Name", 0, &cn, NULL, NULL );
if ( SUCCEEDED( hr ) )
{
if ((cn.vt==VT_NULL) || (cn.vt==VT_EMPTY))
wcout << "Name : " << ((cn.vt==VT_NULL) ? "NULL" : "EMPTY") << endl;
else
wcout << "Name : " << cn.bstrVal << endl;
}
VariantClear(&cn);


}
}
VariantClear(&vtProp);

}

return WBEM_S_NO_ERROR;
}

关于c++ - 无法在 WMI 中检索对象属性 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12661648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com