註冊提供者

實作提供者之前,您應該先向 WMI 註冊您的提供者。 註冊提供者會定義提供者的類型,以及提供者支援的類別。 WMI 只能存取已註冊的提供者。

注意

如需註冊 MI 提供者的詳細資訊,請參閱 如何:註冊 MI 提供者

 

您可以在註冊提供者之前先撰寫提供者程式碼。 不過,很難對未向 WMI 註冊的提供者進行偵錯。 判斷提供者的介面也有助於概述提供者的用途和結構。 因此,註冊您的提供者可協助您設計提供者。

只有系統管理員可以註冊或刪除提供者。

提供者必須針對它執行的所有不同類型的提供者函式註冊。 幾乎所有提供者都會提供其定義的類別實例,但也可能提供屬性資料、方法、事件或類別。 提供者也可以註冊為事件取用者提供者或效能計數器提供者。 建議您在一個提供者中合併所有提供者功能,而不是為每個類型提供許多個別的提供者。 例如, 系統登錄提供者提供方法和實例,以及提供實例、方法和事件的 磁片配額提供者

提供者必須針對它執行的所有不同類型的提供者函式註冊。 幾乎所有提供者都會提供其定義的類別實例,但也可能提供屬性資料、方法、事件或類別。 提供者也可以註冊為事件取用者提供者或效能計數器提供者。

每個註冊類型都會使用相同的 __Win32Provider 實例:

範例:建立和註冊提供者的實例

下列範例示範 MOF 檔案,該檔案會在 root\cimv2 命名空間中建立及註冊 系統登錄提供者 的實例。 它會將別名$Reg指派給提供者,以避免實例和方法註冊中所需的長路徑名稱。 如需詳細資訊,請參閱 建立別名

// Place the Registry provider in the root\cimv2 namespace
#pragma namespace("\\\\.\\ROOT\\cimv2")

// Create an instance of __Win32Provider
instance of __Win32Provider as $Reg
{
Name = "RegProv";        
CLSID = "{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}";
HostingModel = "NetworkServiceHost:LocalServiceHost";
};

// Register as an instance provider by
// creating an instance
// of __InstanceProviderRegistration
instance of __InstanceProviderRegistration
{
 provider = $Reg;
 SupportsDelete = FALSE;
 SupportsEnumeration = TRUE;
 SupportsGet = TRUE;
 SupportsPut = TRUE;
};

// Register as a method provider by
// creating an instance
// of __MethodProviderRegistration
instance of __MethodProviderRegistration
{
 provider = $Reg;
};

// Define the StdRegProv class
[dynamic: ToInstance, provider("RegProv")]
class StdRegProv
{
[implemented, static] uint32 CreateKey(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName);
[implemented, static] uint32 DeleteKey(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName);
[implemented, static] uint32 EnumKey(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [OUT] string sNames[]);
[implemented, static] uint32 EnumValues(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [OUT] string sNames[], 
 [OUT] sint32 Types[]);
[implemented, static] uint32 DeleteValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName);
 [implemented, static] uint32 SetDWORDValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [IN] uint32 uValue = 3);
[implemented, static] uint32 GetDWORDValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [OUT] uint32 uValue);
[implemented, static] uint32 SetStringValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [IN] string sValue = "hello");
[implemented, static] uint32 GetStringValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [in] string sValueName, 
 [OUT] string sValue);
[implemented, static] uint32 SetMultiStringValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [IN] string sValue[] = {"hello", "there"});
[implemented, static] uint32 GetMultiStringValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [OUT] string sValue[]);
[implemented, static] uint32 SetExpandedStringValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [IN] string sValue = "%path%");
[implemented, static] uint32 GetExpandedStringValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [OUT] string sValue);
[implemented, static] uint32 SetBinaryValue(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [in] string sValueName, 
 [in] uint8 uValue[] = {1, 2});
[implemented, static] uint32 GetBinaryValue(
 {IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] string sValueName, 
 [OUT] uint8 uValue[]);
[implemented, static] uint32 CheckAccess(
 [IN] uint32 hDefKey = 2147483650, 
 [IN] string sSubKeyName, 
 [IN] uint32 uRequired = 3, 
 [OUT] boolean bGranted);
};

範例:註冊提供者

下列程式描述如何註冊提供者。

註冊提供者

  1. 將提供者註冊為 COM 伺服器。

    如有必要,您可能需要建立登錄專案。 此程式適用于所有 COM 伺服器,且與 WMI 無關。 如需詳細資訊,請參閱 Microsoft Windows 軟體發展工具組 (SDK) 檔中的 COM 一節。

  2. 建立 MOF 檔案,其中包含 __Win32Provider 實例,以及直接或間接衍生自 __ProviderRegistration的類別實例,例如 __InstanceProviderRegistration。 只有系統管理員可以藉由建立衍生自 __Win32Provider__ProviderRegistration的類別實例來註冊或刪除提供者。

  3. 根據裝載模型中的值,在__Win32Provider實例中設定HostingModel

    注意

    除非提供者需要 LocalSystem 帳戶的高許可權, 否則 __Win32Provider.HostingModel 屬性應設定為 「NetworkServiceHost」。 如需詳細資訊,請參閱 提供者裝載和安全性

     

    下列來自完整範例的 MOF 範例會顯示建立 __Win32Provider實例的程式碼。

    instance of __Win32Provider as $Reg
    {
    Name = "RegProv";        
    CLSID = "{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}";
    HostingModel = "NetworkServiceHost:LocalServiceHost";
    };
    
  4. 直接或間接衍生自 __ProviderRegistration之類別的實例,描述提供者的邏輯實作。 提供者可以註冊數種不同類型的功能。 上述範例會將 RegProv 註冊為實例和方法提供者。 但是,如果 RegProv 支援此功能,它也可以註冊為屬性或事件提供者。 下表列出註冊提供者功能的類別。

    提供者註冊類別 描述
    __InstanceProviderRegistration 註冊 執行個體提供者
    __EventProviderRegistration 註冊 事件提供者
    __EventConsumerProviderRegistration 註冊 事件取用者提供者
    __MethodProviderRegistration 註冊 方法提供者
    __PropertyProviderRegistration 註冊 屬性提供者

     

  5. 將 MOF 檔案放入永久目錄。

    一般而言,您應該將檔案放在提供者的安裝目錄中。

  6. 使用 mofcompIMofCompiler 介面編譯 MOF 檔案。

    如需詳細資訊,請參閱 編譯 MOF 檔案

    Windows 8和Windows Server 2012:安裝提供者時,mofcompIMofCompiler介面會將 [Key] 和 [Static] 限定詞視為 true,不論其實際值為何。 如果其他限定詞存在,但未明確設定為 true,則會將其視為 false。

開發 WMI 提供者

設定 Namepace 安全性描述元

保護您的提供者