//Disclaimer:{Returns a handle to a process}OpenProcess (Dwdesiredaccess:dword;{access Options}Binherithandle:bool;{can inherit; True indicates that a new process can be created with CreateProcess inheritance handle}Dwprocessid:dword{Specify process ID}): Thandle;{The process handle is returned successfully; the failure returns 0}//dwdesiredaccess selectable values:Process_terminate =$0001;{allow terminateprocess to use process handles to close processes}Process_create_thread =$0002;{Allow CreateRemoteThread to create threads using process handles}Process_vm_operation =$0008;{Allow Virtualprotectex to change the virtual memory of a process using a process handle}Process_vm_read =$0010;{allow readprocessmemory to read data from the process's virtual memory using a process handle}Process_vm_write =$0020;{Allow WriteProcessMemory to write data to the process's virtual memory using a process handle}Process_dup_handle =$0040;{allow DuplicateHandle to copy a process handle as a source handle or a target handle}Process_create_process =$0080;{default value}Process_set_quota =$0100;{allow setprocessworkingsetsize to use process handles to set the upper value of virtual memory}Process_set_information =$0200;{allow Setpriorityclass to use process handle to set process priority}Process_query_information =$0400;{Allow getexitcodeprocess or getpriorityclass to read process information through the process handle}SYNCHRONIZE =$00100000;{Allow any waiting function to use the process handle}Process_all_access = (standard_rights_requiredorSYNCHRONIZEor $FFF);{Allow all permissions}
{Gets the exit code for the specified process}GetExitCodeProcess (Hprocess:thandle;{Process Handle}varLpexitcode:dword{receive exit code}): BOOL;{}
{Force end (other) process}TerminateProcess (Hprocess:thandle;{Process Handle}Uexitcode:uint{exit code}): BOOL;{}//TIP: Closing other programs should typically send a WM_CLOSE message to its main window, no longer use this because it cannot close its associated DLL.
//Example (method of forcibly closing OICQ):UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTForm1 =class(Tform) Button1:tbutton;procedureButton1Click (Sender:tobject);End;varForm1:tform1;Implementation{$R *.DFM}procedureTform1.button1click (Sender:tobject);varid:cardinal; Wh:hwnd; Ph:thandle; Exitcode:dword;beginWH: = FindWindow (' #32770 ',Nil); GetWindowThreadProcessId (wh, id); Ph: = OpenProcess (Process_terminate, False, id); GetExitCodeProcess (ph, ExitCode); TerminateProcess (ph, ExitCode);End;End.
OpenProcess, getexitcodeprocess, terminateprocess