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 m;
46 if (!MyFormatMessage(errorCode, m) || m.IsEmpty())
47 {
48 char s[16];
49 for (int i = 0; i < 8; i++)
50 {
51 unsigned t = errorCode & 0xF;
52 errorCode >>= 4;
53 s[7 - i] = (char)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
54 }
55 s[8] = 0;
56 m += "Error #";
57 m += s;
58 }
59 else if (m.Len() >= 2
60 && m[m.Len() - 1] == 0x0A
61 && m[m.Len() - 2] == 0x0D)
62 m.DeleteFrom(m.Len() - 2);
63 return m;
64 }
65
66 }}
67