Process_WINCE.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Process_WINCE.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Process_WINCE.cpp#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: Process
  9. //
  10. // Copyright (c) 2004-2010, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Process_WINCE.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 (GetThreadTimes(GetCurrentThread(), &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::wstring ucommand;
  91. UnicodeConverter::toUTF16(command, ucommand);
  92. std::string commandLine;
  93. for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
  94. {
  95. if (it != args.begin()) commandLine.append(" ");
  96. commandLine.append(*it);
  97. }
  98. std::wstring ucommandLine;
  99. UnicodeConverter::toUTF16(commandLine, ucommandLine);
  100. PROCESS_INFORMATION processInfo;
  101. BOOL rc = CreateProcessW(
  102. ucommand.c_str(),
  103. const_cast<wchar_t*>(ucommandLine.c_str()),
  104. NULL,
  105. NULL,
  106. FALSE,
  107. 0,
  108. NULL,
  109. NULL,
  110. NULL/*&startupInfo*/,
  111. &processInfo
  112. );
  113. if (rc)
  114. {
  115. CloseHandle(processInfo.hThread);
  116. return new ProcessHandleImpl(processInfo.hProcess, processInfo.dwProcessId);
  117. }
  118. else throw SystemException("Cannot launch process", command);
  119. }
  120. void ProcessImpl::killImpl(ProcessHandleImpl& handle)
  121. {
  122. if (handle.process())
  123. {
  124. if (TerminateProcess(handle.process(), 0) == 0)
  125. {
  126. handle.closeHandle();
  127. throw SystemException("cannot kill process");
  128. }
  129. handle.closeHandle();
  130. }
  131. }
  132. void ProcessImpl::killImpl(PIDImpl pid)
  133. {
  134. HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  135. if (hProc)
  136. {
  137. if (TerminateProcess(hProc, 0) == 0)
  138. {
  139. CloseHandle(hProc);
  140. throw SystemException("cannot kill process");
  141. }
  142. CloseHandle(hProc);
  143. }
  144. else
  145. {
  146. switch (GetLastError())
  147. {
  148. case ERROR_ACCESS_DENIED:
  149. throw NoPermissionException("cannot kill process");
  150. case ERROR_NOT_FOUND:
  151. throw NotFoundException("cannot kill process");
  152. default:
  153. throw SystemException("cannot kill process");
  154. }
  155. }
  156. }
  157. bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
  158. {
  159. bool result = true;
  160. DWORD exitCode;
  161. BOOL rc = GetExitCodeProcess(handle.process(), &exitCode);
  162. if (!rc || exitCode != STILL_ACTIVE) result = false;
  163. return result;
  164. }
  165. bool ProcessImpl::isRunningImpl(PIDImpl pid)
  166. {
  167. HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
  168. bool result = true;
  169. DWORD exitCode;
  170. BOOL rc = GetExitCodeProcess(hProc, &exitCode);
  171. if (!rc || exitCode != STILL_ACTIVE) result = false;
  172. return result;}
  173. void ProcessImpl::requestTerminationImpl(PIDImpl pid)
  174. {
  175. NamedEvent ev(terminationEventName(pid));
  176. ev.set();
  177. }
  178. std::string ProcessImpl::terminationEventName(PIDImpl pid)
  179. {
  180. std::string evName("POCOTRM");
  181. NumberFormatter::appendHex(evName, pid, 8);
  182. return evName;
  183. }
  184. } // namespace Poco