3

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

2)检索PID

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 杀死启动的进程!这怎么可能?

谢谢

4

1 回答 1

4

要终止进程,您必须使用该TerminateProcess函数。但是,它接收进程的句柄作为参数:

TerminateProcess(shellExInfo.hProcess, 1);

如果由于某种原因您只存储进程 ID 而不是句柄,那么您应该首先使用以下OpenProcess函数打开一个句柄:

HANDLE h = OpenProcess(PROCESS_TERMINATE, false, process_id);
TerminateProcess(h, 1);
CloseHandle(h);
于 2012-11-02T10:50:28.817 回答