Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  • Print
  • + Share This
Like this article? We recommend

The Management Model

Before getting into the management data itself, it’s important to talk a little about the underlying model—the so-called management model. The model used for WMI is called the Common Information Model (CIM). This is a standard model that enables managed entities to be modelled in a uniform fashion.

Let’s see how to interact with the model using WMI.

The Microsoft Management Model

Listing 1 illustrates a method that forms part of the C# code used to generate the data in Figure 1.

Listing 1 Network interface data acquisition

public string getNetworkInterfaceDetails()
{
string netInterfaceData = String.Empty;

// Use WMI technology to retrieve the interface details
ManagementObjectSearcher searcher =
 new ManagementObjectSearcher(
 "select * from Win32_PerfRawData_Tcpip_NetworkInterface");

foreach(ManagementObject adapterObject in searcher.Get())
{
netInterfaceData += "Adapter name: " + adapterObject["Name"] + "\r\n";
}
return netInterfaceData; 

The key line in Listing 1 is the one in which I instantiate an object of the ManagementObjectSearcher class. Objects of this class are used to retrieve a collection of managed objects based on a specified query. The query broadly follows the approach taken in the SQL language. Once the query has occurred, it is possible to loop through all of the retrieved managed objects using a foreach loop in conjunction with the searcher.Get() method. In the method in Listing 1, I simply concatenate all entities with the "Name" attribute and then return the complete string.

How do I get the details contained in the route table? It’s very similar to the code in Listing 1 and is illustrated in Listing 2.

Listing 2 IP route table data acquisition

public string getIPRouteTable()
{
string ipRouteData = String.Empty;
    
 // Use WMI technology to retrieve the interface details
 ManagementObjectSearcher searcher =
  new ManagementObjectSearcher(
  "select * from Win32_IP4RouteTable");

foreach(ManagementObject adapterObject in searcher.Get())
{
ipRouteData += "Destination: " + adapterObject["Destination"] + ", " +
   "NextHop: " + adapterObject["NextHop"] + "\r\n";
}
      
return ipRouteData;         
}

As you can see, in Listing 2 the approach is basically identical to Listing 1. It’s useful to compare the two listings with the program output in Figure 1.

  • + Share This
  • 🔖 Save To Your Account