process_handle_windows.ipp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (c) 2022 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_V2_DETAIL_IMPL_PROCESS_HANDLE_WINDOWS_IPP
  6. #define BOOST_PROCESS_V2_DETAIL_IMPL_PROCESS_HANDLE_WINDOWS_IPP
  7. #include <boost/process/v2/detail/config.hpp>
  8. #include <boost/process/v2/detail/last_error.hpp>
  9. #include <boost/process/v2/detail/throw_error.hpp>
  10. #include <boost/process/v2/detail/process_handle_windows.hpp>
  11. #include <boost/process/v2/ext/detail/proc_info.hpp>
  12. #include <windows.h>
  13. #if !defined(BOOST_PROCESS_V2_DISABLE_UNDOCUMENTED_API)
  14. extern "C"
  15. {
  16. LONG WINAPI NtResumeProcess(HANDLE ProcessHandle);
  17. LONG WINAPI NtSuspendProcess(HANDLE ProcessHandle);
  18. }
  19. #endif
  20. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  21. namespace detail
  22. {
  23. void get_exit_code_(
  24. HANDLE handle,
  25. native_exit_code_type & exit_code,
  26. error_code & ec)
  27. {
  28. if (!::GetExitCodeProcess(handle, &exit_code))
  29. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  30. }
  31. HANDLE open_process_(DWORD pid)
  32. {
  33. auto proc = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pid);
  34. if (proc == nullptr)
  35. {
  36. error_code ec;
  37. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  38. throw system_error(ec, "open_process()");
  39. }
  40. return proc;
  41. }
  42. void terminate_if_running_(HANDLE handle)
  43. {
  44. DWORD exit_code = 0u;
  45. if (handle == INVALID_HANDLE_VALUE)
  46. return ;
  47. if (::GetExitCodeProcess(handle, &exit_code))
  48. if (exit_code == STILL_ACTIVE)
  49. ::TerminateProcess(handle, 260);
  50. }
  51. bool check_handle_(HANDLE handle, error_code & ec)
  52. {
  53. if (handle == INVALID_HANDLE_VALUE)
  54. {
  55. BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_INVALID_HANDLE_STATE, system_category())
  56. return false;
  57. }
  58. return true;
  59. }
  60. bool check_pid_(pid_type pid_, error_code & ec)
  61. {
  62. if (pid_ == 0)
  63. {
  64. BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_INVALID_HANDLE_STATE, system_category())
  65. return false;
  66. }
  67. return true;
  68. }
  69. struct enum_windows_data_t
  70. {
  71. error_code &ec;
  72. pid_type pid;
  73. };
  74. static BOOL CALLBACK enum_window(HWND hwnd, LPARAM param)
  75. {
  76. auto data = reinterpret_cast<enum_windows_data_t*>(param);
  77. DWORD pid{0u};
  78. GetWindowThreadProcessId(hwnd, &pid);
  79. if (pid != data->pid)
  80. return TRUE;
  81. LRESULT res = ::SendMessageW(hwnd, WM_CLOSE, 0, 0);
  82. if (res)
  83. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(data->ec)
  84. return res == 0;
  85. }
  86. void request_exit_(pid_type pid_, error_code & ec)
  87. {
  88. enum_windows_data_t data{ec, pid_};
  89. if (!::EnumWindows(enum_window, reinterpret_cast<LONG_PTR>(&data)))
  90. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  91. }
  92. void interrupt_(pid_type pid_, error_code & ec)
  93. {
  94. if (!::GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid_))
  95. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  96. }
  97. void terminate_(HANDLE handle, error_code & ec, DWORD & exit_status)
  98. {
  99. if (!::TerminateProcess(handle, 260))
  100. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  101. }
  102. void check_running_(HANDLE handle, error_code & ec, DWORD & exit_status)
  103. {
  104. if (!::GetExitCodeProcess(handle, &exit_status))
  105. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  106. }
  107. #if !defined(BOOST_PROCESS_V2_DISABLE_UNDOCUMENTED_API)
  108. void suspend_(HANDLE handle, error_code & ec)
  109. {
  110. auto nt_err = NtSuspendProcess(handle);
  111. ULONG dos_err = RtlNtStatusToDosError(nt_err);
  112. if (dos_err)
  113. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  114. }
  115. void resume_(HANDLE handle, error_code & ec)
  116. {
  117. auto nt_err = NtResumeProcess(handle);
  118. ULONG dos_err = RtlNtStatusToDosError(nt_err);
  119. if (dos_err)
  120. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  121. }
  122. #else
  123. void suspend_(HANDLE, error_code & ec)
  124. {
  125. BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_CALL_NOT_IMPLEMENTED, system_category())
  126. }
  127. void resume_(HANDLE handle, error_code & ec)
  128. {
  129. BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_CALL_NOT_IMPLEMENTED, system_category())
  130. }
  131. #endif
  132. #if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
  133. template struct basic_process_handle_win<>;
  134. #endif
  135. }
  136. BOOST_PROCESS_V2_END_NAMESPACE
  137. #endif //BOOST_PROCESS_V2_DETAIL_IMPL_PROCESS_HANDLE_WINDOWS_IPP