-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrorstr.cpp
More file actions
154 lines (114 loc) · 3.07 KB
/
errorstr.cpp
File metadata and controls
154 lines (114 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include "time.h"
/*++
ErrorString:
This routine does it's very best to translate a given error code into a
text message. Any trailing non-printable characters are striped from the
end of the text message, such as carriage returns and line feeds.
Arguments:
dwErrorCode supplies the error code to be translated.
Return Value:
The address of a freshly allocated text string. Use FreeErrorString to
dispose of it.
Throws:
Errors are thrown as DWORD status codes.
Remarks:
--*/
void print_time(void);
DWORD ErrorPrint();
//errorstr.cpp
LPCTSTR ErrorString(DWORD dwLastError);
DWORD ErrorPrint()
{
DWORD dwError = GetLastError();
ErrorString(dwError);
return dwError;
}
LPCTSTR
ErrorString(DWORD dwErrorCode)
{
LPTSTR szErrorString = NULL;
DWORD dwLen;
dwLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwErrorCode,
LANG_NEUTRAL,
(LPTSTR)&szErrorString,
0,
NULL);
if ((0 == dwLen) || (szErrorString == NULL))
{
//ASSERT(NULL == szErrorString);
dwLen = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandle("wininet"),
dwErrorCode,
LANG_NEUTRAL,
//MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
//LANG_INVARIANT,
//GetSystemDefaultLangID(),
(LPTSTR)&szErrorString,
0,
NULL);
//printf("dwLen 2:%d %X \n", dwLen, szErrorString);
if (0 == dwLen)
{
//ASSERT(NULL == szErrorString);
if (szErrorString != NULL)
{
printf("Assertion failure: szErrorString should be NULL as FormatMessage failed\n");
}
}
}
//Required for localization !!!
if (szErrorString != NULL)
{
char OEMString[256];
CharToOemBuff(szErrorString, (LPSTR)OEMString, strlen(szErrorString));
OEMString[strlen(szErrorString)] = '\0';
printf("\n(%d) (0x%X) %s\n", dwErrorCode, dwErrorCode, OEMString);
}
else
{
/*
C:\Temp>err 2f92
# for hex 0x2f92 / decimal 12178
ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR winhttp.h
# 1 matches found for "2f92"*/
if (dwErrorCode == 0x2F92)
{
printf("\n(%d) (0x%X) %s\n", dwErrorCode, dwErrorCode, "ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR");
}
else
{
printf("\n(%d) (0x%X) %s\n", dwErrorCode, dwErrorCode, "Unable to get error message");
}
}
//SetConsoleCP(863);
//printf("%d\n",GetConsoleCP());
return szErrorString;
}
/*++
FreeErrorString:
This routine frees the Error String allocated by the ErrorString service.
Arguments:
szErrorString supplies the error string to be deallocated.
Return Value:
None
Throws:
None
Remarks:
--*/
void
FreeErrorString(
LPCTSTR szErrorString)
{
if (NULL != szErrorString)
LocalFree((LPVOID)szErrorString);
}