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



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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.

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.
  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

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

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

    By default, the Form1.vb file is created.
  6. Replace the existing code in the Form1.vb file with the following code:
    Option Strict On
    Public Class Form1
       Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
       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 = "Button1"
          Me.Button1.Size = New System.Drawing.Size(72, 32)
          Me.Button1.TabIndex = 1
          Me.Button1.Text = "Get Processes"
          '
          'TextBox1
          '
          Me.TextBox1.Location = New System.Drawing.Point(272, 368)
          Me.TextBox1.Name = "TextBox1"
          Me.TextBox1.Size = New System.Drawing.Size(160, 20)
          Me.TextBox1.TabIndex = 5
          Me.TextBox1.Text = ""
          '
          'Label1
          '
          Me.Label1.Location = New System.Drawing.Point(160, 376)
          Me.Label1.Name = "Label1"
          Me.Label1.Size = New System.Drawing.Size(112, 23)
          Me.Label1.TabIndex = 7
          Me.Label1.Text = "Remote Computer IP"
          '
          'ListView1
          '
          Me.ListView1.ImeMode = System.Windows.Forms.ImeMode.On
          Me.ListView1.Location = New System.Drawing.Point(32, 32)
          Me.ListView1.Name = "ListView1"
          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("Process", 100, HorizontalAlignment.Left)
          Me.ListView1.Columns.Add("ProcessID", 150, HorizontalAlignment.Left)
          Me.ListView1.Columns.Add("Page Memory Size", 110, HorizontalAlignment.Left)
    
    
          '
          'Button2
          '
          Me.Button2.Location = New System.Drawing.Point(88, 368)
          Me.Button2.Name = "Button2"
          Me.Button2.Size = New System.Drawing.Size(64, 32)
          Me.Button2.TabIndex = 9
          Me.Button2.Text = "Clear"
          '
          '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 = "Form1"
          Me.Text = "Form1"
          Me.ResumeLayout(False)
    
       End Sub
    
    #End Region
       Dim Processes As Process()
    
       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 = "." 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: For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
  7. On the Build menu, click Build Solution.
  8. Switch to Visual Studio .NET or Visual Studio 2005, and then press F5 to run your application.

    The Form1 Windows form appears.
  9. Click Get Processes.

    Notice the list of processes that are running on your local computer.
  10. Click Clear.
  11. 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.
back to the top

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbProgramming kbSample kbAppDev kbHOWTOmaster kbcode KB831490 kbAudDeveloper