註冊提供者
在實作提供者之前,您應該先向 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);
};
範例:註冊提供者
下列程序說明如何註冊提供者。
註冊提供者
將提供者註冊為 COM 伺服器。
如有必要,您可能需要建立註冊表項。 此程式適用於所有 COM 伺服器,且與 WMI 無關。 如需詳細資訊,請參閱 Microsoft Windows 軟體開發工具包 (SDK) 檔中的 COM 一節。
建立MOF檔案,其中包含 __Win32Provider 實體,以及直接或間接衍生自 __ProviderRegistration的類別實例,例如 __InstanceProviderRegistration。 只有系統管理員可以藉由建立衍生自 __Win32Provider 或 __ProviderRegistration的類別實例來註冊或刪除提供者。
根據裝載模型 中的值,在 __Win32Provider 實例中設定 HostingModel。
注意
除非提供者需要 LocalSystem 帳戶的高許可權,否則 __Win32Provider.HostingModel 屬性應設定為 “NetworkServiceHost”。 如需詳細資訊,請參閱 提供者主機服務和安全性。
下列來自完整範例的MOF範例顯示建立 __Win32Provider實例的程序代碼。
instance of __Win32Provider as $Reg { Name = "RegProv"; CLSID = "{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}"; HostingModel = "NetworkServiceHost:LocalServiceHost"; };
直接或間接衍生自 __ProviderRegistration之類別的實例,描述提供者的邏輯實作。 提供者可以註冊數種不同類型的功能。 上述範例會將 RegProv 註冊為實例和方法提供者。 但是,如果 RegProv 支援此功能,它也可以註冊為屬性或事件提供者。 下表列出註冊提供者功能的類別。
提供者註冊類別 描述 __InstanceProviderRegistration 將實例提供者註冊。 __EventProviderRegistration 註冊事件提供者。 __EventConsumerProviderRegistration 註冊 事件取用者。 __方法提供者註冊 註冊 方法提供者。 __PropertyProviderRegistration 註冊為的屬性提供者。 將MOF檔案放入永久目錄。
一般而言,您應該將檔案放在提供者的安裝目錄中。
使用mofcomp 或 IMofCompiler 介面編譯MOF檔案。
如需詳細資訊,請參閱 編譯MOF檔案。
Windows 8 和 Windows Server 2012: 安裝提供者時,mofcomp 和 IMofCompiler 介面會將 [Key] 和 [Static] 限定符視為 true,不論其實際值為何。 如果其他限定符存在,但未明確設定為 true,則會將其視為 false。
相關主題