Click here to Skip to main content
Click here to Skip to main content

Enumerate System Objects using WMI

, 23 Apr 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to query system using WMI interface
Screenshot - WMIQuery1.png

Introduction

WMI is a technology introduced by Microsoft to support the management of operating systems and system objects in an enterprise. WMI allows developers to use a simple, consistent mechanism to query for information or configure settings on computers or enumerate objects on computers across an enterprise. In my project, I had to enumerate the different objects like network adapters, printers, and CPUs on a machine. In the absence of WMI , I might have to learn a lot of APIs and perform a lot of research, atleast one for each object type to be enumerated. With WMI, my life became simple as every object on a machine can be enumerated as a class using WMI.

Explaining more on WMI is beyond the scope of this article. There is no proper documentation available on WMI, so developers are not very much aware of this. This article is designed to introduce enumerating system objects and their characteristics using WMI to developers, which I had difficulty finding. A tool to help browse the information that's available through WMI is available with a test application called WBEMTest.exe that gets shipped with every version of an operating system. This is a very powerful app, since it provides access to almost every DCOM interface exposed by WMI, but it is very difficult for a novice to understand.

WMI works on almost all Microsoft ® platforms. However if WMI is not available in any operating system, one can download "Windows Management Instrumentation (WMI) CORE 1.5 (Windows 95/98/NT 4.0)".

In the .NET Framework, WMI is exposed in the System.Management namespace. One can use this namespace to perform various tasks using WMI.

Getting Started

WMIDemo can be used for getting simple computer information. This contains WMIHelper which exposes several methods which can be used to get specific information of the system area. Basic entity is also included in the library. These can be used for getting and displaying the computer information in any C# project.

  • BIOS - Stores information related to BIOS
  • ComputerSystem - Stores information related to computer system
  • Disk - Stores information related to physical disk
  • HotFixes - Stores information related to the HotFixes installed in the system
  • LogicalDisk - Stores information related to logical disk drives
  • NetworkAdapter - Stores information related to network adapters
  • OS - Stores information related to operating system
  • Printers - Stores information of printers installed
  • Processors - Stores information of processors
  • SoundCard - Stores information related to soundcards installed in the system
  • VideoController - Stores information related to videocards installed in the system

WMIHelper class exposes static methods which return the filled structures for direct use. The class is made static so there is no need for instantiation. Exposed methods are listed below:

  • FillHotFixes - Finds information related to hot fixes and returns object of HotFixes class containing the information
  • FillComputerInformation - Finds information related to computer information and returns object of CompueterSystem class containing the details
  • FillProcessorInformation - Finds information related to processors and returns object of Processors class containing the details
  • FillBiosInformation - Finds information related to BIOS and returns object of BIOS class containing the details
  • FillOSInformation - Finds information related to operating system and returns object of OS class containing the details
  • FillVideoController - Finds information related to Video Controller and returns object of VideoController class containing the details
  • FillSoundCard - - Finds information related to sound cards and returns object of SoundCard class containing the details
  • FillDisks - Finds information related to physical disk drive and returns object of Disk class containing the details
  • FillLogicalDisks - Finds information related to logical disk drives and returns object of LogicalDisk class containing the details.
  • FillPrinters - Finds information related to printers and returns object of Printers class containing the details
  • FillNetworkAdapter - Finds information related to network adapters and returns object of NetworkAdapter class containing the details

Using the Code

Use of this module is very simple. You have to call the required method directly from the class name. No instantiation is required.

 WMIHelper.FillComputerInformation(machineName, userName, password, queryAres)
  • machineName - Name of machine which is to be queried
  • userName - Username of any authenticated user for the machine machineName
  • password - Password for the provided userName
  • queryAres - Area to be queried - this can be found on the DescriptionAttribute of the member in the enum QueryArea
For local machine, local or machineName can be used directly. For local machine, do not specify username and password. For domain user, username will be DOMAIN\USERNAME.

Brief Inside Code

This module uses ManagementClass for retrieving information which is part of managed .NET and can be found under the System.Management namespace. You will need to add a reference to System.Management.dll.

Screenshot - WMIQuery2.png

You will need to create a connection by creating an instance of ConnectionOptions and declare the scope using ManagementScope. Specify the query path using the ManagementPath instance.

path = @"\\" + machineName + @"\root\cimv2";
if (userName != string.Empty && password != string.Empty)
{
    ConnectionOptions options = new ConnectionOptions();
    options.Username = userName;
    options.Password = password;
    mScope = new ManagementScope();
    mScope.Options = options;
    mScope.Path = new ManagementPath(path);
}

You are done. Now instantiate ManagementClass, assign scope and run the query managementClass.GetInstances().

ManagementClass managementClass = new ManagementClass(path + ":" + queryArea);
if (mScope != null)
{
    managementClass.Scope = mScope;
}
ManagementObjectCollection instances = managementClass.GetInstances();
if (instances == null)
{
    return null;
}
foreach (ManagementBaseObject instance in instances)
{
    //Fill information
}

References

History

23rd April, 2007: This is the first release of the code.

Modifications and feature enhancements will be done on request. I will request users to email me directly for any suggestions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manish Ranjan Kumar
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralWMI query doesnot return record for user having less previleges!! Pinmemberdeep@Pune2-Mar-09 20:50 
GeneralGreat Article.. Really Useful Pinmemberwabreyka7-Jan-09 17:00 
This is a really great article and very useful code..
This can be further developed to be something like everest...
It's almost the everest software.. only the the computers in the network is manually inputted..
Smile | :) Smile | :) Smile | :) Smile | :) Smile | :)
Questionservice pack information Pinmemberyadhvi15-Sep-08 3:19 
Generalempty password for session user Pinmemberjamalhaider16-Jun-08 4:38 
QuestionAccessing Access-Point Data via WMI Pinmembersam[CH][HSR]5-Jun-07 6:27 
QuestionCode Requirements PinmemberNhilesh B20-May-07 5:09 
GeneralGood article, thanks Pinmemberersincelik4-May-07 4:29 
JokePassword PinmemberDeepak Raj25-Apr-07 19:45 
AnswerRe: Password PinmemberManish Ranjan Kumar25-Apr-07 21:10 
JokeRe: Password PinmemberDeepak Raj25-Apr-07 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Apr 2007
Article Copyright 2007 by Manish Ranjan Kumar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid