Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. // error.h
  2.  
  3. #ifndef __ERROR_H_
  4. #define __ERROR_H_
  5.  
  6. void GetLastErrorAsString(LPTSTR msg);
  7.  
  8. #endif
  9.  
  10.  
  11. // error.c
  12. #include "error.h"
  13.  
  14. void GetLastErrorAsString(LPTSTR msg)
  15. {
  16.     //Get the error message, if any.
  17.     DWORD errorMessageID = GetLastError();
  18.     if (errorMessageID == 0)
  19.         return NULL; //No error message has been recorded
  20.  
  21.     LPSTR messageBuffer = NULL;
  22.     size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  23.         NULL, errorMessageID, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&messageBuffer, 0, NULL);
  24.  
  25.     lstrcpy(msg, messageBuffer);
  26.  
  27.     LocalFree(messageBuffer);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement