Monday, July 30, 2007

Generating Strongly Typed WMI Classes

I have been playing around with .Net WMI class generation using the mgmtclassgen tool provided by Visual Studio 2005. This tool generates a strongly typed WMI class that can then be used to call methods for a paticular WMI class or retrieve properties. Here is an example on the usage of mgmtclassgen.exe:

mgmtclassgen Win32_Share /n root\cimv2 /l CS /p .\Win32_Share.cs /o WMITest

The first argument is the WMIClass to generate the source and the rest are optional arguments for namespace, the language to use, and what to name the file. When including a generated class in a Visual Studio project, you get the benefit of IntelliSense.

Here is an example of using a generated class in a console .Net 2.0 application:

class Program
{

static void Main(string[] args)
{
ConnectionOptions opt = new ConnectionOptions();
opt.Username = "username";
opt.Password = "password";

ManagementScope scope = new
ManagementScope("\\\\computername\\root\\cimv2", opt);

foreach (Win32_ConnectionShare obj in
Win32_ConnectionShare.GetInstances(scope, new EnumerationOptions()))
{
Console.WriteLine(new Win32_Share(scope,obj.Antecedent).Name);
Console.WriteLine(new
Win32_ServerConnection(scope, obj.Dependent).ShareName);
}
}
}


Although the class generation is a hoop to jump through, the code is cleaner than the late binding style of using an ObjectSearcher or WQL queries. I also find this approach better when I want to populate a dictionary object with the WMI objects for a specific computer I want to gather info about.

For reference about mgmtclassgen usage:

http://msdn2.microsoft.com/en-us/library/2wkebaxa(vs.80).aspx

Sunday, July 29, 2007

Introduction

"I'm Blogging This"

Well now I can officially use this phrase. Well sort of, I don't plan to do much social commentary or editorials of current events but it might make it in at times. If you want to know more about me, check out the profile page.

I began this blog to share my experience in learning what I love, Software Development and to pass what I learn on to others. What am I learning currently? Well, glad you asked. I thought I had a good handle on C# and .Net development and have in fact put together several applications written in C#. What I found out recently is that some fundamental knowledge about the language nad the .Net framework escaped me and as a result, I am spending a great deal of time filling in the holes I have in my knowledge. In the process, I am sharing what I am learning here on this blog.

Well, I hope many read my blog and find its content helpful, insightful, and useful. To all enjoy.

Saturday, July 28, 2007

Simple Delegates

Over the past couple of days I have been learning the use of C# delegates. The simple explanation of a delegate is that it is a type that references a method. C++ developers know this as function pointers. The big difference between C++ function pointers and C# delegates is type safety. In practice a delegate looks like a cross between a method prototype and an object definition. In a way, it sort of is and this in part is what makes it so powerful. Also, a delegate can be reused for methods bearing the same method signature described by the delegate.

The most common use of delegates is for callbacks. I will cover callbacks in my next post as I am still doing a lot playing around with them before I write about how they work.

Here is a very simple example using a delegate:

class Program
{
delegate bool sentinel(int sent);

static void Main(string[] args)
{
int max = int.MaxValue;
int notMax = 1;

sentinel check = new sentinel(SampleMethod.isMaxInt);

Console.WriteLine(check(max));
Console.WriteLine(check(notMax));

}
}
class SampleMethod
{
public static bool isMaxInt(int testInt)
{
return (testInt == int.MaxValue) ? true : false;
}
}


References:

Delegates (C# Programming Guide)
http://msdn2.microsoft.com/en-US/library/ms173171(VS.80).aspx

Professional C# 3rd Edition
Robinson, Simon. Nagel, Christian. Glynn, Jay. Skinner, Morgan. Watson, Karli. Evjen, Bill. Wiley Publishing, Inc. 2004