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