Redigera

Dela via


Registering a Provider

Before implementing your provider, you should first register your provider with WMI. Registering the provider defines the type of the provider and the classes that the provider supports. WMI can only access registered providers.

Note

For more information on registering an MI provider, see How to: Register an MI Provider.

 

You can write your provider code before you register the provider. However, it is very difficult to debug a provider that is not registered with WMI. Determining the interfaces for your provider also helps to outline the purpose and structure of a provider. Therefore, registering your provider helps you design your provider.

Only administrators can register or delete a provider.

A provider must be registered for all the different types of provider functions that it performs. Nearly all providers supply instances of classes they define, but they may also provide property data, methods, events, or classes. The provider may also be registered as an event consumer provider or a performance counter provider. It is recommended that you combine all provider functionality in one provider rather than having many separate providers for each type. An example is the System Registry provider, which provides methods and instances, and the Disk Quota provider, which supplies instances, methods, and events.

A provider must be registered for all the different types of provider functions that it performs. Nearly all providers supply instances of classes they define, but they may also provide property data, methods, events, or classes. The provider may also be registered as an event consumer provider or a performance counter provider.

The same instance of __Win32Provider is used for each type of registration:

Example: Creating and Registering an Instance of a Provider

The following example shows a MOF file that creates and registers an instance of the System Registry provider in the root\cimv2 namespace. It assigns the alias $Reg to the provider to avoid the long pathname required in the instance and method registrations. For more information, see Creating an Alias.

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

Example: Registering a Provider

The following procedure describes how to register a provider.

To register a provider

  1. Register the provider as a COM server.

    If necessary, you may need to create registry entries. This process applies to all COM servers and is unrelated to WMI. For more information, see the COM section in the Microsoft Windows Software Development Kit (SDK) documentation.

  2. Create a MOF file that contains instances of __Win32Provider and an instance of a class derived either directly or indirectly from __ProviderRegistration, such as __InstanceProviderRegistration. Only administrators can register or delete a provider by creating instances of classes derived from __Win32Provider or __ProviderRegistration.

  3. Set the HostingModel in the instance of __Win32Provider according to the values in Hosting models.

    Note

    Unless the provider requires the high privileges of the LocalSystem account, the __Win32Provider.HostingModel property should be set to "NetworkServiceHost". For more information, see Provider Hosting and Security.

     

    The following MOF example from the full example shows the code that creates an instance of __Win32Provider.

    instance of __Win32Provider as $Reg
    {
    Name = "RegProv";        
    CLSID = "{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}";
    HostingModel = "NetworkServiceHost:LocalServiceHost";
    };
    
  4. An instance of a class derived either directly or indirectly from __ProviderRegistration, to describe the logical implementation of the provider. A provider can be registered for several different types of functionality. The example above registers RegProv as an instance and method provider. But if RegProv supports the functionality, it could also be registered as a property or event provider. The following table lists the classes that register provider functionality.

    Provider registration classes Description
    __InstanceProviderRegistration Registers an instance provider.
    __EventProviderRegistration Registers an event provider.
    __EventConsumerProviderRegistration Registers an event consumer provider.
    __MethodProviderRegistration Registers a method provider.
    __PropertyProviderRegistration Registers a property provider.

     

  5. Place the MOF file into a permanent directory.

    Typically, you should place the file in the installation directory of the provider.

  6. Compile the MOF file using mofcomp or the IMofCompiler interface.

    For more information, see Compiling MOF Files.

    Windows 8 and Windows Server 2012: When installing providers, both mofcomp and the IMofCompiler interface treat the [Key] and [Static] qualifiers as true if they are present, regardless of their actual values. Other qualifiers are treated as false if they are present but not explicitly set to true.

Developing a WMI Provider

Setting Namepace Security Descriptors

Securing Your Provider