Terminate a process instantly #190

If at any point you wish to terminate a process instantly then you should take a look at the TerminateProcess function in Windows SDK (Software Developer Kit) help which comes with Delphi.

TerminateProcess function is defined as

BOOL TerminateProcess(
  HANDLE hProcess, // handle to the process
  UINT uExitCode   // exit code for the process 
);

Parameters information according to Delphi Help

hProcess
Identifies the process to terminate.
Windows NT: The handle must have PROCESS_TERMINATE access. For more information, see Process Objects (in Windows SDK Help).
uExitCode
Specifies the exit code for the process and for all threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.

Create a new VCL application, add a button, double-click that button and add the following code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TerminateProcess(GetCurrentProcess, 0);
end;

Your application should terminate instantly.

Author: Dorin Duminica
Contributor: Riccardo Faiella (Topellina)
Added: 2012/07/06
Last updated: 2012/07/06