diff --git a/SimpleCom/SerialConnection.cpp b/SimpleCom/SerialConnection.cpp index b8bf537..29f6e85 100644 --- a/SimpleCom/SerialConnection.cpp +++ b/SimpleCom/SerialConnection.cpp @@ -189,7 +189,7 @@ DWORD WINAPI StdOutRedirector(_In_ LPVOID lpParameter) { return 0; } else { - MessageBox(param->parent_hwnd, e.GetErrorText(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); + MessageBox(param->parent_hwnd, e.GetErrorText().c_str(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); return -1; } } @@ -275,7 +275,7 @@ bool SimpleCom::SerialConnection::StdInRedirector(const HANDLE hSerial, const HA SetEvent(hTermEvent); // We can ignore ERROR_OPERATION_ABORTED because it would be intended. if (e.GetErrorCode() != ERROR_OPERATION_ABORTED) { - MessageBox(_parent_hwnd, e.GetErrorText(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); + MessageBox(_parent_hwnd, e.GetErrorText().c_str(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); } } diff --git a/SimpleCom/SerialDeviceScanner.cpp b/SimpleCom/SerialDeviceScanner.cpp index 393a581..94f6ba6 100644 --- a/SimpleCom/SerialDeviceScanner.cpp +++ b/SimpleCom/SerialDeviceScanner.cpp @@ -74,7 +74,7 @@ static INT_PTR CALLBACK WaitDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM l } } catch (SimpleCom::WinAPIException& e) { - MessageBox(hDlg, e.GetErrorText(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); + MessageBox(hDlg, e.GetErrorText().c_str(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); return FALSE; } diff --git a/SimpleCom/SerialSetup.cpp b/SimpleCom/SerialSetup.cpp index 1860dd7..30fe62e 100644 --- a/SimpleCom/SerialSetup.cpp +++ b/SimpleCom/SerialSetup.cpp @@ -468,7 +468,7 @@ static INT_PTR CALLBACK SettingDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARA } } catch (SimpleCom::WinAPIException& e) { - MessageBox(hDlg, e.GetErrorText(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); + MessageBox(hDlg, e.GetErrorText().c_str(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); return FALSE; } diff --git a/SimpleCom/SimpleCom.cpp b/SimpleCom/SimpleCom.cpp index 9e70cc6..44f94b5 100644 --- a/SimpleCom/SimpleCom.cpp +++ b/SimpleCom/SimpleCom.cpp @@ -112,7 +112,7 @@ int _tmain(int argc, LPCTSTR argv[]) setup.SaveToDCB(&dcb); } catch (SimpleCom::WinAPIException& e) { - MessageBox(parent_hwnd, e.GetErrorText(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); + MessageBox(parent_hwnd, e.GetErrorText().c_str(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); return -1; } catch (SimpleCom::SerialSetupException& e) { @@ -157,7 +157,7 @@ int _tmain(int argc, LPCTSTR argv[]) } } catch (SimpleCom::WinAPIException& e) { - MessageBox(parent_hwnd, e.GetErrorText(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); + MessageBox(parent_hwnd, e.GetErrorText().c_str(), e.GetErrorCaption(), MB_OK | MB_ICONERROR); return -4; } catch (SimpleCom::SerialDeviceScanException& e) { diff --git a/SimpleCom/WinAPIException.cpp b/SimpleCom/WinAPIException.cpp index a96be09..4d65871 100644 --- a/SimpleCom/WinAPIException.cpp +++ b/SimpleCom/WinAPIException.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019, 2021, Yasumasa Suenaga + * Copyright (C) 2019, 2024, Yasumasa Suenaga * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -21,29 +21,24 @@ #include "WinAPIException.h" #include "debug.h" -static thread_local TCHAR fallback_error_text[100]; - SimpleCom::WinAPIException::WinAPIException(DWORD error_code, LPCTSTR error_caption) : _error_code(error_code), - _error_caption(error_caption), - _error_text(nullptr) + _error_caption(error_caption) { - DWORD result = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, _error_code, 0, reinterpret_cast(&_error_text), 0, NULL); + LPTSTR buf = nullptr; + TStringStream error_msg; + DWORD result = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, _error_code, 0, reinterpret_cast(&buf), 0, NULL); if (result == 0) { DWORD errcode = GetLastError(); - _sntprintf_s(fallback_error_text, sizeof(fallback_error_text) / sizeof(TCHAR), _T("Error occured (%#x)"), _error_code); - _error_text = fallback_error_text; + error_msg << _T("Error occured (") << std::showbase << std::hex << _error_code << _T(")"); TStringStream msg; msg << _T("Error occurred in FormatMessage (") << std::showbase << std::hex << errcode << _T(")"); debug::log(msg.str().c_str()); } -} - - -SimpleCom::WinAPIException::~WinAPIException() -{ - if ((_error_text != nullptr) && (_error_text != fallback_error_text) && (_error_code != -1)) { - LocalFree(_error_text); + else { + error_msg << buf << _T(" (") << std::showbase << std::hex << _error_code << _T(")"); + LocalFree(buf); } -} + _error_text = error_msg.str(); +} \ No newline at end of file diff --git a/SimpleCom/WinAPIException.h b/SimpleCom/WinAPIException.h index 5444a55..95f682b 100644 --- a/SimpleCom/WinAPIException.h +++ b/SimpleCom/WinAPIException.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019, 2021, Yasumasa Suenaga + * Copyright (C) 2019, 2024, Yasumasa Suenaga * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -32,13 +32,14 @@ namespace SimpleCom { private: DWORD _error_code; LPCTSTR _error_caption; - LPTSTR _error_text; + TString _error_text; public: - explicit WinAPIException(LPCTSTR error_caption, LPCTSTR error_text) : _error_code(-1), _error_caption(error_caption), _error_text(const_cast(error_text)) {} + explicit WinAPIException(LPCTSTR error_caption, LPCTSTR error_text) : _error_code(-1), _error_caption(error_caption), _error_text(error_text) {} explicit WinAPIException(DWORD error_code) : WinAPIException(error_code, _T("SimpleCom")) {} explicit WinAPIException(DWORD error_code, LPCTSTR error_caption); - virtual ~WinAPIException(); + WinAPIException(const WinAPIException &ex) : _error_code(ex._error_code), _error_caption(ex._error_caption), _error_text(ex._error_text) {} + virtual ~WinAPIException() {}; WinAPIException& operator = (WinAPIException& rvalue) = delete; @@ -50,7 +51,7 @@ namespace SimpleCom { return _error_caption; } - inline LPCTSTR GetErrorText() const noexcept { + inline TString GetErrorText() const noexcept { return _error_text; } }; diff --git a/SimpleComTest/LogWriterTest.cpp b/SimpleComTest/LogWriterTest.cpp index f0b6353..25bc267 100644 --- a/SimpleComTest/LogWriterTest.cpp +++ b/SimpleComTest/LogWriterTest.cpp @@ -37,7 +37,7 @@ namespace SimpleComTest HANDLE hnd = CreateFile(TESTFILENAME, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); if (hnd == INVALID_HANDLE_VALUE) { SimpleCom::WinAPIException ex(GetLastError()); - Assert::Fail(ex.GetErrorText()); + Assert::Fail(ex.GetErrorText().c_str()); } DWORD fsize = GetFileSize(hnd, nullptr); char* contents = new char[fsize + 1]; diff --git a/SimpleComTest/WinAPIExceptionTest.cpp b/SimpleComTest/WinAPIExceptionTest.cpp index 20a851d..7de374f 100644 --- a/SimpleComTest/WinAPIExceptionTest.cpp +++ b/SimpleComTest/WinAPIExceptionTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, Yasumasa Suenaga + * Copyright (C) 2023, 2024, Yasumasa Suenaga * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -49,7 +49,7 @@ namespace SimpleComTest TEST_METHOD(MessageTest) { SimpleCom::WinAPIException e(TEST_ERRCODE); - Assert::AreEqual(expected, e.GetErrorText()); + Assert::AreEqual(expected, e.GetErrorText().c_str()); } };