34 namespace subprocess {
39 GetWindowThreadProcessId(hwnd, &
id);
41 if(
id == (DWORD)lParam)
43 PostMessage(hwnd, WM_CLOSE, 0, 0);
55 HANDLE process = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE,
false, pid);
57 if (process == NULL) {
68 if (WaitForSingleObject(process, timeoutMs) != WAIT_OBJECT_0) {
70 = (TerminateProcess(process, 0) ? SUCCESS_KILL : FAILED);
72 terminateReturn = SUCCESS_CLEAN;
77 return terminateReturn;
80 WindowsSubprocess::WindowsSubprocess(
const string& command,
81 const vector<string>& args) {
83 STARTUPINFO startupInfo = { 0 };
84 startupInfo.cb =
sizeof(startupInfo);
86 stringstream commandLine;
87 commandLine << command <<
" ";
88 for (vector<string>::const_iterator argIt = args.begin(); argIt
89 != args.end(); ++argIt) {
90 commandLine <<
"\"" << *argIt <<
"\" ";
94 bool result = ::CreateProcess(NULL,
95 const_cast<char*> (commandLine.str().c_str()), NULL, NULL,
false,
96 NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo,
100 throw runtime_error(
"Could not create process");
105 WindowsSubprocess::~WindowsSubprocess() {
106 terminateApp(processInformation.dwProcessId, 5000);
bool CALLBACK terminateAppEnum(HWND hwnd, LPARAM lParam)