1 // Windows/ErrorMsg.h
2 
3 #include "StdAfx.h"
4 
5 #ifndef _UNICODE
6 #include "../Common/StringConvert.h"
7 #endif
8 
9 #include "ErrorMsg.h"
10 
11 #ifndef _UNICODE
12 extern bool g_IsNT;
13 #endif
14 
15 namespace NWindows {
16 namespace NError {
17 
MyFormatMessage(DWORD errorCode,UString & message)18 static bool MyFormatMessage(DWORD errorCode, UString &message)
19 {
20   LPVOID msgBuf;
21   #ifndef _UNICODE
22   if (!g_IsNT)
23   {
24     if (::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
25         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
26         NULL, errorCode, 0, (LPTSTR) &msgBuf, 0, NULL) == 0)
27       return false;
28     message = GetUnicodeString((LPCTSTR)msgBuf);
29   }
30   else
31   #endif
32   {
33     if (::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
34         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
35         NULL, errorCode, 0, (LPWSTR) &msgBuf, 0, NULL) == 0)
36       return false;
37     message = (LPCWSTR)msgBuf;
38   }
39   ::LocalFree(msgBuf);
40   return true;
41 }
42 
MyFormatMessage(DWORD errorCode)43 UString MyFormatMessage(DWORD errorCode)
44 {
45   UString message;
46   if (!MyFormatMessage(errorCode, message))
47   {
48     wchar_t s[16];
49     for (int i = 0; i < 8; i++)
50     {
51       unsigned t = errorCode & 0xF;
52       errorCode >>= 4;
53       s[7 - i] = (wchar_t)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
54     }
55     s[8] = '\0';
56     message = (UString)L"Error #" + s;
57   }
58   return message;
59 }
60 
61 }}
62