Tag Archives: WMI

Codexpert – 2013 Articles Summary

C++ Language and STL

Microsoft Libraries (Windows API, MFC, ATL, WIC, WMI, etc.)

Miscellaneous

 

Listing Processes – Part 5: Using Windows Management Instrumentation (WMI)

Using Win32_Process WMI class

A previous article shows how to use Win32_PhysicalMedia WMI class to get physical drive info. We can write something similar for getting a list of running processes. All we have to do is to replace the WQL query and get specific properties for Win32_Process. However, to simplify the things, I wrote few C++ wrapper classes over the WMI stuff.
Here is a brief description for each one:

    • CWMIConnection – opens and keeps a connection to WMI namespace.
    • CWMIQuery – executes WQL (SQL for WMI) queries and navigates through results.
    • CWMIWin32_Process – is derived from CWMIQuery and is specialized for Win32_Process.
    • CWMIValue – a class that gets user readable strings from different CIM types.

The implementation details can be found in the attached demo project.
Let’s show now just a usage example, that fills a list-view control with info about running processes.

Demo project

The demo project is a simple MFC dialog-based application that uses WMI wrapper classes to list and get info about running processes.
Download: Listing_Processes__Using_WMI.zip (857)

 

Using WMI - Demo Application

Using WMI – Demo Application

 

Resources

See also

Get Physical Drive Serial Number – Part 2

The previous article shows how to get manufacturer-provided serial number for a physical drive by calling DeviceIoControl function.
Now, let’s see how can it be made by using WMI (Windows Management Instrumentation).

Get serial number by using Win32_PhysicalMedia WMI class

To get the physical drive serial number by using Win32_PhysicalMedia class, follow these steps:

  1. Initialize COM.
  2. Set the default process security level.
  3. Create a connection to WMI namespace.
  4. Set the security levels on WMI connection.
  5. Execute a WQL (WMI Query Language) query to get a list of physical media. Each list element contains a tag as unique identifier (e.g. PHYSICALDRIVE0)  and the manufacturer-provided serial number.
  6. Get each enumerator element until find the desired physical drive. For detailed code, see the complete demo application, below.

Putting all together with some helpful ATL stuff, we can make now a simple console demo application.

See also

Resources

Later edit

We can simplify a little bit the above example by adding WHERE clause in the WQL query.