简体   繁体   English

C ++ TerminateProcess函数

[英]C++ TerminateProcess function

I've been searching examples for the Win32 API C++ function TerminateProcess() but couldn't find any. 我一直在搜索Win32 API C ++函数TerminateProcess()的示例,但找不到任何。

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, 我一般不熟悉Win32 API,所以我想问一下这里比我更好的人能给我看一个例子,

  • Retrieving a process handle by its PID required to terminate it and then call TerminateProcess with it. 通过终止它所需的PID检索进程句柄,然后用它调用TerminateProcess。

If you aren't familiar with C++ a C# equivalent would help too. 如果你不熟悉C ++,那么C#等价物也会有所帮助。

To answer the original question, in order to retrieve a process handle by its PID and call TerminateProcess, you need code like the following: 要回答原始问题,要通过其PID检索进程句柄并调用TerminateProcess,您需要如下代码:

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. 请记住,TerminateProcess不允许其目标在有效状态下清理和退出。 Think twice before using it. 使用前请三思而后行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM