Microsoft KB Archive/831490

= How to enumerate the running processes of an application by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 831490

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SUMMARY
''A process is the running instance of an application. A thread is the basic unit of a process. To retrieve information that corresponds to processes that are running on a local computer or on a remote computer, use the following methods:''
 * The GetProcesses method retrieves information about the processes that are running on a local computer. This method creates an array of process components and then associates the processes that are running with these process components. The GetProcesses method never returns an empty array.
 * The GetProcesses(String) method retrieves information about the processes that are running on a local computer or on a remote computer. You must pass the computer name or the computer Internet Protocol (IP) address to the GetProcesses(String) method to retrieve the processes that are running on a remote computer. Also, you must have administrative rights on the remote computer to retrieve the processes that are running on that computer.



IN THIS TASK
 * INTRODUCTION
 * Requirements
 * Process definition
 * The GetProcesses method
 * Step-by-step sample
 * REFERENCES



INTRODUCTION
This step-by-step article describes how to enumerate running instances of applications by using Microsoft Visual Basic .NET.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 programming

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003.
 * Microsoft Visual Studio .NET 2002, Microsoft Visual Studio .NET 2003, or Microsoft Visual Studio 2005.

back to the top

Process definition
A process is the running instance of an application. A thread is the basic unit of a process. The operating system allocates processor time to a thread so that the thread can run any part of the process code. A process can create one or more threads to run any part of the code that is associated with the process.

You can use a process component to start, to stop, to control, and to monitor a process. You can use a process component to obtain the list of processes that are running on a computer. After you initialize a process component, you can obtain information about the set of threads, about the loaded modules, and about the performance for all the running processes. You can also obtain information about processes that are running on a remote computer.

back to the top

The GetProcesses method
You can use the GetProcesses method to obtain information about the processes that are running on local computers and on remote computers. This method creates an array of new process components and then associates these components with the existing process resources. The two overloaded versions of this method are as follows:
 * The GetProcesses method

The GetProcesses method creates an array of new process components and associates the components with all the process resources that are on the local computer. The process resources must already exist on the local computer. This method does not create process resources. Instead, this method associates existing resources with application-generated process components. Because the operating system itself is constantly running background processes, this array is never empty.
 * The GetProcesses(String) method

The GetProcesses(String) method accepts a string as a parameter. Typically, the string is the computer name or the IP address of a remote computer. However, you can pass a period (.) to this method to retrieve the list of processes that are running on your local computer. To retrieve information about the processes that are running on a remote computer, you must have administrative rights to map a network drive to a folder on the remote computer.

back to the top

Step-by-step sample
Note You must have administrative rights on the remote computer to follow these steps.  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project.

The New Project dialog box appears. Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Windows Application. In the Name box, type ProcessInfo, and then click OK.

By default, the Form1.vb file is created.  Replace the existing code in the Form1.vb file with the following code: Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'The Windows Form Designer requires this call. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If     End If      MyBase.Dispose(disposing) End Sub

'Required by the Windows Form Designer. Private components As System.ComponentModel.IContainer

'NOTE: The Windows Form Designer requires the following procedure. 'It can be modified by using the Windows Form Designer. 'Do not modify the procedure by using the Code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents process1 As System.Windows.Forms.ColumnHeader Friend WithEvents Process2 As System.Windows.Forms.ColumnHeader Friend WithEvents PageMemorySize As System.Windows.Forms.ColumnHeader

<System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.TextBox1 = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.ListView1 = New System.Windows.Forms.ListView Me.Button2 = New System.Windows.Forms.Button Me.SuspendLayout '     'Button1 '     Me.Button1.Location = New System.Drawing.Point(8, 368) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(72, 32) Me.Button1.TabIndex = 1 Me.Button1.Text = &quot;Get Processes&quot; '     'TextBox1 '     Me.TextBox1.Location = New System.Drawing.Point(272, 368) Me.TextBox1.Name = &quot;TextBox1&quot; Me.TextBox1.Size = New System.Drawing.Size(160, 20) Me.TextBox1.TabIndex = 5 Me.TextBox1.Text = &quot;&quot; '     'Label1 '     Me.Label1.Location = New System.Drawing.Point(160, 376) Me.Label1.Name = &quot;Label1&quot; Me.Label1.Size = New System.Drawing.Size(112, 23) Me.Label1.TabIndex = 7 Me.Label1.Text = &quot;Remote Computer IP&quot; '     'ListView1 '     Me.ListView1.ImeMode = System.Windows.Forms.ImeMode.On      Me.ListView1.Location = New System.Drawing.Point(32, 32) Me.ListView1.Name = &quot;ListView1&quot; Me.ListView1.Size = New System.Drawing.Size(360, 288) Me.ListView1.TabIndex = 8 Me.ListView1.View = System.Windows.Forms.View.Details Me.ListView1.Columns.Add(&quot;Process&quot;, 100, HorizontalAlignment.Left) Me.ListView1.Columns.Add(&quot;ProcessID&quot;, 150, HorizontalAlignment.Left) Me.ListView1.Columns.Add(&quot;Page Memory Size&quot;, 110, HorizontalAlignment.Left)

'     'Button2 '     Me.Button2.Location = New System.Drawing.Point(88, 368) Me.Button2.Name = &quot;Button2&quot; Me.Button2.Size = New System.Drawing.Size(64, 32) Me.Button2.TabIndex = 9 Me.Button2.Text = &quot;Clear&quot; '     'Form1 '     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(440, 422) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.ListView1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Button1) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub

Dim Processes As Process
 * 1) End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim myprocess As Process Dim compName As String = TextBox1.Text If compName = &quot;.&quot; Then Processes = process.GetProcesses Else Processes = process.GetProcesses(compName) End If        Dim proclength As Integer For proclength = 0 To Processes.Length - 1 myprocess = Processes(proclength) Dim process(2) As String process(0) = myprocess.ProcessName process(1) = myprocess.Id.ToString process(2) = myprocess.PagedMemorySize.ToString Dim process_Listview As ListViewItem = New ListViewItem(process) ListView1.Items.Add(process_Listview) Next

Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ListView1.Items.Clear End Sub End Class Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

</li> On the Build menu, click Build Solution.</li> Switch to Visual Studio .NET or Visual Studio 2005, and then press F5 to run your application.

The Form1 Windows form appears.</li> Click Get Processes.

Notice the list of processes that are running on your local computer.</li> Click Clear.</li> In the Remote Computer IP box, type the IP address of the remote computer, and then click Get Processes. You will get the processes that are running on the remote computer.</li></ol>

back to the top

<div class="references_section">