Mar 262010

This function is handy for getting and formatting the string that is associated with critical errors.  You just pass in the errno returned from a Win32 call.  This information is available elsewhere, but I’ve found this encapsulated function to be handy.

CString GetCriticalErrorString(DWORD nError) {
  LPVOID lpMsgBuf;
  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
          NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
          (LPTSTR) &lpMsgBuf, 0, NULL );
  CString cs = (LPCTSTR)lpMsgBuf;
  LocalFree(lpMsgBuf);
  return cs;
}

Leave a Reply

(required)

(required)