Delphi Traversal Process-win32api

Source: Internet
Author: User

This blog's Delphi code uses the version is delphixe10.x

1.1. Enumerating processes

Get the specified process ID from the process name, the code is verbose, no longer repeat

UnitUuitls;InterfaceusesTLHELP32, Winapi.windows;functionGetpidbyproname (proname:string):DWORD;ImplementationfunctionGetpidbyproname (proname:string):DWORD;varHwd:thandle;// handlerocessentry:tprocessentry32;//variable of struct typeFoundBoolean;//Returns a Boolean value (used to determine if process information is found)ProcessIDDWORD;//Store the found process IDPname:string;//Store the found process name end;beginResult: =0;//Get process snapshot handleHWD: = CreateToolhelp32Snapshot (Th32cs_snapprocess,0);//Assign a value to the first parameter of the TPROCESSENTRY32 structure (it can also be understood to initialize the first parameter of the structure)Rocessentry.dwsize: = sizeof (rocessentry);//Use the Process32First function to get information about the first processFound: = Process32First (HWD, rocessentry);//If the Process32First function succeeds, that is, the first process in the list of processes is found, the loop starts   whileFound =true  Do  begin  //Get the next process informationFound: = Process32Next (HWD, rocessentry); Pname: = Rocessentry.szexefile;ifPname = Proname Then    begin      //To display the found processResult: = Rocessentry.th32processid; Break    End;End;End;End.
The 1.2 enumeration process will meet the conditions of the process PID storage container

There is a problem here, originally wanted to use the array as the return value, but did not succeed, had to use the generic tlist, if a friend can handle it greatly appreciated

UnitUbasetools;InterfaceusesGenerics.collections, PSAPI, variants, Winapi.windows, Winapi.messages, TLHelp32, Vcl.stdctrls, System.sysutils, Vcl.dialogs;{*------------------------------------------------------------------------------Enumerate game processes, get all the PID@param proname@return-------------------------------------------------------------------------------}functionListpids (proname:string): tlist<Cardinal>;functionStrtodword (Value:string):DWORD;//Dwordtostr (): Converts a DWORD to a 4 byte stringfunctionDwordtostr (Value:DWORD):string;ImplementationfunctionListpids (proname:string): tlist<Cardinal>;varContinueloop:bool;//Whether to continue the loopFsnapshothandle:thandle;//Process Snapshot handlefprocessentry32:tprocessentry32;//Process entry structure informationPIDsstring; Pid:Integer;  Hprocess:thandle; Processfullpathname:string; BufArray[0.. MAX_PATH] of Char; BUF1:Array[0.. MAX_PATH] of Char;varlist:tlist<Cardinal>;{defines a generic TList class, which specifies the string} to use for thebeginList: = tlist<Cardinal.  Create; Fsnapshothandle: = CreateToolhelp32Snapshot (Th32cs_snapprocess,0);//createtoolhelp32snapshot function to get process snapshotFprocessentry32.dwsize: = Sizeof (FPROCESSENTRY32);//InitializeContinueloop: = Process32First (Fsnapshothandle, FProcessEntry32);//process32first Get information about the first process in a system snapshot   whileContinueloop Do  begin   //Process IDPID: = Fprocessentry32.th32processid; Hprocess: = OpenProcess (process_query_informationorProcess_vm_read,FALSE, PID);ifHprocess <>0  Then    begin      ifStrpas (fprocessentry32.szexefile) = Proname Then      beginList.add (FPROCESSENTRY32.TH32PROCESSID);End;End; Continueloop: = Process32Next (Fsnapshothandle, FProcessEntry32);End;  CloseHandle (Fsnapshothandle); Result: = List;End;

Delphi Traversal Process-win32api

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.