How to: Create a Basic In-Process WMI Provider

In-process WMI providers are hosted by the WMI service. The WMI functionality they provide available as long as the WMI infrastructure is functioning correctly. This example is a dynamic-link library (DLL). This simple DLL is a great starting point when learning how to create in-process providers by using WMI.NET provider extensions.

Note

This simple example uses the Singleton parameter of the ManagementEntityAttribute attribute to implement a WMI class that can only have a single instance. This was done just to keep the code to a minimum and provide you with a very basic in-process provider that you can easily understand, compile, register and test. The provider framework is in no way limited to creating singleton WMI classes. To see a richer in-process code sample without this limitation, see How to: Expose Configuration Settings Through WMI.

The following steps were followed to create and compile the code sample.

To create the sample

  1. Create a new Windows Class Library Visual Studio project.

  2. Use the WmiConfigurationAttribute assembly-level attribute to specify that the provider publishes classes to the root/BasicInProc namespace and uses an in-process hosting model. In this case, the HostingModel is set to NetworkService, which is one of the three choices for in-process providers.

  3. Define a public class that derives from the installer class DefaultManagementInstaller. Use the RunInstallerAttribute attribute to specify that the Installer Tool (Installutil.exe) should call the Install method of the installer class.

  4. Define a public class, ReadOnlyProperty, in which to implement the provider. Use the ManagementEntityAttribute to indicate that the class implements a singleton provider.

  5. Add a public string variable, TestProperty, to create a corresponding WMI class property. Use the ManagementProbeAttribute attribute to mark the string as a read-only WMI property.

  6. Define a public, static method, Bind(), to associate the WMI class with an instance of that class. In the case of this singleton class, do that by creating an instance of the class, setting its one read-only property and returning it. Use the ManagementBindAttribute to identify the method as the one that creates or retrieves and returns the appropriate instance of the WMI class.

Deploying the In-Process WMI Provider

Take the following steps to deploy a decoupled WMI provider.

To deploy an in-process WMI provider

  1. Compile the provider library. Ensure that it is signed so you can install it in the Global Assembly Cache (GAC).

  2. Use the gacutil command line tool to install the provider library in the GAC.

  3. Run installutil against the library to register it.

Example

The following sample illustrates how to create a basic in-process WMI provider by using the WMI.NET Provider Extensions framework.

using System;
using System.Management.Instrumentation;

[assembly: WmiConfiguration("root/BasicInProc", HostingModel = ManagementHostingModel.NetworkService)]

[System.ComponentModel.RunInstaller(true)]

public class TheInstaller : DefaultManagementInstaller
{ 
}

namespace ROP
{
   [ManagementEntity(Singleton=true)]
   public class ReadOnlyProperty
   {
      [ManagementProbe]
      public string TestProperty;

      [ManagementBind]
      public static ReadOnlyProperty Bind()
      {
         ReadOnlyProperty rop = new ReadOnlyProperty();
         rop.TestProperty = "Fixed Value";
         return rop;
      }  

   }
}

Compiling the Code

To compile this code, you must include references to System.Management.Instrumentation and System.Configuration.Install.

See Also

Reference

System.Management.Instrumentation

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.