Skip to content

Commit

Permalink
Log GetLastErrorStr() if GenerateConsoleCtrlEvent(..) fails
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Orlov <[email protected]>
  • Loading branch information
MichaelOrlov committed Jun 11, 2023
1 parent 1995971 commit b9f8031
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b9f8031

Please sign in to comment.