Vb. NET implementation Super PTM Viewer

Source: Internet
Author: User
Tags integer thread thread class tostring client
First of all, to do an interpretation of the name of the article, p is to represent the process, that is, processes, T is to represent thread, that is, the thread, M is representative module, that is, modules.

There is a performance viewer in Win Nt,win 2000,win XP that we can use to view the programs that are running on the computer, get the main window name associated with the program, and view CPU and memory, virtual page usage. But for the more demanding computer users, these are not enough, especially some DIY enthusiasts, they want to be able to view their computers at any time, to learn more information to know whether their computer should be upgraded, whether the virtual page to make adjustments, whether to change the CPU, or increase the memory.

Because of the above, we have every reason to make a better viewer. Before we write code, we need to know some basic knowledge, namely Proccess,thread,module. Now let's learn the basics of proecss.

1. The concept of PROCCSS

In the shortest possible way, the process is the currently running application. The application that is running includes the exe,dll of both files. The difference between a file and a process is that the file is kept on disk, it is still, the computer does not call the file, and the process is not static, and it is for a reason that the computer requires a certain amount of resources for each process to invoke. These resources include memory, virtual pages, handles, GDI ...

In. NET, we can use the process class, which is located in the System.Diagnostics space, to provide access to processes running on the computer. Using the Process class, you can get a list of the currently running processes, or start a new process. The process class is used to access system processes. After you initialize the process class, you can use it to get information about the currently running process. Such information includes line Cheng, loaded modules (. dll and. exe files), and performance information, such as the amount of memory currently used by the process.

The system process is uniquely identified on the system by its process identifier. As with many Windows resources, processes are identified by their handles, and handles may not be unique on the computer. A handle is a generic term that represents a resource identifier. The operating system retains the process handle, which is accessed through the Handle property of the process component, even if the process has exited. Therefore, you can get management information for the process, such as exitcode (usually, or zero for success, or not 0 error codes) and Exittime. Handles are very valuable resources.

2. The concept of thread

A process consists of one or more threads. In other words, the thread is dividing the process into smaller parts. Each thread has a priority, and a lower-priority thread may be forced to wait when a higher-priority thread completes the task. On a multiprocessor computer, the CPU load is balanced by moving multiple threads to different processors. Each process starts with a thread, called the primary thread. Any thread can create additional threads. All threads in the process share the address space of the process.

In. NET, you can use the System.Diagnostics ProcessThread class to get thread information about the process. Create a new thread with the thread class of system.threading. Note that the thread class cannot be used to access threads of other processes, and he can only be used to create a Thread, you must remember this. If you want to do more with the thread, you can use the ID returned in the System.Diagnostics.ProcessThread class to manipulate the thread by using it as an argument to the API function.

3. The concept of module

A module refers to an executable file or a dynamic-link library (DLL). Each process contains one or more modules. You can use the System.Diagnostics Processmodule class to get information about the module, which has a very useful property entrypointaddress He can return the memory address of the function that runs when the system loads and runs the module. Then use the tools in Visual Studio6.0 to view the address to know what the function is! Then you can see the basic programming ideas of the application by looking at the relevant data based on the function name, which is useful to programmers.

Knowing the basics, you can now program. First create a window, add a TabControl pagination control to the window, and add Label1,label2,listbox1,listbox2,button1 to the first page. Add Label4,label6,listbox3,listbox4 on the second page and add label5,label7,listbox5,listbox6 on the third page. Set the Horizontalscrollbar property of all ListBox controls to True

Then add the following code (vb.net write)

Public Class Form1
Inherits System.Windows.Forms.Form
Public ZJ
Public myprocesses () as Process
Public myprocess as Process
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear ()
myprocesses = Process.getprocesses ()
For each myprocess in myprocesses
LISTBOX1.ITEMS.ADD (Myprocess.processname)
Next
Label1.Text = ""
Label1.Text = "Native Total" & ListBox1.Items.Count & "Processes running"
End Sub

Private Sub Listbox1_doubleclick (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Listbox1.doubleclick
Dim I:dim F as Integer:dim H as Integer
ListBox2.Items.Clear ()
ZJ = Listbox1.selectedindex
Label2.Text = "Process" & ListBox1.Items.Item (ZJ) & "Related Information"
With Listbox2.items
If myprocesses (ZJ). MainWindowTitle = "" Then
. ADD ("No related main window")
Else
. ADD (Myprocesses (ZJ). MainWindowTitle)
End If
. ADD (Myprocesses (ZJ). Basepriority & "Basic priority of process")
. ADD (Myprocesses (ZJ). Handle.tostring & "process native handle")
. ADD (Myprocesses (ZJ). Handlecount & "Number of handles opened by process")
. ADD (Myprocesses (ZJ). Id & Unique identifier for process)
. ADD (Myprocesses (ZJ). MachineName & "The name of the computer on which the process is running")
. ADD (Myprocesses (ZJ). Mainmodule.tostring & "Main module of Process")
. ADD (Myprocesses (ZJ). Mainwindowhandle.tostring & "Handle of Process main window")
'. ADD (Myprocesses (ZJ). Modules & "")
. ADD (Myprocesses (ZJ). Nonpagedsystemmemorysize & "System memory size allocated to paging on this process")
. ADD (Myprocesses (ZJ). Pagedmemorysize & "Paging memory Size")
. ADD (Myprocesses (ZJ). Pagedsystemmemorysize & "Paging system memory Size")
. ADD (Myprocesses (ZJ). Peakpagedmemorysize & "Peak paging memory Size")
. ADD (Myprocesses (ZJ). Peakvirtualmemorysize & "Peak virtual memory Size")
. ADD (Myprocesses (ZJ). PrivateMemorySize & "Dedicated memory Size")
. ADD (Myprocesses (ZJ). StartTime & "process start Time")
. ADD (Myprocesses (ZJ). Totalprocessortime.seconds & "Total Processor Time for process")
. ADD (Myprocesses (ZJ). Userprocessortime.seconds & "User processor Time for process")
. ADD (Myprocesses (ZJ). Virtualmemorysize & "Virtual memory size for processes")
End With
ListBox3.Items.Clear ()
Tabpage1.text = "Process" & ListBox1.Items.Item (ZJ) & "Basic Information"
Tabpage2.text = "Process" & ListBox1.Items.Item (ZJ) & "Related thread Information"
Tabpage3.text = "Process" & ListBox1.Items.Item (ZJ) & "Related module information"
'/////////////////////////////////////////////
f = myprocesses (ZJ). Threads.count
Label4.text = "Process" & ListBox1.Items.Item (ZJ) & "A total of & F &" Related threads, thread ID information is as follows: "
Dim int, PK, M, n
For n = 0 to F-1
LISTBOX3.ITEMS.ADD (Myprocesses (ZJ). Threads (n). ID)
Next
'///////////////////////////////////////

ListBox5.Items.Clear ()
Dim Zha, KL
Zha = myprocesses (ZJ). Modules.count
Label5.text = "with Process" & ListBox1.Items.Item (ZJ) & "A total of & Zha &" A related module, the module location information is as follows: "
For KL = 0 to Zha-1
LISTBOX5.ITEMS.ADD (Myprocesses (ZJ). Modules (KL). FileName)
Next
End Sub

Private Sub Listbox3_doubleclick (ByVal sender as Object, ByVal e as System.EventArgs) Handles Listbox3.doubleclick
ListBox4.Items.Clear ()
Dim F, n
f = myprocesses (ZJ). Threads.count
n = Listbox3.selectedindex
Label6.text = "Threads" & ListBox3.Items.Item (n) & "Related Information"
With Listbox4.items ' thread information related to the process see System.Diagnostics.processThread class. Because the Processes.threads property is an array of thread objects returned and an instance of the thread object array is created by the ProcessThread class, the information for the thread object array is contained in System.Diagnostics. In the ProcessThread class.
. ADD (Myprocesses (ZJ). Threads (n). basepriority)
. ADD (Myprocesses (ZJ). Threads (n). currentpriority)
. ADD (Myprocesses (ZJ). Threads (n). Prioritylevel)
. ADD (Myprocesses (ZJ). Threads (n). Privilegedprocessortime)
. ADD (Myprocesses (ZJ). Threads (n). StartTime)
. ADD (Myprocesses (ZJ). Threads (n). threadstate.tostring)
. ADD (Myprocesses (ZJ). Threads (n). Totalprocessortime.seconds)
. ADD (Myprocesses (ZJ). Threads (n). Userprocessortime.seconds)
. ADD (Myprocesses (ZJ). Threads (n). waitreason.tostring)
End With
End Sub

Private Sub Listbox5_doubleclick (ByVal sender as Object, ByVal e as System.EventArgs) Handles Listbox5.doubleclick
ListBox6.Items.Clear ()
Dim F, N, M
f = myprocesses (ZJ). Modules.count
n = Listbox5.selectedindex
Label7.text = "Module" & ListBox5.Items.Item (n) & "Related Information"
With Listbox6.items ' process-related module information see system.diagnostics. Processmodule class, Because the Processes.modules property is to return an array of modules objects and modules an instance of an array of objects is created by the Processmodule class, the information for the Modules object array is contained in System.Diagnostics. In the Processmodule class.
m = Hex (myprocesses (ZJ). Modules (n). Baseaddress.toint32) ' converted into 16
. ADD (M)
m = Hex (myprocesses (ZJ). Modules (n). ENTRYPOINTADDRESS.TOINT32)
. ADD (M)
. ADD (Myprocesses (ZJ). Modules (n). FileVersionInfo)
. ADD (Myprocesses (ZJ). Modules (n). Modulememorysize)
. ADD (Myprocesses (ZJ). Modules (n). ModuleName)
End With
End Sub

End Class This is workpiece, at the end of the process do a little additional explanation, readers, when you know how to write this program, you may think that if you just write a simple program you may feel meaningless, then how can this program to play his greater significance? Let me tell you. In fact, this code is not deliberately written by me, but a local area network in the remote control part. Scans the processes running on the client, looks at the client's resources, sends the information to the server, and shuts down the processes on the client through the processes class so that managers can manage the client. Another purpose of writing this article is to expect readers to master the System.diagnostics.process,system.diagnostics.processthread,system.diagnostics in. NET more quickly. Processmodule class. Passed in vb.net and Win2000.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.