From f61e83d936ebab185b09682f922aa58734153761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 15 Jul 2022 16:40:49 +0200 Subject: Add Win32Error exception class. --- c/common.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'c/common.cpp') diff --git a/c/common.cpp b/c/common.cpp index dd84742..3a99769 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -29,11 +29,35 @@ TCHAR *TszFromSz(const char *sz, int iCp) return tsz; } +Win32Error::Win32Error(DWORD dwErr) +{ + m_dwErr = dwErr; + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + m_dwErr, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&m_szMsg, + 0, NULL + ); +} + +Win32Error::~Win32Error() +{ + if (m_szMsg) + HeapFree(GetProcessHeap(), 0, m_szMsg); +} + +const char *Win32Error::what(void) const noexcept +{ + return m_szMsg; +} + Library::Library(const TCHAR *tszLibrary) { m_hModule = LoadLibrary(tszLibrary); if (!m_hModule) - throw std::invalid_argument("Library not found."); + throw Win32Error(GetLastError()); } Library::~Library() -- cgit v1.2.3