C ++ TerminateProcess Function

I searched for examples for the Win32 API C ++ TerminateProcess () function, but could not find.

I am not familiar with the Win32 API at all, so I wanted to ask if anyone who is better than me can show me an example,

  • Getting the process descriptor of its PID, necessary to complete it, and then calls TerminateProcess with it.

If you are new to C ++, the C # equivalent will help too.

+7
c ++ windows process termination
source share
1 answer

To answer the original question, to get the process descriptor by its PID and call TerminateProcess, you will need the following code:

BOOL TerminateProcess(DWORD dwProcessId, UINT uExitCode) { DWORD dwDesiredAccess = PROCESS_TERMINATE; BOOL bInheritHandle = FALSE; HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); if (hProcess == NULL) return FALSE; BOOL result = TerminateProcess(hProcess, uExitCode); CloseHandle(hProcess); return result; } 

Keep in mind that TerminateProcess does not allow its target to clear and exit in the correct state. Think twice before using it.

+15
source share

All Articles