Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

Determine Performance Requirements (Continued)

Drill Down on WMI Classes
Begin your exploration of the built-in WMI classes by opening the Server Explorer (Ctrl-Alt-S). This application is an explorer-style interface within Visual Studio .NET. It provides developers with a convenient tool for browsing through resources, including data schemas, performance counters, and yes, WMI events and classes. You can drag many objects you view in the Server Explorer onto the IDE design surface and generate code automatically.

ADVERTISEMENT

Next, navigate through the treeview in this window under Servers and your machine name. (If you are on a network, you might want to add other machines under Servers for browsing). Expand the node labeled Management Classes to see a list of high-level WMI classes that you can manage (see Figure 1).

Many of WMI’s classes are nested in hierarchical relationships. For example, Win32_Threads are children of Win32_Process. The process table on a developer’s workstation can prove highly volatile, so you might need to click on the Refresh button on the Server Explorer toolbar occasionally to bring these contents up to date.

In this example, you want to create a solution built around a new Web site in Visual Studio .NET 2005 (File | New | Web Site…) named WmiGridView. Using the WMI support in .NET requires that your Web site add an assembly reference to the System.Management.dll assembly. Right-click on the Web site in the Solution Explorer and select Add References… to add this assembly into your project. Then select Add New Item… to include an empty XSLT stylesheet named “wmi2ds.xslt” to the Web site.

WmiGridView achieves its databinding at a high level by walking through a compact series of steps (see Figure 2). It extracts WMI instances from the WMI Repository provided by the winmgmt service that runs in the background. Next, WmiGridView takes the XML representation of these instances and transforms them with XSLT into the XML representation of an ADO.NET data set. The transformation maps Classes to DataTables, Instances to DataRows, and Properties to DataColumns. Finally, the application binds the GridView to the extracted and transformed information contained in the ADO.NET DataSet (see Figure 3).

Before delving into the intricacies of mapping between XML formats, you must confront the issue of enumerating threads. WMI Query Language (WQL) offers constructs that you’ll recognize immediately if you’ve spent any time in relational databases using SQL. For example, consider this query:

select * from Win32_Threads
where ProcessHandle = (
   select ProcessId from Win32_Process
   where Name = "aspnet_wp.exe"
)

This selection applies equally well to a relational database modeling an operating system and WMI, and it’s typical of the System.Management code you’ll be writing for WmiGridView. You create a SelectQuery object in .NET to represent each part of this selection, and a ManagementObjectSearcher acts like a data reader that enables you to enumerate through the resulting ManagementObjects. You read WMI properties much as you look up values in a dictionary, where the keys are the property names:

SelectQuery subquery =
   new SelectQuery("Win32_Thread",
   string.Format("ProcessHandle=\"{0}\"",
   procInstance["ProcessId"]));

searcher = new ManagementObjectSearcher(subquery);

ManagementObjectCollection threads = searcher.Get();
   foreach (ManagementObject threadInstance in
      threads) { . . . }
Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home