C++ TerminateProcess function

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.


    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.

    链接地址: http://www.djcxy.com/p/88656.html

    上一篇: 嘲笑Java枚举添加一个值来测试失败案例

    下一篇: C ++ TerminateProcess函数