Returns the last Win32 error, in string format. Returns an empty string if there is no error.
| 231 | |
| 232 | // Returns the last Win32 error, in string format. Returns an empty string if there is no error. |
| 233 | std::string GetLastErrorAsString() { |
| 234 | // Get the error message ID, if any. |
| 235 | DWORD errorMessageID = ::GetLastError(); |
| 236 | if (errorMessageID == 0) { |
| 237 | return std::string(); // No error message has been recorded |
| 238 | } |
| 239 | |
| 240 | LPSTR messageBuffer = nullptr; |
| 241 | |
| 242 | // Ask Win32 to give us the string version of that message ID. |
| 243 | // The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet |
| 244 | // know how long the message string will be). |
| 245 | size_t size = |
| 246 | FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 247 | NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); |
| 248 | |
| 249 | // Copy the error message into a std::string. |
| 250 | std::string message(messageBuffer, size); |
| 251 | |
| 252 | // Free the Win32's string's buffer. |
| 253 | LocalFree(messageBuffer); |
| 254 | |
| 255 | return message; |
| 256 | } |
| 257 | |
| 258 | bool Delay(long mis) { |
| 259 | MSG msg = {}; |