简体   繁体   English

使用 ShellExecuteEx 启动终止进程

[英]Kill process started with ShellExecuteEx

1) I started a process with ShellExecuteEx 1) 我用 ShellExecuteEx 启动了一个进程

2) retrieve the PID with 2)检索PID

GetProcessId(shellExInfo.hProcess)

Sample Code:示例代码:

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

Now I want to kill the started process with given PID!现在我想用给定的 PID 终止启动的进程! How is this possible?这怎么可能?

Thx谢谢

To terminate a process you have to use the TerminateProcess function.要终止进程,您必须使用TerminateProcess函数。 However, it receives a handle to the process as a parameter:但是,它接收进程句柄作为参数:

TerminateProcess(shellExInfo.hProcess, 1);

If for some reason you store only the process id but not the handle, then you should open a handle first using the OpenProcess function:如果由于某种原因您只存储进程 ID 而没有存储句柄,那么您应该首先使用OpenProcess函数打开一个句柄:

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

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

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