From d7bb04a09c7f04b0c3a8580d6a9ccd6313656fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 22 Aug 2022 03:17:54 +0200 Subject: Add InternetError exception. --- c/win.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 21 deletions(-) (limited to 'c/win.cpp') diff --git a/c/win.cpp b/c/win.cpp index c7ce7ec..601f82c 100644 --- a/c/win.cpp +++ b/c/win.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "pl.h" #include "util.h" @@ -84,24 +85,29 @@ int EBMessageBox(const std::wstring_view text, const std::wstring_view caption, void ShowException(const wchar_t* const fmt, const wchar_t* const title, const UINT uType) noexcept { - const wchar_t* what = L"an exception"; - WcharPtr why; - try { std::rethrow_exception(std::current_exception()); } catch (const term_t& t) { - what = L"a Prolog exception"; - try { why = PlString(t); } catch (...) {} - } catch (const Win32Error& e) { - what = L"a Windows error"; - try { why = WcharPtr::Copy(e.What()); } catch (...) {} + WcharPtr what = PlString(t); + std::wstring msg(wcslen(fmt)+wcslen(what), 0); + Swprintf(msg, fmt, static_cast(what)); + EBMessageBox(msg, title, uType); + } catch (const WideException& e) { + std::wstring msg(wcslen(fmt)+wcslen(e.What()), 0); + Swprintf(msg, fmt, e.What()); + EBMessageBox(msg, title, uType); } catch (const std::exception& e) { - try { why = WcharPtr::FromNarrow(e.what()); } catch (...) {} - } catch (...) {} + auto what = WcharPtr::FromNarrow(e.what()); + std::wstring msg(wcslen(fmt)+wcslen(what), 0); + Swprintf(msg, fmt, static_cast(what)); + EBMessageBox(msg, title, uType); + } catch (...) { + const wchar_t* what = L"an unknown error occurred"; + std::wstring msg(wcslen(fmt)+wcslen(what), 0); + Swprintf(msg, fmt, what); + EBMessageBox(msg, title, uType); + } - std::wstring msg(wcslen(fmt)+wcslen(what)+wcslen(why), 0); - Swprintf(msg, fmt, what, static_cast(why)); - EBMessageBox(msg, title, uType); } int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept @@ -118,9 +124,6 @@ int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept return 1; } -Win32Error::Win32Error() noexcept - : code(GetLastError()) {} - Win32Error::Win32Error(const DWORD code) noexcept : code(code) {} @@ -136,25 +139,66 @@ const char* Win32Error::what() const noexcept { if (!m_szMsg) FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, + FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_FROM_SYSTEM + |FORMAT_MESSAGE_FROM_HMODULE + |FORMAT_MESSAGE_IGNORE_INSERTS, + GetModuleHandle(L"wininet.dll"), code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (char*)&m_szMsg, 0, nullptr); - return m_szMsg; + return m_szMsg? m_szMsg: "An unknown error occurred"; } const wchar_t* Win32Error::What() const noexcept { if (!m_wszMsg) FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, + FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_FROM_SYSTEM + |FORMAT_MESSAGE_FROM_HMODULE + |FORMAT_MESSAGE_IGNORE_INSERTS, + GetModuleHandle(L"wininet.dll"), code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (wchar_t*)&m_wszMsg, 0, nullptr); + return m_wszMsg? m_wszMsg: L"An unknown error occurred"; +} + +InternetError::InternetError(DWORD codeSystem) +{ + if (codeSystem != ERROR_INTERNET_EXTENDED_ERROR) + throw Win32Error{codeSystem}; + + DWORD len, cch; + InternetGetLastResponseInfo(&code, nullptr, &len); + + cch = len+1; + m_szMsg = new char[cch]; + if (!InternetGetLastResponseInfoA(&code, m_szMsg, &cch)) + throw Win32Error{}; + + cch = len+1; + m_wszMsg = new wchar_t[cch]; + if (!InternetGetLastResponseInfo(&code, m_wszMsg, &len)) + throw Win32Error{}; +} + +InternetError::~InternetError() +{ + delete m_szMsg; + delete m_wszMsg; +} + +const char* InternetError::what() const noexcept +{ + return m_szMsg; +} + +const wchar_t* InternetError::What() const noexcept +{ return m_wszMsg; } -- cgit v1.2.3