I have a process ID and need to programmatically complete the association process with Delphi 5

Can someone help me with a coding example to close a related process when I have a process id. I will use Delphi 5 to perform this operation programmatically on a Windows 2003 server.

+4
source share
3 answers

If you have a process ID and you want this process to end, you can use this code:

function TerminateProcessByID(ProcessID: Cardinal): Boolean; var hProcess : THandle; begin Result := False; hProcess := OpenProcess(PROCESS_TERMINATE,False,ProcessID); if hProcess > 0 then try Result := Win32Check(Windows.TerminateProcess(hProcess,0)); finally CloseHandle(hProcess); end; end; 
+11
source

Use EnumWindows() and GetWindowProcessThreadId() to find all the windows that belong to the process and then send them to WM_CLOSE and / or WM_QUIT .

+4
source

Together with WM_CLOSE and WM_QUIT you can make it really elegant and just start the second instance of the application with STOP as a parameter. Like this:

In the main part of the project ...

 if ((ParamCount >= 1) and (UpperCase(paramstr(1)) = 'STOP')) then // send the WM_CLOSE, etc.. 

When the application starts and sees that it has the "STOP" parameter, then track down the first instance and kill it. Then close the second instance without creating the main form, etc. Thus, you do not need to write / deploy the second program, only to kill the first.

0
source

Source: https://habr.com/ru/post/1305638/


All Articles