编写用于互操作的关联提供程序

关联提供程序提供了一种机制来注册配置文件,并将其与不同命名空间中实现的配置文件相关联。

关联提供程序用于公开标准配置文件,如电源配置文件。 这是通过在 root/interop 命名空间中编写一个关联提供程序实现的,该提供程序通过实现一个派生自 CIM_RegisteredProfile 的类来公开关联实例。 该提供程序必须在 root/interop 和 root/<implemented> 命名空间中注册,以支持跨命名空间遍历。

每当在 root/interop 命名空间中运行关联查询时,Windows Management Instrumentation (WMI) 就会加载关联提供程序。

实现用于互操作的关联提供程序

  1. CIM_RegisteredProfile 派生一个类,并在 root\interop 命名空间中创建该派生类的静态实例。 至少必须使用有效值传播以下属性:

    尽管 InstanceID 唯一定义了 CIM_RegisteredProfile 的实例,但 RegisteredName、RegisteredOrganization 和 RegisteredVersion 的组合必须在组织范围内唯一标识已注册的配置文件。 有关各个属性的详细信息,请参阅 CIM_RegisteredProfile

    以下代码示例说明从 CIM_RegisteredProfile 派生 ProcessProfile 类并填充静态实例的语法。

    class ProcessProfile : CIM_RegisteredProfile
    {
    };
    
    instance of ProcessProfile as $PP
    {
         InstanceID = "Process";
         RegisteredName = "Process";
         RegisteredOrganization = "1"; // Set to "Other"
         OtherRegisteredOrganization = "Microsoft";
         RegisteredVersion = "1.0";
    };
    

    注意

    对于 Windows 客户端,RegisteredOrganization 属性必须设置为 1,OtherRegisteredOrganization 属性必须设置为“Microsoft”。

     

  2. 创建返回 CIM_ElementConformsToProfile 关联实例的提供程序。 该过程分为两步。

    1. 在互操作和实现命名空间中创建一个派生自 CIM_ElementConformsToProfile 的类。 由于同一配置文件可以由不同的供应商实现,因此类的名称应该是唯一的。 建议的命名约定为“<组织>_<产品名称>_<类名>_<版本>”。 ConformantStandard 或 ManagedElement 属性必须指定包含此类所属命名空间的 MSFT_TargetNamespace 限定符。

      以下代码示例说明从 root\interop 命名空间中的 CIM_ElementConformsToProfile 派生 Microsoft_Process_ElementConformsToProfile_v1 类的语法。 在此示例中,Win32_Process 托管元素使用 MSFT_TargetNamespace 限定符来引用 root\cimv2 命名空间。

      #pragma namespace("\\\\.\\root\\interop")
      [Provider("ProcessAssociation"),Dynamic]
      Class Microsoft_Process_ElementConformsToProfile_v1: CIM_ElementConformsToProfile
      {
           CIM_RegisteredProfile ref ConformantStandard = $PP;
           [MSFT_TargetNamespace("root\\cimv2")]Win32_process ref ManagedElement = null;
      };
      

      以下代码示例说明从 root\cimv2 命名空间中的 CIM_ElementConformsToProfile 派生 Microsoft_Process_ElementConformsToProfile_v1 类的语法。 在此示例中,CIM_RegisteredProfile 合规标准使用 MSFT_TargetNamespace 限定符引用 root\interop 命名空间。

      #pragma namespace("\\\\.\\root\\cimv2")
      [Provider("ProcessAssociation"),Dynamic]
      Class Microsoft_Process_ElementConformsToProfile_v1: CIM_ElementConformsToProfile
      {
           [MSFT_TargetNamespace("root\\interop")] CIM_RegisteredProfile ref ConformantStandard = $PP;
           Win32_process ref ManagedElement = null;
      };
      

      如果未在引用 implemented 命名空间的属性中指定 MSFT_TargetNamespace 限定符,则“Associators of”语句的 ResultClass 筛选器将不起作用。 例如,如果未指定 MSFT_TargetNamespace 限定符,则以下 Windows PowerShell 命令行将不会返回对象:get-wmiobject -query "associators of {ProcessProfile.InstanceID='Process'} where resultclass='Win32_Process'"。

      MSFT_TargetNamespace 限定符不能指向远程计算机上的命名空间。 例如,不支持以下命名空间:MSFT_TargetNamespace(\\\\<RemoteMachine>\\root\\interop)。

    2. 编写一个提供程序用于返回创建的派生类的实例。 有关详细信息,请参阅编写实例提供程序。 访问跨命名空间实例时,可能必须访问客户端的安全级别。 有关详细信息,请参阅模拟客户端

      关联提供程序应实现 IWbemServices.CreateInstanceEnumAsyncIWbemServices.GetObjectAsync 方法。 实现 IWbemServices.ExecQueryAsync 方法是可选操作。 由于可以从 root\interop 和 root\implemented 命名空间访问此提供程序,因此不应显式依赖于该提供程序内部的命名空间<>。

  3. 在 root\interop 和 root\<implemented> 命名空间中注册关联提供程序。 有关详细信息,请参阅注册实例提供程序

    以下代码示例说明在 root\interop 命名空间中注册关联提供程序的语法。

    #pragma namespace("\\\\.\\root\\interop")
    instance of __Win32Provider as $P
    {
        Name    = "ProcessAssociation" ;
        ClsId   = "{DA13393B-A2D5-4BAC-9BD2-30B092E9EBB8}";
    } ;
    
    instance of __InstanceProviderRegistration
    {
        Provider = $P;
        SupportsPut = false;
        SupportsGet = TRUE;
        SupportsDelete = false;
        SupportsEnumeration = TRUE;
    };
    

    以下代码示例说明在 root\cimv2 命名空间中注册关联提供程序的语法。

    #pragma namespace("\\\\.\\root\\cimv2")
    instance of __Win32Provider as $R
    {
        Name    = "ProcessAssociation" ;
        ClsId   = "{DA13393B-A2D5-4BAC-9BD2-30B092E9EBB8}";
    } ;
    
    instance of __InstanceProviderRegistration
    {
        Provider = $R;
        SupportsPut = false;
        SupportsGet = TRUE;
        SupportsDelete = false;
        SupportsEnumeration = TRUE;
    };
    
  4. CIM_ElementConformsToProfile 的架构放入 implemented 命名空间中。 对于 Windows 客户端,这是位于 %systemroot%\system32\wbem 文件夹中的 interop.mof 文件。

  5. 为提供程序实现 IWbemProviderInit 接口。

    WMI 使用 IWbemProviderInit 加载和初始化提供程序。 IWbemProviderInit.Initialize 方法的实现方式应允许为两个不同的命名空间调用该方法。 有关详细信息,请参阅初始化提供程序

CIM_ElementConformsToProfile

CIM_RegisteredProfile

编写实例提供程序

注册实例提供程序