| 43 | |
| 44 | #ifdef US_PLATFORM_WINDOWS |
| 45 | std::string |
| 46 | GetLastWin32ErrorStr() |
| 47 | { |
| 48 | // Retrieve the system error message for the last-error code |
| 49 | LPVOID lpMsgBuf; |
| 50 | DWORD dw = GetLastError(); |
| 51 | |
| 52 | DWORD rc = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
| 53 | | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 54 | NULL, |
| 55 | dw, |
| 56 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 57 | reinterpret_cast<LPWSTR>(&lpMsgBuf), |
| 58 | 0, |
| 59 | NULL); |
| 60 | |
| 61 | // If FormatMessage fails using FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 62 | // it means that the size of the error message exceeds an internal |
| 63 | // buffer limit (128 kb according to MSDN) and lpMsgBuf will be |
| 64 | // uninitialized. |
| 65 | // Inform the caller that the error message couldn't be retrieved. |
| 66 | if (rc == 0) |
| 67 | { |
| 68 | return std::string("Failed to retrieve error message."); |
| 69 | } |
| 70 | |
| 71 | std::string errMsg(ToUTF8String(std::wstring(reinterpret_cast<LPCWSTR>(lpMsgBuf)))); |
| 72 | |
| 73 | LocalFree(lpMsgBuf); |
| 74 | return errMsg; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | std::string |