Failed to retrieve object property in WMI (c++)

假如想象 提交于 2019-12-19 03:16:12

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/12661648/failed-to-retrieve-object-property-in-wmi-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!