How to: Expose Configuration Settings Through WMI

You can use WMI Provider Extensions to create in-process WMI providers. These WMI providers are hosted by the WMI infrastructure and do not require another hosting application.

Example

The following code example demonstrates how to create an in-process WMI provider. It exposes a class named my_process in the root/MyProc namespace. This simple example recreates some of the functionality of the Win32_Process class, familiar to most users of WMI. It does so by leveraging the appropriate functionality in the System.Diagnostics.Process class. That functionality is mapped to WMI APIs and WMI class properties and methods by using appropriate attributes like ManagementProbeAttribute and ManagementEnumeratorAttribute.

The code includes a class called TheInstaller that is inherited from DefaultManagementInstaller. That class is used by installutil.exe to provide automatic registration of the provider with WMI.

Note that you must install the resulting dynamic-link library (dll) in the Global Assembly Cache (GAC). You can do this by using the gacutil.exe tool. Your assembly must be signed in order to install it in the GAC. To learn more about provider installation, see WMI.NET Provider Extensions Installation.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Diagnostics;
using ProcessInProc;

[assembly: WmiConfiguration("root/MyProc", HostingModel = ManagementHostingModel.NetworkService)]
// This is the installer class that installs an instrumented assembly.
// To use the default project installer, simply derive a class from
// DefaultManagementInstaller.  No methods need to be overridden.
[System.ComponentModel.RunInstaller(true)]
public class TheInstaller : DefaultManagementInstaller
{ }

namespace ProcessInProc
{
    // The management entity attribute 
    [ManagementEntity (Name="my_process")]
    public class ProcessProvider
    {
        int m_id;
        Process m_oProcess;

        //Specify that process ID will be the key value
        [ManagementKey]
        public int Id
        {
            get
            {
                return m_oProcess.Id;
            }
        }

        // Management probe attributes indicates read-only properties      on the corresponding WMI class
        [ManagementProbe]
        public string Name
        {
            get
            {
                return m_oProcess.ProcessName;
            }
        }

        [ManagementProbe]
        public long PrivateMemorySize
        {
            get
            {
                return m_oProcess.PrivateMemorySize64;
            }
        }

        [ManagementBind]
        public ProcessProvider(int Id)
        {
           m_oProcess = Process.GetProcessById(Id);
           Init(m_oProcess);
        }

        ProcessProvider(Process oProcess)
        {
            Init(oProcess);
        }

        private void Init(Process oProcess)
        {
            m_id = oProcess.Id;
            m_oProcess = oProcess;
        }

        [ManagementEnumerator]
        public static IEnumerable Enumerate()
        {
            Process[] rgProcesses = Process.GetProcesses();
            for (int i = 0; i < rgProcesses.Length; i++)
            {
                yield return new ProcessProvider(rgProcesses[i]);
            }
        }

    }
}

Compiling the Code

This example requires 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.