开发者

C++ TerminateProcess function

开发者 https://www.devze.com 2022-12-23 01:55 出处:网络
I\'ve been searching examples for the Win32 API C++ function TerminatePro开发者_如何学编程cess() but couldn\'t find any.

I've been searching examples for the Win32 API C++ function TerminatePro开发者_如何学编程cess() but couldn't find any.

I'm not that familiar with the Win32 API in general and so I wanted to ask if someone here who is better in it than me could show me an example for,

  • Retrieving a process handle by its PID required to terminate it and then call TerminateProcess with it.

If you aren't familiar with C++ a C# equivalent would help too.


To answer the original question, in order to retrieve a process handle by its PID and call TerminateProcess, you need code like the following:

BOOL TerminateProcessEx(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 clean up and exit in a valid state. Think twice before using it.

0

精彩评论

暂无评论...
验证码 换一张
取 消