Skip to content

Commit

Permalink
Add error code in hex to error dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
YaSuenag committed Sep 28, 2024
1 parent 096b382 commit 70e98eb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions SimpleCom/SerialConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion SimpleCom/SerialDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion SimpleCom/SerialSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions SimpleCom/SimpleCom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 11 additions & 16 deletions SimpleCom/WinAPIException.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<LPTSTR>(&_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<LPTSTR>(&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();
}
11 changes: 6 additions & 5 deletions SimpleCom/WinAPIException.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<LPTSTR>(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;

Expand All @@ -50,7 +51,7 @@ namespace SimpleCom {
return _error_caption;
}

inline LPCTSTR GetErrorText() const noexcept {
inline TString GetErrorText() const noexcept {
return _error_text;
}
};
Expand Down
2 changes: 1 addition & 1 deletion SimpleComTest/LogWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions SimpleComTest/WinAPIExceptionTest.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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());
}

};
Expand Down

0 comments on commit 70e98eb

Please sign in to comment.