Welcome to MSDN Blogs Sign in | Join | Help

Creating Strongly-Typed WMI Classes with Mgmtclassgen.exe

My recent work involved writing a C# to automating the installs of server products. A lot of the work involved using WMI.

Generally, working with WMI in scripting languages like Perl, Python, or VBScript is easy but using it in C# can be difficult. WMI is very late-bound and that can be an unpleasant burden with an early-bound language like C#.

Mgmtclassgen.exe makes it simple

Mgmtclassgen.exe creates stringly-typed wrapper "C#" wrapper classes for WMI classes. It has made a substantial amount of my WMI work very easy.

A simple snippet:

      foreach(Process ps in Process.GetInstances())
      {
         Console.WriteLine(ps.Name);
      }

How to get Mgmtclassgen.exe

Search in your "Program Files" folder for your version of visual studio and you should find Mgmtclassgen.exe. I've found it in Visual Studio 2003 and Visual Studio 2005.

How to use Mgmtclassgen.exe

Here are the  MSDN docs for mgmtclassgen.exe 

A simple example:

C:> mgmtclassgen.exe Win32_Service

This will generate a .CS file containing the strongly-typed wrapper for the Win32_Service class

 

Published Friday, November 18, 2005 1:12 AM by saveenr

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Creating Strongly-Typed WMI Classes with Mgmtclassgen.exe

This is very helpful. Thanks!
Friday, November 18, 2005 9:40 AM by marlowe

# re: Creating Strongly-Typed WMI Classes with Mgmtclassgen.exe

Is there any reason you chose not to derive the generated class from ManagementBaseObject or ManagementObject? I'm thinking of creating my own custom tool to generate strongly-typed code for a WMI instance (and its various ancestors) such that it matches the hierarchy described in WMI. I'd also like to fix the 300-odd warnings that the generated code causes, but that's just aesthetics. ;)
Monday, September 25, 2006 1:42 PM by Richard

# Sql Server, WMI and PowerShell

So I started out on a quest: a quest for the overview of installed SQL Server instances on my machine.

Wednesday, March 12, 2008 10:25 AM by Peter's Pattern

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker