-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBTAtlWindow.h
359 lines (334 loc) · 13.2 KB
/
BTAtlWindow.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* This is a part of the BugTrap package.
* Copyright (c) 2005-2009 IntelleSoft.
* All rights reserved.
*
* Description: This class provides better error handling for ATL/WTL windows.
* Author: Maksim Pyatkovskiy.
*
* This source code is only intended as a supplement to the
* BugTrap package reference and related electronic documentation
* provided with the product. See these sources for detailed
* information regarding the BugTrap package.
*/
#ifndef _BTATLWINDOW_H_
#define _BTATLWINDOW_H_
#pragma once
#ifndef _BUGTRAP_H_
#error Include BugTrap.h first
#endif // _BUGTRAP_H_
#ifndef __cplusplus
#error C++ compiler is required
#endif // __cplusplus
#ifndef __ATLWIN_H__
#error This class cannot be used in non ATL applications
#endif // __ATLWIN_H__
namespace ATL
{
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
#define _BTWND_INITIALIZER_ : m_pfnFilter(&BT_SehFilter), m_pfnBaseWndProc(BASE_CLASS::GetWindowProc()), BASE_CLASS
#else
#define _BTWND_INITIALIZER_ : m_pfnBaseWndProc(BASE_CLASS::GetWindowProc()), BASE_CLASS
#endif // _ATL_NO_EXCEPTIONS && ! _EXCEPTION_
/// This class adds error handling to ATL/WTL windows.
/// Only one form of exception handling is permitted per function,
/// therefore this class uses two functions to catch C++ and Windows errors.
template <class BASE_CLASS>
class BTWindow : public BASE_CLASS {
protected:
/// Object initialization (0 parameters).
BTWindow(void) _BTWND_INITIALIZER_ () { }
/// Object initialization (1 parameter).
template <typename T1>
explicit BTWindow(T1 param1) _BTWND_INITIALIZER_ (param1) { }
/// Object initialization (2 parameters)
template <typename T1, typename T2>
BTWindow(T1 param1, T2 param2) _BTWND_INITIALIZER_ (param1, param2) { }
/// Object initialization (3 parameters).
template <typename T1, typename T2, typename T3>
BTWindow(T1 param1, T2 param2, T3 param3) _BTWND_INITIALIZER_ (param1, param2, param3) { }
/// Object initialization (4 parameters).
template <typename T1, typename T2, typename T3, typename T4>
BTWindow(T1 param1, T2 param2, T3 param3, T4 param4) _BTWND_INITIALIZER_ (param1, param2, param3, param4) { }
/// Object initialization (5 parameters).
template <typename T1, typename T2, typename T3, typename T4, typename T5>
BTWindow(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5) _BTWND_INITIALIZER_ (param1, param2, param3, param4, param5) { }
/// This window procedure uses SEH to intercept all unhandled exceptions.
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/// Overridden to return address of custom window procedure.
virtual WNDPROC GetWindowProc(void);
private:
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
/// This window procedure intercepts C++ exceptions.
LRESULT CppExceptionHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/// Exception filter.
LONG (CALLBACK * m_pfnFilter)(PEXCEPTION_POINTERS pExceptionPointers);
#ifdef _M_X64
/// This window procedure saves exception context.
LRESULT SaveExceptionContext(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/// Exception context.
EXCEPTION_POINTERS m_ExceptionPointers;
#endif // _M_X64
#endif // _ATL_NO_EXCEPTIONS && ! _EXCEPTION_
/// Cached address of base window procedure.
WNDPROC m_pfnBaseWndProc;
};
#undef _BTWND_INITIALIZER_
/**
* @return address of window procedure.
*/
template <class BASE_CLASS>
inline WNDPROC BTWindow<BASE_CLASS>::GetWindowProc()
{
return &WindowProc;
}
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
#ifdef _M_X64
/**
* @param hWnd - window handle.
* @param uMsg - specifies the Windows message to be processed.
* @param wParam - provides additional information used in message processing.
* @param lParam - provides additional information used in message processing.
* @return the return value depends on the message.
*/
template <class BASE_CLASS>
LRESULT BTWindow<BASE_CLASS>::SaveExceptionContext(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
__try {
return (*m_pfnBaseWndProc)(hWnd, uMsg, wParam, lParam);
} __except (CopyMemory(&m_ExceptionPointers, GetExceptionInformation(), sizeof(m_ExceptionPointers)), EXCEPTION_CONTINUE_SEARCH) {
return 0;
}
}
#endif // _M_X64
/**
* @param hWnd - window handle.
* @param uMsg - specifies the Windows message to be processed.
* @param wParam - provides additional information used in message processing.
* @param lParam - provides additional information used in message processing.
* @return the return value depends on the message.
*/
template <class BASE_CLASS>
LRESULT BTWindow<BASE_CLASS>::CppExceptionHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
try {
#ifdef _M_X64
return SaveExceptionContext(hWnd, uMsg, wParam, lParam);
#else
return (*m_pfnBaseWndProc)(hWnd, uMsg, wParam, lParam);
#endif// ! _M_X64
}
#ifndef _ATL_NO_EXCEPTIONS
catch (CAtlException& rException) {
BT_SetUserMessageFromCode(rException.m_hr);
// an exception will be caught by SEH handler
throw;
}
#endif // _ATL_NO_EXCEPTIONS
#ifdef _EXCEPTION_
catch (std::exception& rException) {
// extract error message
const CHAR* pszErrorMessageA = rException.what();
if (pszErrorMessageA != NULL && *pszErrorMessageA != '\0') {
#ifdef _UNICODE
DWORD dwErrorMessageSizeW = MultiByteToWideChar(CP_ACP, 0, pszErrorMessageA, -1, NULL, 0);
// alloca() cannot be used in catch block
WCHAR* pszErrorMessageW = (WCHAR*)malloc(dwErrorMessageSizeW * sizeof(WCHAR));
if (pszErrorMessageW != NULL) {
MultiByteToWideChar(CP_ACP, 0, pszErrorMessageA, -1, pszErrorMessageW, dwErrorMessageSizeW);
BT_SetUserMessage(pszErrorMessageW);
free(pszErrorMessageW);
}
#else
BT_SetUserMessage(pszErrorMessageA);
#endif // ! _UNICODE
}
m_pfnFilter = &BT_CppFilter;
// an exception will be caught by SEH handler
throw;
}
#endif // _EXCEPTION_
}
#endif // ! _ATL_NO_EXCEPTIONS || _EXCEPTION_
/**
* @param hWnd - window handle.
* @param uMsg - specifies the Windows message to be processed.
* @param wParam - provides additional information used in message processing.
* @param lParam - provides additional information used in message processing.
* @return the return value depends on the message.
*/
template <class BASE_CLASS>
LRESULT BTWindow<BASE_CLASS>::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
// This cast requires direct inheritance from CWindowImplBase, i.e.
// CWindowImplBase descendant should be declared first in the list of base classes.
// More generic solution would require additional template arguments.
BTWindow* pThis = (BTWindow*)hWnd;
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
__try {
return pThis->CppExceptionHandler(hWnd, uMsg, wParam, lParam);
} __except ((*pThis->m_pfnFilter)(GetExceptionInformation())) {
pThis->m_pfnFilter = &BT_SehFilter;
return 0;
}
#else
__try {
return (*pThis->m_pfnBaseWndProc)(hWnd, uMsg, wParam, lParam);
} __except (BT_SehFilter(GetExceptionInformation())) {
return 0;
}
#endif // _ATL_NO_EXCEPTIONS && ! _EXCEPTION_
}
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
#define _BTDLG_INITIALIZER_ : m_pfnFilter(&BT_SehFilter), m_pfnBaseDlgProc(BASE_CLASS::GetDialogProc()), BASE_CLASS
#else
#define _BTDLG_INITIALIZER_ : m_pfnBaseDlgProc(BASE_CLASS::GetDialogProc()), BASE_CLASS
#endif // _ATL_NO_EXCEPTIONS && ! _EXCEPTION_
/// This class adds error handling to ATL/WTL dialogs.
/// Only one form of exception handling is permitted per function,
/// therefore this class uses two functions to catch C++ and Windows errors.
template <class BASE_CLASS>
class BTDialog : public BASE_CLASS {
protected:
/// Object initialization (0 parameters).
BTDialog(void) _BTDLG_INITIALIZER_ () { }
/// Object initialization (1 parameter).
template <typename T1>
explicit BTDialog(T1 param1) _BTDLG_INITIALIZER_ (param1) { }
/// Object initialization (2 parameters)
template <typename T1, typename T2>
BTDialog(T1 param1, T2 param2) _BTDLG_INITIALIZER_ (param1, param2) { }
/// Object initialization (3 parameters).
template <typename T1, typename T2, typename T3>
BTDialog(T1 param1, T2 param2, T3 param3) _BTDLG_INITIALIZER_ (param1, param2, param3) { }
/// Object initialization (4 parameters).
template <typename T1, typename T2, typename T3, typename T4>
BTDialog(T1 param1, T2 param2, T3 param3, T4 param4) _BTDLG_INITIALIZER_ (param1, param2, param3, param4) { }
/// Object initialization (5 parameters).
template <typename T1, typename T2, typename T3, typename T4, typename T5>
BTDialog(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5) _BTDLG_INITIALIZER_ (param1, param2, param3, param4, param5) { }
/// This dialog procedure uses SEH to intercept all unhandled exceptions.
static INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/// Overridden to return address of custom dialog procedure.
virtual DLGPROC GetDialogProc(void);
private:
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
/// This dialog procedure intercepts C++ exceptions.
INT_PTR CppExceptionHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/// Exception filter.
LONG (CALLBACK * m_pfnFilter)(PEXCEPTION_POINTERS pExceptionPointers);
#ifdef _M_X64
/// This window procedure saves exception context.
INT_PTR SaveExceptionContext(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/// Exception context.
EXCEPTION_POINTERS m_ExceptionPointers;
#endif // _M_X64
#endif // _ATL_NO_EXCEPTIONS && ! _EXCEPTION_
/// Cached address of base dialog procedure.
DLGPROC m_pfnBaseDlgProc;
};
#undef _BTDLG_INITIALIZER_
/**
* @return address of dialog procedure.
*/
template <class BASE_CLASS>
inline DLGPROC BTDialog<BASE_CLASS>::GetDialogProc()
{
return &DialogProc;
}
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
#ifdef _M_X64
/**
* @param hWnd - window handle.
* @param uMsg - specifies the Windows message to be processed.
* @param wParam - provides additional information used in message processing.
* @param lParam - provides additional information used in message processing.
* @return the return value depends on the message.
*/
template <class BASE_CLASS>
INT_PTR BTDialog<BASE_CLASS>::SaveExceptionContext(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
__try {
return (*m_pfnBaseDlgProc)(hWnd, uMsg, wParam, lParam);
} __except (CopyMemory(&m_ExceptionPointers, GetExceptionInformation(), sizeof(m_ExceptionPointers)), EXCEPTION_CONTINUE_SEARCH) {
return 0;
}
}
#endif // _M_X64
/**
* @param hWnd - window handle.
* @param uMsg - specifies the Windows message to be processed.
* @param wParam - provides additional information used in message processing.
* @param lParam - provides additional information used in message processing.
* @return the return value depends on the message.
*/
template <class BASE_CLASS>
INT_PTR BTDialog<BASE_CLASS>::CppExceptionHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
try {
#ifdef _M_X64
return SaveExceptionContext(hWnd, uMsg, wParam, lParam);
#else
return (*m_pfnBaseDlgProc)(hWnd, uMsg, wParam, lParam);
#endif// ! _M_X64
}
#ifndef _ATL_NO_EXCEPTIONS
catch (CAtlException& rException) {
BT_SetUserMessageFromCode(rException.m_hr);
// an exception will be caught by SEH handler
throw;
}
#endif // _ATL_NO_EXCEPTIONS
#ifdef _EXCEPTION_
catch (std::exception& rException) {
// extract error message
const CHAR* pszErrorMessageA = rException.what();
if (pszErrorMessageA != NULL && *pszErrorMessageA != '\0') {
#ifdef _UNICODE
DWORD dwErrorMessageSizeW = MultiByteToWideChar(CP_ACP, 0, pszErrorMessageA, -1, NULL, 0);
// alloca() cannot be used in catch block
WCHAR* pszErrorMessageW = (WCHAR*)malloc(dwErrorMessageSizeW * sizeof(WCHAR));
if (pszErrorMessageW != NULL) {
MultiByteToWideChar(CP_ACP, 0, pszErrorMessageA, -1, pszErrorMessageW, dwErrorMessageSizeW);
BT_SetUserMessage(pszErrorMessageW);
free(pszErrorMessageW);
}
#else
BT_SetUserMessage(pszErrorMessageA);
#endif // ! _UNICODE
}
m_pfnFilter = &BT_CppFilter;
// an exception will be caught by SEH handler
throw;
}
#endif // _EXCEPTION_
}
#endif // ! _ATL_NO_EXCEPTIONS || _EXCEPTION_
/**
* @param hWnd - window handle.
* @param uMsg - specifies the Windows message to be processed.
* @param wParam - provides additional information used in message processing.
* @param lParam - provides additional information used in message processing.
* @return the return value depends on the message.
*/
template <class BASE_CLASS>
INT_PTR BTDialog<BASE_CLASS>::DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
// This cast requires direct inheritance from CDialogImplBase, i.e.
// CDialogImplBase descendant should be declared first in the list of base classes.
// More generic solution would require additional template arguments.
BTDialog* pThis = (BTDialog*)hWnd;
#if ! defined _ATL_NO_EXCEPTIONS || defined _EXCEPTION_
__try {
return pThis->CppExceptionHandler(hWnd, uMsg, wParam, lParam);
} __except ((*pThis->m_pfnFilter)(GetExceptionInformation())) {
pThis->m_pfnFilter = &BT_SehFilter;
return 0;
}
#else
__try {
return (*pThis->m_pfnBaseDlgProc)(hWnd, uMsg, wParam, lParam);
} __except (BT_SehFilter(GetExceptionInformation())) {
return 0;
}
#endif // _ATL_NO_EXCEPTIONS && ! _EXCEPTION_
}
}
#ifndef BT_DO_NOT_USE_DEFAULT_NAMESPACES
using namespace ATL;
#endif // BT_DO_NOT_USE_DEFAULT_NAMESPACES
#endif // _BTATLWINDOW_H_