10 out of 24 rated this helpful - Rate this topic

TerminateProcess function

Applies to: desktop apps only

Terminates the specified process and all of its threads.

Syntax

BOOL WINAPI TerminateProcess(
  __in  HANDLE hProcess,
  __in  UINT uExitCode
);

Parameters

hProcess [in]

A handle to the process to be terminated.

The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.

uExitCode [in]

The exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.

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

The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.

This function stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed or canceled. When a process terminates, its kernel object is not destroyed until all processes that have open handles to the process have released those handles.

TerminateProcess is asynchronous; it initiates termination and returns immediately. If you need to be sure the process has terminated, call the WaitForSingleObject function with a handle to the process.

A process cannot prevent itself from being terminated.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

ExitProcess
OpenProcess
GetExitCodeProcess
GetExitCodeThread
Process and Thread Functions
Processes
Terminating a Process

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Behavior with INVALID_HANDLE_VALUE argument
TerminateProcess(INVALID_HANDLE_VALUE,0) is the same as TerminateProcess( GetCurrentProcess(),0); Both terminate the current process ( kill self ). With other invalid handle values, TerminateProcess() fails and returns 0. GetLastError() gives error for bad handle.
Terminating services running as LocalSystem

To terminate services running as LocalSystem, you need to enable the SeDebugPrivilege privilege, prior to calling the OpenProcess function with PROCESS_TERMINATE.