Reputation: 5669
1) I started a process with ShellExecuteEx
2) retrieve the PID with
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! How is this possible?
Thx
Upvotes: 4
Views: 11190
Reputation: 72539
To terminate a process you have to use the TerminateProcess
function. 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:
HANDLE h = OpenProcess(PROCESS_TERMINATE, false, process_id);
TerminateProcess(h, 1);
CloseHandle(h);
Upvotes: 5