-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorOutFunctions.h
188 lines (163 loc) · 5.36 KB
/
ErrorOutFunctions.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#define INET_ERR_OUT_MSG_BOX_BUFFER_SIZE 512
#define INET_ERR_OUT_FORMAT_BUFFER_SIZE 256
#ifdef _UNICODE
#define _itot _itow_s
#else
#define _itot _itoa_s
#endif
// Forward declaration of helper function
void WINAPI addLastErrorToMsg(LPTSTR szMsgBuffer, DWORD dwSize);
// Function for displaying Internet Error information in a message box
BOOL WINAPI InternetErrorOut(
HWND hWnd,
DWORD dwError,
LPCTSTR szFailingFunctionName)
{
HANDLE hProcHeap;
TCHAR szMsgBoxBuffer[INET_ERR_OUT_MSG_BOX_BUFFER_SIZE],
szFormatBuffer[INET_ERR_OUT_FORMAT_BUFFER_SIZE],
szConnectiveText[] = { TEXT("\nAdditional Information: ") },
*szExtErrMsg = NULL,
*szCombinedErrMsg = NULL;
DWORD dwInetError,
dwBaseLength,
dwExtLength = 0;
if ((hProcHeap = GetProcessHeap()) == NULL)
{
StringCchCopy(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("Call to GetProcessHeap( ) failed..."));
goto InetErrorOutError_1;
}
dwBaseLength = FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE, // dwFlags
GetModuleHandle(TEXT("wininet.dll")), // lpSource
dwError, // dwMessageId
0, // dwLanguageId
(LPTSTR)szFormatBuffer, // lpBuffer
INET_ERR_OUT_FORMAT_BUFFER_SIZE, // nSize
NULL); // * arguments
if (!dwBaseLength)
{
StringCchCopy(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("Call to FormatMessage( ) failed..."));
addLastErrorToMsg(szMsgBoxBuffer,
INET_ERR_OUT_MSG_BOX_BUFFER_SIZE);
goto InetErrorOutError_1;
}
if (FAILED(StringCchPrintf(szMsgBoxBuffer,
INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("%s error; code: %d\nDescription: %s\n"),
szFailingFunctionName, dwError, szFormatBuffer)))
{
StringCchCopy(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("Call to StringCchPrintf( ) failed...\n"));
goto InetErrorOutError_1;
}
StringCchLength(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
(size_t*)&dwBaseLength);
// Adjust base-length value to count the number of bytes:
dwBaseLength *= sizeof(TCHAR);
if (dwError == ERROR_INTERNET_EXTENDED_ERROR)
{
InternetGetLastResponseInfo(&dwInetError, NULL, &dwExtLength);
// Adjust the extended-length value to a byte count
// that includes the terminating null:
++dwExtLength *= sizeof(TCHAR);
if ((szExtErrMsg = (TCHAR*)HeapAlloc(hProcHeap, 0, dwExtLength))
== NULL)
{
StringCchCat(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("\nFailure: Could not allocate buffer for addional details."));
addLastErrorToMsg(szMsgBoxBuffer,
INET_ERR_OUT_MSG_BOX_BUFFER_SIZE);
goto InetErrorOutError_1;
}
if (!InternetGetLastResponseInfo(&dwInetError,
szExtErrMsg,
&dwExtLength))
{
StringCchCat(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("\nCall to InternetGetLastResponseInfo( ) failed--"));
addLastErrorToMsg(szMsgBoxBuffer,
INET_ERR_OUT_MSG_BOX_BUFFER_SIZE);
goto InetErrorOutError_2;
}
dwBaseLength += dwExtLength + sizeof(szConnectiveText);
}
if ((szCombinedErrMsg = (TCHAR*)HeapAlloc(hProcHeap, 0,
dwBaseLength + sizeof(TCHAR)))
== NULL)
{
StringCchCat(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("\nFailure: Could not allocate final output buffer."));
addLastErrorToMsg(szMsgBoxBuffer,
INET_ERR_OUT_MSG_BOX_BUFFER_SIZE);
goto InetErrorOutError_2;
}
if (FAILED(StringCchCopy(szCombinedErrMsg,
dwBaseLength, szMsgBoxBuffer)) ||
(dwExtLength &&
(FAILED(StringCchCat(szCombinedErrMsg,
dwBaseLength, szConnectiveText)) ||
FAILED(StringCchCat(szCombinedErrMsg,
dwBaseLength, szExtErrMsg)))))
{
StringCchCat(szMsgBoxBuffer, INET_ERR_OUT_MSG_BOX_BUFFER_SIZE,
TEXT("\nFailure: Could not assemble final message."));
goto InetErrorOutError_3;
}
MessageBox(hWnd,
szCombinedErrMsg,
TEXT("Internet Error Message"),
MB_OK | MB_ICONERROR);
HeapFree(hProcHeap, 0, szExtErrMsg);
HeapFree(hProcHeap, 0, szCombinedErrMsg);
return(TRUE);
InetErrorOutError_3:
HeapFree(hProcHeap, 0, szCombinedErrMsg);
InetErrorOutError_2:
HeapFree(hProcHeap, 0, szExtErrMsg);
InetErrorOutError_1:
MessageBox(hWnd,
(LPCTSTR)szMsgBoxBuffer,
TEXT("InternetErrorOut( ) Failed"),
MB_OK | MB_ICONERROR);
return(FALSE);
}
void WINAPI addLastErrorToMsg(LPTSTR szMsgBuffer, DWORD dwSize)
{
TCHAR szNumberBuffer[32];
_itot(GetLastError(), szNumberBuffer, 10);
StringCchCat(szMsgBuffer, dwSize,
TEXT("\n System Error number: "));
StringCchCat(szMsgBuffer, dwSize, szNumberBuffer);
StringCchCat(szMsgBuffer, dwSize, TEXT(".\n"));
}
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}