Symptoms
A process is the running instance of an application. A thread is the basicunit 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 aboutthe processes that are running on a local computer. This method creates an array of process components and then associates the processesthat 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 theremote computer to retrieve the processes that are running on that computer.
Resolution
This step-by-step article describes how to enumerate running instances of applications by using Microsoft Visual Basic .NET.
RequirementsThis 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.
Process definitionA 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 thethread 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 aprocess component to obtain the list of processes that are running on a computer. After you initialize a process component, you can obtain informationabout the set of threads, about the loaded modules, and about the performance forall the running processes. You can also obtain information about processes that are running on a remote computer.
The GetProcesses methodYou 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.
Step-by-step sampleNote 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, clickVisual 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 OnPublic Class Form1Inherits 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 ThenIf Not (components Is Nothing) Thencomponents.Dispose()End IfEnd IfMyBase.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.ButtonFriend WithEvents TextBox1 As System.Windows.Forms.TextBoxFriend WithEvents Label1 As System.Windows.Forms.LabelFriend WithEvents ListView1 As System.Windows.Forms.ListViewFriend WithEvents Button2 As System.Windows.Forms.ButtonFriend WithEvents process1 As System.Windows.Forms.ColumnHeaderFriend WithEvents Process2 As System.Windows.Forms.ColumnHeaderFriend 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 = 1Me.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 = 5Me.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 = 7Me.Label1.Text = “Remote Computer IP””ListView1′Me.ListView1.ImeMode = System.Windows.Forms.ImeMode.OnMe.ListView1.Location = New System.Drawing.Point(32, 32)Me.ListView1.Name = “ListView1″Me.ListView1.Size = New System.Drawing.Size(360, 288)Me.ListView1.TabIndex = 8Me.ListView1.View = System.Windows.Forms.View.DetailsMe.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 = 9Me.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 RegionDim Processes As Process()Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickTryDim myprocess As ProcessDim compName As String = TextBox1.TextIf compName = “.” ThenProcesses = process.GetProcesses()ElseProcesses = process.GetProcesses(compName)End IfDim proclength As IntegerFor proclength = 0 To Processes.Length – 1myprocess = Processes(proclength)Dim process(2) As Stringprocess(0) = myprocess.ProcessNameprocess(1) = myprocess.Id.ToString()process(2) = myprocess.PagedMemorySize.ToString()Dim process_Listview As ListViewItem = New ListViewItem(process)ListView1.Items.Add(process_Listview)NextCatch ex As ExceptionMessageBox.Show(ex.Message())End TryEnd SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickListView1.Items.Clear()End SubEnd ClassNote 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(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(http://msdn2.microsoft.com/en-us/library/ms171843.aspx)On the Build menu, click Build Solution.Switch to Visual Studio .NET or Visual Studio 2005, and then press F5 to run your application.
The Form1 Windows form appears.Click Get Processes.
Notice the list of processes that are running on your local computer.Click Clear.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.