diff --git a/rosbag2_test_common/include/rosbag2_test_common/process_execution_helpers_windows.hpp b/rosbag2_test_common/include/rosbag2_test_common/process_execution_helpers_windows.hpp index 5bc83bf196..c86ff80ebf 100644 --- a/rosbag2_test_common/include/rosbag2_test_common/process_execution_helpers_windows.hpp +++ b/rosbag2_test_common/include/rosbag2_test_common/process_execution_helpers_windows.hpp @@ -28,6 +28,29 @@ using namespace ::testing; // NOLINT +std::string GetLastErrorStr() +{ + LPVOID lp_msg_buf; + DWORD err_code = GetLastError(); + std::string result_str("Error code:" + std::to_string(err_code) + ". Error message: "); + + DWORD buff_len = FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err_code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lp_msg_buf, + 0, NULL); + if (buff_len) { + LPCSTR lpc_msg_str = (LPCSTR)(lp_msg_buf); + result_str.append(std::string(lpc_msg_str, lpc_msg_str + buff_len)); + } + LocalFree(lp_msg_buf); + return result_str; +} + struct Process { PROCESS_INFORMATION process_info; @@ -148,13 +171,15 @@ void stop_execution( switch (signum) { // According to the // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/signal?view=msvc-170 - // SIGINT and SIGBREAK is not supported for any Win32 application. + // SIGINT is not supported for any Win32 application. // Need to use native Windows control event instead. case SIGINT: - EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_C_EVENT, handle.process_info.dwProcessId)); + EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_C_EVENT, handle.process_info.dwProcessId)) << + GetLastErrorStr(); break; case SIGBREAK: - EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, handle.process_info.dwProcessId)); + EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, handle.process_info.dwProcessId)) << + GetLastErrorStr(); break; case SIGTERM: // The CTRL_CLOSE_EVENT is analog of the SIGTERM from POSIX. Windows sends CTRL_CLOSE_EVENT