diff --git a/except.cpp b/except.cpp index 07ed1ff3..489a7a50 100644 --- a/except.cpp +++ b/except.cpp @@ -14,9 +14,10 @@ * Copyright 2010 Danny Robson */ -#include "except.hpp" -#include "debug.hpp" -#include "platform.hpp" +#include "./except.hpp" +#include "./debug.hpp" +#include "./platform.hpp" +#include "./cast.hpp" #include #include @@ -92,7 +93,7 @@ win32_error::win32_error (DWORD _id): runtime_error ("Win32 error"), id (_id) { - CHECK_NEQ (id, ERROR_SUCCESS); + CHECK_NEQ (id, (DWORD)ERROR_SUCCESS); } @@ -101,7 +102,7 @@ win32_error::win32_error (void): runtime_error ("Win32 error"), id (GetLastError ()) { - CHECK_NEQ (id, ERROR_SUCCESS); + CHECK_NEQ (id, (DWORD)ERROR_SUCCESS); } @@ -134,8 +135,28 @@ win32_error::throw_code (void) void win32_error::throw_code (DWORD id) { - CHECK (id != ERROR_SUCCESS); + CHECK_NEQ (id, (DWORD)ERROR_SUCCESS); throw win32_error (id); } + +//----------------------------------------------------------------------------- +std::string +win32_error::code_string (void) +{ + return code_string (GetLastError ()); +} + +//----------------------------------------------------------------------------- +std::string +win32_error::code_string (DWORD code) +{ + char message[256]; + + auto res = FormatMessage (0, NULL, code, 0, message, sizeof (message), NULL); + if (res == 0) + win32_error::throw_code (); + + return std::string (message); +} #endif diff --git a/except.hpp b/except.hpp index 2619241f..3d4a8736 100644 --- a/except.hpp +++ b/except.hpp @@ -78,6 +78,9 @@ namespace util { static void throw_code [[gnu::noreturn]] (void); static void throw_code [[gnu::noreturn]] (DWORD); + + static std::string code_string (void); + static std::string code_string (DWORD); }; } #endif