C ++ TerminateProcess函数 [英] C++ TerminateProcess function

查看:157
本文介绍了C ++ TerminateProcess函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索Win32 API C ++函数的示例

I've been searching examples for the Win32 API C++ function TerminateProcess() but couldn't find any.

我一般对Win32 API不太熟悉,所以我想问一个比我更好的人可以举一个例子给我看

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,

  • 通过终止该进程的PID检索进程句柄,然后使用它调用TerminateProcess.

如果您不熟悉C ++,则等效的C#也将有所帮助.

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

推荐答案

要回答原始问题,为了通过其PID检索进程句柄并调用TerminateProcess,您需要类似以下的代码:

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;
}

请记住,TerminateProcess不允许其目标清除并以有效状态退出.使用前请三思.

Keep in mind that TerminateProcess does not allow its target to clean up and exit in a valid state. Think twice before using it.

这篇关于C ++ TerminateProcess函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆