C++ TerminateProcess function

▼魔方 西西 提交于 2019-12-05 16:56:42

问题


I've been searching examples for the Win32 API C++ function TerminateProcess() 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.


回答1:


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.



来源:https://stackoverflow.com/questions/2443738/c-terminateprocess-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!