Get a list of processes or desired processes in Delphi (enumerate processes, traverse processes)

Source: Internet
Author: User

  1. A common programming task is to enumerate all running "applications". The Windows Task Manager is a good example. It lists "applications" in two ways. The first tab of Task Manager lists all the application windows on the desktop. The second tab lists all the processes in the system. This article provides detailed information on how to perform these tasks.
  2. Enumerate top-level windows If you compare the enumeration process to the top-level window on the enumeration desktop, it might be easier to enumerate the top-level windows. To enumerate the top-level windows, use the EnumWindows () function. Do not use GetWindow () to create your own window list, as it may be affected by the z-axis order change and the loss of window interference.
  3. EnumWindows () takes a pointer to a callback function and a user-defined LPARAM value as its argument. It calls the callback function for each window (or top-level window) on the desktop, respectively. The callback function can then perform some processing on the window handle, such as adding it to a list. This method is guaranteed to be unaffected by items such as changes in the z-axis order of the window. After you get the window handle, you can get its caption by calling GetWindowText ().
  4. Enumerating processes
  5. Creating a list of processes in the system is a little more complicated than enumerating windows. This is primarily because the API functions that perform this operation vary depending on the WIN32 operating system that is used. Functions in the TOOLHELP32 API Library can be used in Windows 95, Windows 98, Windows Millennium Edition, Windows 2000, and Windows XP. However, in Windows NT, you must use the functions in the Psapi API library (the library is included in the Platform SDK).   This article discusses both techniques and provides a sample wrapper function called EnumProcs () that can be run in the Win32 operating system.
  6. Enumerating processes using the TOOLHELP32 library
  7. The ToolHelp32 method is discussed first. the TOOLHELP32 function in KERNEL32. dll is a standard API function.  Note that these APIs are not available in Windows NT 4.0.
  8. You can use the various functions provided by TOOLHELP32 to enumerate processes and threads in the system and to obtain memory and module information. However, when enumerating a process, only the following three functions are required: CreateToolhelp32Snapshot (), Process32First (), and Process32Next ().
  9. The first step in using the TOOLHELP32 function is to create a "snapshot" of the system information. You can use the CreateToolhelp32Snapshot () function to complete this step. This function allows you to select the type of information that is stored in the snapshot. If you need process information, be sure to include the th32cs_snapprocess flag. The CreateToolhelp32Snapshot () function returns a handle that must be passed to CloseHandle () after the handle has been used.
  10. Next, in order to retrieve the list of processes in the snapshot, you can call Process32First once and then repeatedly call multiple Process32Next. Do this until one of the functions returns FALSE. This will get the process from the snapshot process list one by one. Both of these functions take a "snapshot" handle and a pointer to the PROCESSENTRY32 structure as its argument.
  11. After calling Process32First or Process32Next, the PROCESSENTRY32 structure will contain useful information about a process in the system. The process ID is in the TH32PROCESSID member of the structure. It can be passed to OpenProcess () to get a handle to the process. The executable file (process name) and path of the process are stored in the Szexefile member of the structure. You can also find other useful information in this structure. (Szexefile refers to the process name).
  12. Note: Remember to set the dwsize member of the PROCESSENTRY32 struct to sizeof (PROCESSENTRY32) before calling Process32First (). *************************************************************************************************************** ********************
  13. Example:
  14. Procedure Tform1.button1click (Sender:tobject); Add a buttin1 button to the program and a memo1 display box  var  jubing:hwnd;//handle  fprocessentry32:tprocessentry32;//variable  of struct type Zhenjia:boolean;   Returns a Boolean value (used to determine if process information is found)  Processid:dword;//store found process ID  mingcheng:string;//store found process name end;      Begin  Jubing: = CreateToolhelp32Snapshot (th32cs_snapprocess, 0);   Get process Snapshot handle     fprocessentry32.dwsize: = sizeof (FPROCESSENTRY32);// Assign a value to the first parameter of the TPROCESSENTRY32 structure (also understood to initialize the first parameter of the structure)      Zhenjia: = Process32First (JUBING,FPROCESSENTRY32);//Use The Process32First function obtains the information for the first process      while Zhenjia = true does//if the Process32First function succeeds, that is, the first process in the process list is found. Start loop begin     Zhenjia: = Process32Next (JUBING,FPROCESSENTRY32);//Get the next process information      Mingcheng: = fprocessentry32.szexefile;// Get the name of a process if      Mingcheng = ' svchost.exe ' then//if the process name equals this string self      . MEMO1.LINES.ADD (Mingcheng); To show the process of finding it.  

      

Get a list of processes or desired processes in Delphi (enumerate processes, traverse processes)

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.