Article : Management Strongly Typed Class Generator - Mgmtclassgen

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey Group,

Today we will discuss Management Strongly Typed Class Generator aka
Mgmtclassgen.exe. This tool is used to generate an early - bound managed
class for Windows Management Instrumentation Classes.

What are Windows Management Instrumentation Classes ?

Well windows SDK has a set of classes by which you can access the various
devices associated to your computer like your harddisk, memory,floppy drive,
processor, sound card , mointor etc.

You have an entire list of these classes. Check out the below link for the
list

http://msdn.microsoft.com/library/d...isdk/wmi/computer_system_hardware_classes.asp

Creating a managed class for WMI class is very simple.

Go to VS.Net command prompt type

mgmtclassgen /? /// this will list all the options.

/N <WMINamespace> WMI namespace containing the class.Defaults
to root\cimv2
/O <ClassNamespace> .NET namespace in which the class isgenerated
/L <Generated Language> One of the following. Defaulted to CS
CS - CSharp(C#)
VB - Visual Basic
JS - JScript
/P <FilePath> Output file path
/M <Machine> Remote computer to connect. Defaults
to the local machine
/U <User> User name
/PW <Password> Password
/? Displays this help screen

Example : MgmtclassGen Win32_Logicaldisk /L VB /N root\cimv2 /P
c:\temp\logicaldisk.vb

Note : The help also gives an example showing how to use the tool.

Let us now create our own managed class for SoundDevice and Desktop Monitor
associated with our machine.

Go to VS.Net and type the following command to generate a managed class
forsound device

Mgmtclassgen Win32_SoundDevice /L CS /N root\cimv2 /P soundDevice.cs

You should get the following message

Microsoft (R) Management Strongly Typed Class Generator Version
1.1.4322.573
Copyright c Microsoft Corporation 1998-2002. All rights reserved.
Generating Code for WMI Class Win32_SoundDevice ...
Code Generated Successfully!!!!

Use the below command to generate a managed class for desktop monitor

Mgmtclassgen Win32_DesktopMonitor /L CS /N ROOT/CIMV2 /P
DesktopMonitor.cs

You should get the following message

Microsoft (R) Management Strongly Typed Class Generator Version
1.1.4322.573
Copyright c Microsoft Corporation 1998-2002. All rights reserved.
Generating Code for WMI Class Win32_DesktopMonitor ...
Code Generated Successfully!!!!

Managed classes for SoundDevice and DesktopMonitor are ready !!!!

Let use them in our application.

Create a console application and add these two classes to the project.

Add using WMI = ROOT.CIMV2.Win32 at the top. Here WMI is an alias to the
namespace name.

Type the following code in the static Main() for your class

public static void Main()
{

Console.WriteLine(WMI.SoundDevice.GetInstances().Count);

foreach(WMI.SoundDevice sd in
WMI.SoundDevice.GetInstances())
{
Console.WriteLine(sd.DeviceID);
Console.WriteLine(sd.SystemName);
Console.WriteLine(sd.Description);
}

Console.WriteLine("MONITOR");

Console.WriteLine(" ");


Console.WriteLine(WMI.DesktopMonitor.GetInstances().Count);

foreach(WMI.DesktopMonitor DM in
WMI.DesktopMonitor.GetInstances())
{
Console.WriteLine(DM.ScreenHeight + " " +
DM.ScreenWidth);
}

Console.ReadLine();
}

In the above code we displaying values of some basic properties for sound
device and desktop monitor.

-- Please post your queries and comments for my articles in the usergroup
for the benefit of all. I hope this step from my end is helpful to all of us.

Regards,
Namratha (Nasha)
MyBlog -- http://spaces.msn.com/members/nasha
 
Back
Top