勇芳软件工作室.汉化:  Windows Management Instrumentation > COM API for WMI > Interfaces > IWbemServices >

GetObjectAsync Method

Previous pageReturn to chapter overviewNext page

描述

 

IWbemServices.GetObjectAsync方法基于其路径检索对象(类定义或实例)。这与IWbemServices.GetObject类似,只是调用立即返回,并将对象提供给提供的对象接收器。

 

目前,此方法仅从与IWbemServices指针相关联的命名空间中检索对象。

 

C++ 语法

 

HRESULT GetObjectAsync(

[in]  const BSTR strObjectPath,

[in]  LONG lFlags,

[in]  IWbemContext *pCtx,

[in]  IWbemObjectSink *pResponseHandler

);

 

PowerBASIC 语法

 

METHOD GetObjectAsync ( _

BYVAL strObjectPath AS DWORD, _

BYVAL lFlags AS LONG, _

BYVAL pCtx AS IWbemContext, _

BYVAL pResponseHandler AS IWbemCallResult _

) AS LONG

 

参数

 

strObjectPath

 

检索对象的路径。对于实例提供者,StrObjectPath可以采用以下格式:

 

·命名空间:Class.Key =“Value”
·命名空间:Class =“Value”
·命名空间:Class.Key =“Value”,Key2 =“Value2”

 

在类之前指定命名空间是可选的。没有命名空间的对象路径指的是当前命名空间中的实例。如果需要,您可以用双引号(')替换单引号(')来分隔字符串属性类型的开始和结束。

 

如果这是NULL,则返回一个可以成为新类的空对象。有关详细信息,请参阅创建一个类.

 

lFlags

 

以下标志影响此方法的行为。

 

含义

WBEM_FLAG_USE_AMENDED_QUALIFIERS

如果设置此标志,WMI将检索存储在当前连接的本地化名称空间中的修改后的限定符。如果未设置,则仅检索存储在立即命名空间中的限定符。

WBEM_FLAG_DIRECT_READ

该标志引起对指定类的提供者的直接访问,而不考虑其父类或子类。

WBEM_FLAG_SEND_STATUS

通过客户端实施IWbemObjectSink.SetStatus注册接收中间状态报告的请求。提供商实施必须支持该标志的中间状态报告来改变行为。

 

pCtx

 

通常为NULL。否则,这是一个指向IWbemContext对象的指针,提供者可以使用它来返回所请求的类或实例。必须在提供程序的文档中指定上下文对象中的值。有关此参数的更多信息,请参阅拨打电话给WMI.

 

pResponseHandler

 

指向呼叫者执行IWbemObjectSink的指针。该处理程序通过IWbemObjectSink.Indicate方法可用时接收所请求的对象。pObjParam参数包含该对象。如果返回任何错误代码,则不使用提供的IWbemObjectSink指针。如果返回WBEM_S_NO_ERROR,则调用用户的IWbemObjectSink实现来指示操作的结果。如果WBEM_S_NO_ERROR返回,Windows Management才会调用AddRef到指针。在错误代码返回的情况下,引用计数与输入相同。有关此参数的详细信息,请参阅调用方法.

 

返回值

 

此方法返回一个HRESULT,指示方法调用的状态。下表列出了HRESULT中包含的值。

 

失败后,您可以从COM函数GetErrorInfo获取任何可用的信息。

 

如果网络问题导致您失去与Windows Management的远程连接,也可以返回COM特定的错误代码。

 

退货代码

描述

WBEM_E_FAILED

这表示其他未指定的错误。

WBEM_E_INVALID_PARAMETER

指定了无效参数。

WBEM_E_INVALID_CLASS

指定的类不存在。

WBEM_E_INVALID_OBJECT_PATH

指定的路径无效。

WBEM_S_NO_ERROR

通话成功。

WBEM_E_NOT_FOUND

提供者无法识别所请求的类。

WBEM_E_PROVIDER_NOT_CAPABLE

提供者无法检索请求的实例,即使该类名被识别为提供者提供。

WBEM_E_SHUTTING_DOWN

Windows管理服务可能已停止并重新启动。需要新的电话ConnectServer.

 

示例代码

 

以下示例介绍如何为实例提供程序实现GetObjectAsync.

 

SCODE CInstPro::GetObjectAsync (BSTR ObjectPath,

                              long lFlags, IWbemContext *pCtx,

                              IWbemObjectSink FAR* pHandler)

{

  ULONG cRef;         // Reference count

  SCODE sc;

  BOOL bOK = FALSE;

 

  IWbemServices *  m_pNamespace;

  IWbemClassObject FAR* pObj;

 

  // Check arguments.

 

  if(ObjectPath == NULL || pHandler == NULL ||

      m_pNamespace == NULL)

      return WBEM_E_INVALID_PARAMETER;

 

 

  // Based on the object path, produce an empty instance

  // of the class and gather required data,

  // setting the instance's property values accordingly.

  // This example assumes that GetByPath is implemented

  // by the provider to do this.

  // The IWbemPath interface can be used to parse

  // the object path, separating the namespace and class name.

 

  sc = GetByPath (ObjectPath, &pObj, pCtx);

  if(sc == S_OK)

  {

      pHandler->Indicate (1, &pObj);

      pObj->Release();

      bOK = TRUE;

  }

 

  sc = (bOK) ? S_OK : WBEM_E_NOT_FOUND;

 

  // Set status.

 

  pHandler->SetStatus(0,sc, NULL, NULL);

 

  // Free memory resources.

 

  SysFreeString(ObjectPath);

  m_pNamespace->Release();

  pObj->Release();

 

  return sc;

 

}

 

以下示例显示了典型的类提供程序如何实现GetObjectAsync.

 

HRESULT CStdProvider::GetObjectAsync(

          /* [in] */ BSTR strObjectPath,

          /* [in] */ long lFlags,

          /* [in] */ IWbemContext __RPC_FAR *pCtx,

          /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler

          )

{

 

  IWbemClassObject *pClass = 0;

 

// Assume there is an IWbemServices pointer available.

// Retrieve an 'empty' object which is built up

// into the class definition.

 

  HRESULT hRes = m_pSvc->GetObject(NULL, 0, NULL, &pClass, 0);

  if (hRes)

      return hRes;

 

// Parse the object path and determine which class is  

// required. The path string is the required class name.

// Fill in the properties required for the class definition

// using pClass->Put(...), and so on.

 

 

  // ...

 

  // Send the class definition back to WMI.

  pResponseHandler->Indicate(1, &pClass);

 

// Indicate that it is now finished.

 

  pResponseHandler->SetStatus(0, WBEM_S_NO_ERROR, 0, 0);

  SysFreeString(strObjectPath);

  m_pSvc->Relaase();

  pClass->Release();  // This is no longer needed.

  return WBEM_S_NO_ERROR;

}

 

备注

 

实现实例提供程序时,您应该使用PSDK的WMI部分中的对象路径解析器示例代码来解析客户端请求的对象的对象路径。此外,支持派生类的提供程序只需要提供类的本地属性的值,而不是继承的属性。WMI请求基类的提供者处理继承的属性。

 

当实现类提供者时,GetObjectAsync必须通过解析存储在strObjectPath参数中的类名称对象路径来确定正在请求哪个类。然后,GetObjectAsync方法可以动态生成类,也可以从私有缓存中获取类。然后,GetObjectAsync使用pResponseHandler参数指向的IWbemObjectSink指针将类发送到WMI。调用IWbemObjectSink.SetStatus方法来指示结果集的结束。如果发生错误条件,也可以调用IWbemObjectSink.Indicate而不进行任何调用。

 

因为回调可能不会以与客户端所需的相同的身份验证级别返回,所以建议您使用半异步而不是异步通信。如果需要异步通信,请参阅调用方法.

 

有关半同步使用方法的更多信息,请参阅IWbemServices.GetObject调用方法.

 

要求

 

客户

需要Windows Vista,Windows XP,Windows 2000 Professional,Windows NT Workstation 4.0 SP4及更高版本,Windows Me,Windows 98或Windows 95。

服务器

需要Windows Server 2008,Windows Server 2003,Windows 2000 Server或Windows NT Server 4.0 SP4及更高版本。

在Wbemcli.inc中声明。

DLL

需要Fastprox.dll。

需要Esscli.dll。

需要Framedyn.dll。

需要Ntevt.dll。

需要Stdprov.dll。

需要Viewprov.dll。

需要Wbemcomn.dll。

需要Wbemcore.dll。

需要Wbemess.dll。

需要Wbemsvc.dll。

需要Wmipicmp.dll。

需要Wmidcprv.dll。

需要Wmipjobj.dll。

需要Wmiprvsd.dll。