TerminateProcess
Synopsis
#include <windows.h>
BOOL WINAPI TerminateProcess(
_In_ HANDLE hProcess,
_In_ UINT uExitCode
);
Parameters
| Parameter | Description |
|---|---|
hProcess |
Handle to the process to be terminated. The handle must have PROCESS_TERMINATE access. |
uExitCode |
Exit code to be used by the terminated process. This value is available to other processes through GetExitCodeProcess. |
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
- TerminateProcess does not allow the target process to cleanup resources or execute any termination handlers. Use it only as a last resort.
- Calling TerminateProcess on a critical system process can cause the system to become unstable or crash.
- The function is asynchronous; the target process may not have exited when TerminateProcess returns.
Example
#include <windows.h>
#include <stdio.h>
int main(void)
{
DWORD pid = 1234; // PID of the process to terminate
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (!hProc) {
printf("OpenProcess failed: %lu\n", GetLastError());
return 1;
}
if (!TerminateProcess(hProc, 0)) {
printf("TerminateProcess failed: %lu\n", GetLastError());
} else {
printf("Process %lu terminated.\n", pid);
}
CloseHandle(hProc);
return 0;
}