TODO: Move to WinApi.hpp
| 2 | |
| 3 | // TODO: Move to WinApi.hpp |
| 4 | std::string w_FormatMessageA(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, DWORD nSize, va_list* Arguments) |
| 5 | { |
| 6 | LPSTR pErrorText = nullptr; |
| 7 | |
| 8 | auto szErrorText = FormatMessageA( |
| 9 | dwFlags, |
| 10 | lpSource, |
| 11 | dwMessageId, |
| 12 | dwLanguageId, |
| 13 | reinterpret_cast<LPSTR>(&pErrorText), |
| 14 | nSize, |
| 15 | Arguments); |
| 16 | if (0 == szErrorText) |
| 17 | { |
| 18 | std::ostringstream oss; |
| 19 | oss << "FormatMessageA failed: " << GetLastError(); |
| 20 | throw std::runtime_error(oss.str()); |
| 21 | } |
| 22 | |
| 23 | const auto sErrorText = std::string(pErrorText); |
| 24 | |
| 25 | /* if FORMAT_MESSAGE_ALLOCATE_BUFFER is used, the buffer is allocated using LocalAlloc, so after the std::string initialization we should free it */ |
| 26 | if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) |
| 27 | { |
| 28 | LocalFree(pErrorText); |
| 29 | pErrorText = nullptr; |
| 30 | } |
| 31 | |
| 32 | return sErrorText; |
| 33 | } |
| 34 | |
| 35 | std::string GetLastErrorString(std::string FailedFunctionName, DWORD dwLastError) |
| 36 | { |
no outgoing calls
no test coverage detected