Working with Windows Management Instrumentation

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Windows Management Instrumentation (WMI) is a COM based technology used to get system-related information. Such information may include hardware resources like, the motherboard, memory, disk, etc. You can use WMI to communicate with the Windows OS, the hardware devices, running processes, COM+ components, etc. In this article we will discuss the basics of WMI and then explore how we can make use of WMI in our applications.

Windows Management Instrumentation (WMI)

Windows Management Instrumentation (WMI) provides a robust, low-level communication with the Windows Operating System from the managed environment. You can use WMI to communicate with the system’s hardware and still leverage the benefit of the CLR's managed environment, automated memory management, garbage collection, etc.  Actually, Windows Management Instrumentation (WMI) provides a set of extensions to the Windows Driver Model. These extensions can be accessed from the managed environment.

WMI includes the following providers:

Win32 Provider

WDM Provider

Event Log Provider

Registry Provider

SNMP Provider

View Provider

Performance Counter Provider

Active Directory Provider

Windows Installer Provider

The WMI Query Language

The WMI Query Language, a subset of the American National Standard Query Language (ANSI SQL), is much the same as any other standard query language and is used to retrieve system management information.

The following query retrieves the information of all the logical disk drives in your system.

Select * from Win32_LogicalDisk

To retrieve processor details in your computer system, you’ll need to write the following query:

SELECT * FROM Win32_Processor

Note that all WMI classes follow a language format known as the Managed Object Format (MOF).

To use WMI in your applications, you’ll need to include the following namespaces:

System.Diagnostics

System.Management

System.Management.Instrumentation

Programming WMI

In this section we will explore WMI – we will use WMI to retrieve CPU ID, MAC ID and Volume Serial Number of a drive in your system. The CPU ID identifies the CPU of the computer's central processor. The information includes model number, processor family, cache size, clock speed, and also the manufacturer codename. The Media Access Control or MAC address is the Network Interface Card address. It is composed of 12 hexadecimal characters (0-9, A-F) and is defined as a unique identifier used to identify most network equipment. Note that every network device has a unique MAC address. The Volume Serial Number of a drive is a running serial number that is generated for every format of the drive.

The following code snippet illustrates how you can display the drive information of your system:

ManagementObjectSearcher managementObjectSearcherQuery = new ManagementObjectSearcher
    ("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection managementObjectCollectionQueryCollection = managementObjectSearcherQuery.Get(); 
foreach ( ManagementObject managementObject in managementObjectCollectionQueryCollection) 
{ 
    switch (int.Parse( managementObject["DriveType"].ToString())) 
    { 
        case Removable: //Removable drives 
            break; 
        case LocalDisk: //Local drives 
            break; 
        case CD: //CD drives 
            break; 
        case Network: //Network drives 
            break; 
        default:
            break; 
    } 
      Console.WriteLine("Drive: " + managementObject["Name"].ToString()); 
}

To retrieve the volume serial number of any drive in your system you can write the following code:

                                public string GetVolumeSerial(string strDriveLetter)
           {
                if( strDriveLetter=="" || strDriveLetter==null) strDriveLetter="C";
                ManagementObject disk = 
                     new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter +":\"");
                disk.Get();
                return disk["VolumeSerialNumber"].ToString();
           }

To retrieve the MAC address of the first network card in your system, you can write the following code;

                                public string GetMACAddress()
           {
                ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
                string MACAddress=String.Empty;
                foreach(ManagementObject managementObject in managementObjectCollection)
                {
                     if(MACAddress==String.Empty)  
                     {
                           if((bool)managementObject["IPEnabled"] == true) 
                           MACAddress= managementObject["MacAddress"].ToString() ;
                     }
                     managementObject.Dispose();
                }
                MACAddress = MACAddress.Replace(":","");
                return MACAddress;
           }

To retrieve the processor id of the first processor, you can write the following code:

                                public string GetProcessorID()
           {
                string cpuInfo =  String.Empty;
                ManagementClass managementClass = new ManagementClass("Win32_Processor");
                ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
                foreach(ManagementObject managementObject in managementObjectCollection)
                {
                     if(cpuInfo.Equals(String.Empty))
                     {
                         cpuInfo = managementObject.Properties["ProcessorId"].Value.ToString();                         
                     }
                }
                return cpuInfo;
           }
 

References

http://aspalliance.com/629_Introducing_Windows_Management_Instrumentation_WMI#Page1

http://www.codeproject.com/Articles/54064/Working-With-Windows-Management-Instrumentation-WM

http://msdn2.microsoft.com/en-us/library/ms257361(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms811553.aspx

Conclusion

WMI, an implementation of the WBEM standards, is a key component of Microsoft Windows management services and provides a framework for accessing system level information from the managed environment. This article presented an overview of WMI, WMI Query Language and an idea on how we can leverage WMI in our applications. Happy reading!



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date