RSC  0.16.0
WindowsSubprocess.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSC project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "WindowsSubprocess.h"
28 
29 #include <stdexcept>
30 #include <sstream>
31 
32 using namespace std;
33 
34 namespace rsc {
35 namespace subprocess {
36 
37 bool CALLBACK terminateAppEnum(HWND hwnd, LPARAM lParam) {
38 
39  DWORD id;
40  GetWindowThreadProcessId(hwnd, &id);
41 
42  if(id == (DWORD)lParam) {
43  PostMessage(hwnd, WM_CLOSE, 0, 0);
44  }
45 
46  return true;
47 
48 }
49 
50 WindowsSubprocess::TerminateResult WindowsSubprocess::terminateApp(DWORD pid,
51  DWORD timeoutMs) {
52 
53  // If we can't open the process with PROCESS_TERMINATE rights,
54  // then we give up immediately.
55  HANDLE process = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, false, pid);
56 
57  if (process == NULL) {
58  return FAILED;
59  }
60 
61  // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
62  // matches your process's.
63  EnumWindows((WNDENUMPROC) terminateAppEnum, (LPARAM) pid);
64 
65  // Wait on the handle. If it signals, great. If it times out,
66  // then you kill it.
67  TerminateResult terminateReturn;
68  if (WaitForSingleObject(process, timeoutMs) != WAIT_OBJECT_0) {
69  terminateReturn
70  = (TerminateProcess(process, 0) ? SUCCESS_KILL : FAILED);
71  } else {
72  terminateReturn = SUCCESS_CLEAN;
73  }
74 
75  CloseHandle(process);
76 
77  return terminateReturn;
78 }
79 
80 WindowsSubprocess::WindowsSubprocess(const string& command,
81  const vector<string>& args) {
82 
83  STARTUPINFO startupInfo = { 0 };
84  startupInfo.cb = sizeof(startupInfo);
85 
86  stringstream commandLine;
87  commandLine << command << " ";
88  for (vector<string>::const_iterator argIt = args.begin(); argIt
89  != args.end(); ++argIt) {
90  commandLine << "\"" << *argIt << "\" ";
91  }
92 
93  // Try to start the process
94  bool result = ::CreateProcess(NULL,
95  const_cast<char*> (commandLine.str().c_str()), NULL, NULL, false,
96  NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo,
97  &processInformation);
98 
99  if (result == 0) {
100  throw runtime_error("Could not create process");
101  }
102 
103 }
104 
105 WindowsSubprocess::~WindowsSubprocess() {
106  terminateApp(processInformation.dwProcessId, 5000);
107 }
108 
109 }
110 }
STL namespace.
bool CALLBACK terminateAppEnum(HWND hwnd, LPARAM lParam)