Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Kill进程从ShellExecuteEx开始_C++_Windows_Winapi_Process_Pid - Fatal编程技术网

C++ Kill进程从ShellExecuteEx开始

C++ Kill进程从ShellExecuteEx开始,c++,windows,winapi,process,pid,C++,Windows,Winapi,Process,Pid,1) 我用ShellExecuteEx启动了一个进程 2) 使用 GetProcessId(shellExInfo.hProcess) 示例代码: SHELLEXECUTEINFO shellExInfo; shellExInfo.cbSize = sizeof(SHELLEXECUTEINFO); shellExInfo.fMask = SEE_MASK_NOCLOSEPROCESS; shellExInfo.hwnd = NULL; shellExInfo.lpVerb = "open"

1) 我用ShellExecuteEx启动了一个进程

2) 使用

GetProcessId(shellExInfo.hProcess)
示例代码:

SHELLEXECUTEINFO shellExInfo;

shellExInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shellExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shellExInfo.hwnd = NULL;
shellExInfo.lpVerb = "open";
shellExInfo.lpFile = processToStart.c_str();
shellExInfo.lpParameters = processParams.c_str();
shellExInfo.lpDirectory = NULL;
shellExInfo.nShow = SW_SHOW;
shellExInfo.hInstApp = NULL;

ShellExecuteEx(&shellExInfo); // start process

GetProcessId(shellExInfo.hProcess); // retrieve PID
现在我想用给定的PID终止已启动的进程!这怎么可能


Thx

要终止进程,必须使用
终止进程
功能。但是,它接收一个进程句柄作为参数:

TerminateProcess(shellExInfo.hProcess, 1);
如果出于某种原因只存储进程id而不存储句柄,则应首先使用
OpenProcess
函数打开句柄:

HANDLE h = OpenProcess(PROCESS_TERMINATE, false, process_id);
TerminateProcess(h, 1);
CloseHandle(h);

请记住,如果关闭
ShellExecuteEx()
返回的进程句柄,那么在调用
OpenProcess()
之前,如果进程关闭并且其ID被重新用于不同的进程,则会引入竞争条件。因此,最好保持进程句柄打开,直到使用完该进程。使用
CreateProcess()
而不是
ShellExecute/Ex()
来运行可执行文件。它不仅是首选API,而且还返回进程ID和进程句柄,因此您不必手动调用
GetProcessId()
。@RemyLebeau但ShellExecute是运行进程的唯一方法,需要权限提升(“runas”操作)。@MonahTuk这不完全正确。虽然“runas”是运行提升进程的唯一官方API,但CodeProject上有一个非官方API(和其他API),它使用与
ShellExecute()
内部使用的提升API相同的API。@RemyLebeau感谢您的解释!