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.

void CDemoDlg::_FillProcessesList()
    // clear listview control

        // Open WMI connection
        CWMIConnection wmiConnection;

        // Query Win32_Process
        CWMIWin32_Process wmiQuery(wmiConnection);

        // Fill the list
        int nItem = 0;
            m_listProcesses.InsertItem(nItem, NULL);

            m_listProcesses.SetItemText(nItem, ePID, WMI_GETPROPERTYSTR(wmiQuery, Handle));
            m_listProcesses.SetItemText(nItem, eSessionID, WMI_GETPROPERTYSTR(wmiQuery, SessionId));
            m_listProcesses.SetItemText(nItem, eImageName, WMI_GETPROPERTYSTR(wmiQuery, Caption));
            m_listProcesses.SetItemText(nItem, eCommandLine, WMI_GETPROPERTYSTR(wmiQuery, CommandLine));
            // ...

            // NOTE: This is just for demo purpose and can be completed. 
            // For a compelte list of Win32_Process properties, see MSDN documentation.
    catch(COleException* e)

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: (2000 downloads)


Using WMI - Demo Application
Using WMI – Demo Application



See also

Leave a Comment