无法在WMI(c ++)中检索对象属性 [英] Failed to retrieve object property in WMI (c++)

查看:192
本文介绍了无法在WMI(c ++)中检索对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



通过WMI接收事件通知



此程序通过WMI收到一个事件通知(进程创建),并在收到该事件后调用EventSink :: Indicate函数。



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

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

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

  for(int i = 0; i  {
VARIANT varName;
hres = apObjArray [i] - > Get(_bstr_t(LName),
0,& varName,0,0);
// ...
}

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



请让我知道我错过了什么?任何帮助都不胜感激。

解决方案

名称属性是 TargetInstance 对象,因此您必须获取TargetInstance对象的值,然后检索 Name 属性的值。 / p>

尝试此示例

  HRESULT EventSink ::指示(long lObjectCount, 
IWbemClassObject ** apObjArray)
{
HRESULT hr = S_OK;
_variant_t vtProp; (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(LName,0,& cn,NULL,NULL);
if(SUCCEEDED(hr))
{
if((cn.vt == VT_NULL)||(cn.vt == VT_EMPTY))
wcout<< 名称:<< ((cn.vt == VT_NULL)?NULL:EMPTY)<< ENDL;
else
wcout<<< 名称:<< cn.bstrVal<< ENDL;
}
VariantClear(& cn);


}
}
VariantClear(& vtProp);

}

返回WBEM_S_NO_ERROR;
}


I want to do something with WMI (receiving some event notification) so I start with simple example from MSDN website:

Receiving Event Notifications Through WMI

this program receives an event notification (process creation) through WMI, and calls the function EventSink::Indicate upon receiving the event.

I used the same code in the link above (copy/past) with one change: in the class EventSink, the function

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

I added few lines to retrieve a property of the object (the object is returned in apObjArray):

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

now the Get(...) functions returns WBEM_E_NOT_FOUND (The specified property is not found) no matter what I look for (am sure from the documentation that the properties are there...)

please let me know what have I missed ?! any help is appreciated.

解决方案

The Name property is part of the TargetInstance object, so you must get the value of the TargetInstance object and then retrieve the value of the Name property.

Try this sample

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;
}

这篇关于无法在WMI(c ++)中检索对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆