Process_WIN32U.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // Process_WIN32U.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Process_WIN32U.cpp#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: Process
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Process_WIN32U.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/NumberFormatter.h"
  18. #include "Poco/NamedEvent.h"
  19. #include "Poco/UnicodeConverter.h"
  20. #include "Poco/Pipe.h"
  21. namespace Poco {
  22. //
  23. // ProcessHandleImpl
  24. //
  25. ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
  26. _hProcess(hProcess),
  27. _pid(pid)
  28. {
  29. }
  30. ProcessHandleImpl::~ProcessHandleImpl()
  31. {
  32. closeHandle();
  33. }
  34. void ProcessHandleImpl::closeHandle()
  35. {
  36. if (_hProcess)
  37. {
  38. CloseHandle(_hProcess);
  39. _hProcess = NULL;
  40. }
  41. }
  42. UInt32 ProcessHandleImpl::id() const
  43. {
  44. return _pid;
  45. }
  46. HANDLE ProcessHandleImpl::process() const
  47. {
  48. return _hProcess;
  49. }
  50. int ProcessHandleImpl::wait() const
  51. {
  52. DWORD rc = WaitForSingleObject(_hProcess, INFINITE);
  53. if (rc != WAIT_OBJECT_0)
  54. throw SystemException("Wait failed for process", NumberFormatter::format(_pid));
  55. DWORD exitCode;
  56. if (GetExitCodeProcess(_hProcess, &exitCode) == 0)
  57. throw SystemException("Cannot get exit code for process", NumberFormatter::format(_pid));
  58. return exitCode;
  59. }
  60. //
  61. // ProcessImpl
  62. //
  63. ProcessImpl::PIDImpl ProcessImpl::idImpl()
  64. {
  65. return GetCurrentProcessId();
  66. }
  67. void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
  68. {
  69. FILETIME ftCreation;
  70. FILETIME ftExit;
  71. FILETIME ftKernel;
  72. FILETIME ftUser;
  73. if (GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, &ftUser) != 0)
  74. {
  75. ULARGE_INTEGER time;
  76. time.LowPart = ftKernel.dwLowDateTime;
  77. time.HighPart = ftKernel.dwHighDateTime;
  78. kernelTime = long(time.QuadPart/10000000L);
  79. time.LowPart = ftUser.dwLowDateTime;
  80. time.HighPart = ftUser.dwHighDateTime;
  81. userTime = long(time.QuadPart/10000000L);
  82. }
  83. else
  84. {
  85. userTime = kernelTime = -1;
  86. }
  87. }
  88. ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
  89. {
  90. std::string commandLine = command;
  91. for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
  92. {
  93. commandLine.append(" ");
  94. commandLine.append(*it);
  95. }
  96. std::wstring ucommandLine;
  97. UnicodeConverter::toUTF16(commandLine, ucommandLine);
  98. STARTUPINFOW startupInfo;
  99. GetStartupInfoW(&startupInfo); // take defaults from current process
  100. startupInfo.cb = sizeof(STARTUPINFOW);
  101. startupInfo.lpReserved = NULL;
  102. startupInfo.lpDesktop = NULL;
  103. startupInfo.lpTitle = NULL;
  104. startupInfo.dwFlags = STARTF_FORCEOFFFEEDBACK;
  105. startupInfo.cbReserved2 = 0;
  106. startupInfo.lpReserved2 = NULL;
  107. HANDLE hProc = GetCurrentProcess();
  108. bool mustInheritHandles = false;
  109. if (inPipe)
  110. {
  111. DuplicateHandle(hProc, inPipe->readHandle(), hProc, &startupInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  112. mustInheritHandles = true;
  113. inPipe->close(Pipe::CLOSE_READ);
  114. }
  115. else if (GetStdHandle(STD_INPUT_HANDLE))
  116. {
  117. DuplicateHandle(hProc, GetStdHandle(STD_INPUT_HANDLE), hProc, &startupInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  118. mustInheritHandles = true;
  119. }
  120. else
  121. {
  122. startupInfo.hStdInput = 0;
  123. }
  124. // outPipe may be the same as errPipe, so we duplicate first and close later.
  125. if (outPipe)
  126. {
  127. DuplicateHandle(hProc, outPipe->writeHandle(), hProc, &startupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  128. mustInheritHandles = true;
  129. }
  130. else if (GetStdHandle(STD_OUTPUT_HANDLE))
  131. {
  132. DuplicateHandle(hProc, GetStdHandle(STD_OUTPUT_HANDLE), hProc, &startupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  133. mustInheritHandles = true;
  134. }
  135. else
  136. {
  137. startupInfo.hStdOutput = 0;
  138. }
  139. if (errPipe)
  140. {
  141. DuplicateHandle(hProc, errPipe->writeHandle(), hProc, &startupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS);
  142. mustInheritHandles = true;
  143. }
  144. else if (GetStdHandle(STD_ERROR_HANDLE))
  145. {
  146. DuplicateHandle(hProc, GetStdHandle(STD_ERROR_HANDLE), hProc, &startupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS);
  147. mustInheritHandles = true;
  148. }
  149. else
  150. {
  151. startupInfo.hStdError = 0;
  152. }
  153. if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
  154. if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
  155. if (mustInheritHandles)
  156. {
  157. startupInfo.dwFlags |= STARTF_USESTDHANDLES;
  158. }
  159. std::wstring uinitialDirectory;
  160. UnicodeConverter::toUTF16(initialDirectory, uinitialDirectory);
  161. const wchar_t* workingDirectory = uinitialDirectory.empty() ? 0 : uinitialDirectory.c_str();
  162. const char* pEnv = 0;
  163. std::vector<char> envChars;
  164. if (!env.empty())
  165. {
  166. envChars = getEnvironmentVariablesBuffer(env);
  167. pEnv = &envChars[0];
  168. }
  169. PROCESS_INFORMATION processInfo;
  170. DWORD creationFlags = GetConsoleWindow() ? 0 : CREATE_NO_WINDOW;
  171. BOOL rc = CreateProcessW(
  172. NULL, // applicationName
  173. const_cast<wchar_t*>(ucommandLine.c_str()),
  174. NULL, // processAttributes
  175. NULL, // threadAttributes
  176. mustInheritHandles,
  177. creationFlags,
  178. (LPVOID) pEnv,
  179. workingDirectory,
  180. &startupInfo,
  181. &processInfo
  182. );
  183. if (startupInfo.hStdInput) CloseHandle(startupInfo.hStdInput);
  184. if (startupInfo.hStdOutput) CloseHandle(startupInfo.hStdOutput);
  185. if (startupInfo.hStdError) CloseHandle(startupInfo.hStdError);
  186. if (rc)
  187. {
  188. CloseHandle(processInfo.hThread);
  189. return new ProcessHandleImpl(processInfo.hProcess, processInfo.dwProcessId);
  190. }
  191. else throw SystemException("Cannot launch process", command);
  192. }
  193. void ProcessImpl::killImpl(ProcessHandleImpl& handle)
  194. {
  195. if (handle.process())
  196. {
  197. if (TerminateProcess(handle.process(), 0) == 0)
  198. {
  199. handle.closeHandle();
  200. throw SystemException("cannot kill process");
  201. }
  202. handle.closeHandle();
  203. }
  204. }
  205. void ProcessImpl::killImpl(PIDImpl pid)
  206. {
  207. HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  208. if (hProc)
  209. {
  210. if (TerminateProcess(hProc, 0) == 0)
  211. {
  212. CloseHandle(hProc);
  213. throw SystemException("cannot kill process");
  214. }
  215. CloseHandle(hProc);
  216. }
  217. else
  218. {
  219. switch (GetLastError())
  220. {
  221. case ERROR_ACCESS_DENIED:
  222. throw NoPermissionException("cannot kill process");
  223. case ERROR_NOT_FOUND:
  224. throw NotFoundException("cannot kill process");
  225. default:
  226. throw SystemException("cannot kill process");
  227. }
  228. }
  229. }
  230. bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
  231. {
  232. bool result = true;
  233. DWORD exitCode;
  234. BOOL rc = GetExitCodeProcess(handle.process(), &exitCode);
  235. if (!rc || exitCode != STILL_ACTIVE) result = false;
  236. return result;
  237. }
  238. bool ProcessImpl::isRunningImpl(PIDImpl pid)
  239. {
  240. HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
  241. bool result = true;
  242. DWORD exitCode;
  243. BOOL rc = GetExitCodeProcess(hProc, &exitCode);
  244. if (!rc || exitCode != STILL_ACTIVE) result = false;
  245. return result;
  246. }
  247. void ProcessImpl::requestTerminationImpl(PIDImpl pid)
  248. {
  249. NamedEvent ev(terminationEventName(pid));
  250. ev.set();
  251. }
  252. std::string ProcessImpl::terminationEventName(PIDImpl pid)
  253. {
  254. std::string evName("POCOTRM");
  255. NumberFormatter::appendHex(evName, pid, 8);
  256. return evName;
  257. }
  258. } // namespace Poco