| |
33 |
33 |
TString strError = FormatWin32ErrorCode(dwErrorCode, false); |
| |
34 |
34 |
if (strError.IsEmpty()) |
| |
35 |
35 |
return FormatWin32ErrorCodeWithModule(dwErrorCode, GetModuleHandle(pszModuleName), bUseNumberFallback); |
| |
36 |
36 |
|
| |
37 |
37 |
return strError; |
| |
38 |
38 |
} |
| |
39 |
39 |
|
| |
40 |
40 |
TString TWin32ErrorFormatter::FormatWin32ErrorCodeWithModule(DWORD dwErrorCode, HMODULE hModule, bool bUseNumberFallback) |
| |
41 |
41 |
{ |
| |
42 |
42 |
const DWORD dwMaxError = 1024; |
| |
43 |
43 |
|
| |
44 |
44 |
TString strData; |
| |
45 |
45 |
wchar_t* pszBuffer = strData.GetBuffer(dwMaxError); |
| |
46 |
46 |
|
| |
47 |
47 |
DWORD dwPos = 0; |
| |
48 |
48 |
if(hModule) |
| |
49 |
49 |
dwPos = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, hModule, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pszBuffer, dwMaxError - 1, nullptr); |
| |
50 |
50 |
else |
| |
51 |
51 |
dwPos = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pszBuffer, dwMaxError - 1, nullptr); |
| |
52 |
52 |
|
| |
53 |
|
if (dwPos == 0xffffffff) |
| |
|
53 |
if (dwPos == 0xffffffff || dwPos == 0) |
| |
54 |
54 |
{ |
| |
55 |
55 |
int iPos = 0; |
| |
56 |
56 |
if (bUseNumberFallback) |
| |
57 |
57 |
iPos = _sntprintf_s(pszBuffer, dwMaxError, _TRUNCATE, _T("ErrorCode: 0x%lx"), dwErrorCode); |
| |
58 |
58 |
strData.ReleaseBufferSetLength(iPos < 0 ? 0 : iPos); |
| |
59 |
59 |
} |
| |
60 |
60 |
else |
| |
61 |
|
{ |
| |
62 |
|
if(dwPos == 0) |
| |
63 |
|
ATLTRACE(L"Cannot format error message. Error code=%lu\n", GetLastError()); |
| |
64 |
|
|
| |
65 |
61 |
strData.ReleaseBufferSetLength(std::min(dwPos, dwMaxError - 1)); |
| |
66 |
|
} |
| |
67 |
62 |
|
| |
68 |
63 |
strData.TrimRightSelf(_T("\r\n")); |
| |
69 |
64 |
|
| |
70 |
65 |
return strData; |
| |
71 |
66 |
} |
| |
72 |
67 |
} |