Example Code

function ExitProcessEx(const hProcess: THandle; uExitCode: DWORD): BOOL; stdcall; { ExitProcessEx is a safer solution in terminating a process on Windows. The TerminateProcess API violently tears down a process and its address space and consequently this does not allow loaded modules such as DLLs to perform any cleanup code, usually handled within the DLL's DLL_PROCESS_DETACH event, since it's never sent. Disk buffers aren't flushed, global resources or global memory may also be unfreed or in an unknown state. ExitProcessEx is an extension to the ExitProcess Win32 API that can be used on a remote process, which is the proper route to cleanly ending a process. Only if a process can not be terminated cleanly will ExitProcessEx use TerminateProcess internally. } procedure TForm1.FormCreate(Sender: TObject); var hProcess: THandle; dwExitCode: DWORD; begin hProcess := GetCurrentProcess(); dwExitCode := 123; if ExitProcessEx(hProcess, dwExitCode) then MessageBox(0, 'Process successfully terminated', 'ExitProcessEx', (MB_ICONINFORMATION or MB_TOPMOST or MB_SETFOREGROUND)); end;