|
C++
Tapping
processes using WMI - I
In the last
article we saw how to list the processes currently running on the local machine.
In this article we will see a similar application that displays the processes
currently running on the machine, but with additional functionality. We will
display additional information about each process and also extend the program
to view the processes running on a remote machine. The application will also
allow us to terminate the running process.
Create a WinForm application and place controls on it
as shown below:
Change the names of controls as shown in the following table.
|
Control
|
Name
|
| ListView |
m_plist
|
| Machine Name TextBox |
m_mname
|
| Username TextBox |
m_uname
|
| Password TextBox |
m_password
|
| Refresh Button |
m_brefresh
|
| Terminate Process Button |
m_bterminate
|
When the application is executed, it displays the list of processes and their
information of the local machine. To view the processes of remote machine, the
user should enter the machine name, username and password and click the Refresh
button.
To terminate the process, the user should select the process from list view
control and click the Terminate Process button.
Let us first add code to the constructor of the Form1 class after the call to
the InitializeComponent( ) method to display processes of the current machine.
ManagementClass mc = new ManagementClass ( @root\cimv2:Win32_Process
) ;
ManagementObjectCollection mobjects = mc.GetInstances( ) ;
addprocesses ( mobjects ) ;
The object collection returned by the GetInstances( ) method is passed to a
user-defined function addprocesses( ). We have separated code to add the processes
in a function, because we need to execute the same code again when the user
clicks the Refresh button.
The addprocesses( ) function is given below:
public void addprocesses ( ManagementObjectCollection moc
)
{
m_plist.Items.Clear(
) ;
string[ ] items = new
string [ 5 ] ;
foreach ( ManagementObject
mo in moc )
{
items [ 0 ] = mo [ Caption ].
ToString( ) ;
items [ 1 ] = mo [ ProcessId ].
ToString( ) ;
int vs = Int32.Parse ( mo [ WorkingSetSize ].ToString( ) ) ;
vs = vs / 1024 ;
items [ 2 ] = vs.ToString( ) + Kb ;
if
( mo [ ExecutablePath ] == null )
items [ 3 ] = NA ;
else
items
[ 3 ] = mo
[ ExecutablePath ].ToString( ) ;
items [ 4 ] = mo [ Priority ].ToString( ) ;
ListViewItem lvitems = new
ListViewItem ( items ) ;
m_plist.Items.Add ( lvitems ) ;
}
}
In this function,
at first, we have removed all the previously added list items from the control
by calling the Clear( ) method. Next, we have enumerated the objects from the
collection passed to this function in a foreach loop. Each object in the collection
represents a process. We will display the process name, process ID, memory the
process is using, path of the EXE and priority of process in the list control.
To obtain this information of the process, we have used various properties of
the Win32_Process class. They are given below:
- Caption: It returns the name of the process.
- ProcessId: It returns the ID of the process.
- WorkingSetSize: It returns the amount of memory (in bytes) the process
requires to run efficiently.
- ExecutablePath: It returns the path of the process.
- Priority: It returns the priority of the process.
To display the values returned by these properties, firstly we have created
an array of five strings. Since the properties return value of type object we
have converted them to strings.
The WorkingSetSize property returns the memory size in bytes. We have converted
bytes into kilobytes before converting them to string.
After populating the string array, we have created an object of the ListViewItem
and passed the array to its constructor. The 0th element in the array will be
treated as new list item, and rest of the elements as its sub-items. Then we
have called the Add( ) method to add the new list item to the list control.
So, when we execute the application, it looks as shown in the following figure.
We can now enter the machine name, user name and password
and click the Refresh button to view the processes of
that machine. We will see the Refresh button handler
next time.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanetkar@dcubesoft.com |
|