Skip to content

Commit 428da78

Browse files
committed
Log GetLastErrorStr() if GenerateConsoleCtrlEvent(..) fails
Signed-off-by: Michael <[email protected]>
1 parent 1995971 commit 428da78

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

rosbag2_test_common/include/rosbag2_test_common/process_execution_helpers_windows.hpp

+28-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@
2828

2929
using namespace ::testing; // NOLINT
3030

31+
std::string GetLastErrorStr()
32+
{
33+
LPVOID lp_msg_buf;
34+
DWORD err_code = GetLastError();
35+
std::string result_str("Error code:" + std::to_string(err_code) + ". Error message: ");
36+
37+
DWORD buff_len = FormatMessage(
38+
FORMAT_MESSAGE_ALLOCATE_BUFFER |
39+
FORMAT_MESSAGE_FROM_SYSTEM |
40+
FORMAT_MESSAGE_IGNORE_INSERTS,
41+
NULL,
42+
err_code,
43+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
44+
(LPTSTR) &lp_msg_buf,
45+
0, NULL);
46+
if (buff_len) {
47+
LPCSTR lpc_msg_str = (LPCSTR)(lp_msg_buf);
48+
result_str.append(std::string(lpc_msg_str, lpc_msg_str + buff_len));
49+
}
50+
LocalFree(lp_msg_buf);
51+
return result_str;
52+
}
53+
3154
struct Process
3255
{
3356
PROCESS_INFORMATION process_info;
@@ -148,13 +171,15 @@ void stop_execution(
148171
switch (signum) {
149172
// According to the
150173
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/signal?view=msvc-170
151-
// SIGINT and SIGBREAK is not supported for any Win32 application.
174+
// SIGINT is not supported for any Win32 application.
152175
// Need to use native Windows control event instead.
153176
case SIGINT:
154-
EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_C_EVENT, handle.process_info.dwProcessId));
177+
EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_C_EVENT, handle.process_info.dwProcessId)) <<
178+
GetLastErrorStr();
155179
break;
156180
case SIGBREAK:
157-
EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, handle.process_info.dwProcessId));
181+
EXPECT_TRUE(GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, handle.process_info.dwProcessId)) <<
182+
GetLastErrorStr();
158183
break;
159184
case SIGTERM:
160185
// The CTRL_CLOSE_EVENT is analog of the SIGTERM from POSIX. Windows sends CTRL_CLOSE_EVENT

0 commit comments

Comments
 (0)