-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmnHdr.h
318 lines (204 loc) · 8.87 KB
/
CmnHdr.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
/******************************************************************************
Module: CmnHdr.h
Notices: Copyright (c) 2000 Jeffrey Richter
Purpose: Common header file containing handy macros and definitions
used throughout all the applications in the book.
See Appendix A.
******************************************************************************/
#pragma once // Include this header file once per compilation unit
/////////////////////////// Force Windows subsystem ///////////////////////////
#pragma comment(linker, "/subsystem:Windows,5")
//////////////////////////// Unicode Build Option /////////////////////////////
// If we are not compiling for an x86 CPU, we always compile using Unicode.
#ifndef _M_IX86
#define UNICODE
#endif
// To compile without Unicode, comment out the line below.
#define UNICODE
// When using Unicode Windows functions, use Unicode C-Runtime functions too.
#ifdef UNICODE
#define _UNICODE
#endif
///////////////////////// Include Windows Definitions /////////////////////////
#pragma warning(push, 4)
#include <Windows.h>
#include <TChar.h>
///////////// Verify that the proper header files are being used //////////////
#ifndef WT_EXECUTEINPERSISTENTIOTHREAD
#pragma message("You are not using the latest Platform SDK header/library ")
#pragma message("files. This may prevent the project from building correctly.")
#pragma message("You may install the Platform SDK from the book's CD-ROM or ")
#pragma message("from http://msdn.microsoft.com/downloads/")
#endif
////////////// Allow code to compile cleanly at warning level 4 ///////////////
/* nonstandard extension 'single line comment' was used */
#pragma warning(disable:4001)
// unreferenced formal parameter
#pragma warning(disable:4100)
// Note: Creating precompiled header
#pragma warning(disable:4699)
// function not inlined
#pragma warning(disable:4710)
// unreferenced inline function has been removed
#pragma warning(disable:4514)
// assignment operator could not be generated
#pragma warning(disable:4512)
// cast truncates constant value
#pragma warning(disable:4310)
///////////////////////// Pragma message helper macro /////////////////////////
/*
When the compiler sees a line like this:
#pragma chMSG(Fix this later)
it outputs a line like this:
c:\CD\CmnHdr.h(82):Fix this later
You can easily jump directly to this line and examine the surrounding code.
*/
#define chSTR2(x) #x
#define chSTR(x) chSTR2(x)
#define chMSG(desc) message(__FILE__ "(" chSTR(__LINE__) "):" #desc)
////////////////////////////// chINRANGE Macro ////////////////////////////////
// This macro returns TRUE if a number is between two others
#define chINRANGE(low, Num, High) (((low) <= (Num)) && ((Num) <= (High)))
//////////////////////////////// chDIMOF Macro ////////////////////////////////
// This macro evaluates to the number of elements in an array.
#define chDIMOF(Array) (sizeof(Array) / sizeof(Array[0]))
///////////////////////////// chSIZEOFSTRING Macro ////////////////////////////
// This macro evaluates to the number of bytes needed by a string.
#define chSIZEOFSTRING(psz) ((lstrlen(psz) + 1) * sizeof(TCHAR))
/////////////////// chROUNDDOWN & chROUNDUP inline functions //////////////////
// This inline function rounds a value down to the nearest multiple
template <class TV, class TM>
inline TV chROUNDDOWN(TV Value, TM Multiple) {
return((Value / Multiple) * Multiple);
}
// This inline function rounds a value down to the nearest multiple
template <class TV, class TM>
inline TV chROUNDUP(TV Value, TM Multiple) {
return(chROUNDDOWN(Value, Multiple) +
(((Value % Multiple) > 0) ? Multiple : 0));
}
///////////////////////////// chBEGINTHREADEX Macro ///////////////////////////
// This macro function calls the C runtime's _beginthreadex function.
// The C runtime library doesn't want to have any reliance on Windows' data
// types such as HANDLE. This means that a Windows programmer needs to cast
// values when using _beginthreadex. Since this is terribly inconvenient,
// I created this macro to perform the casting.
typedef unsigned (__stdcall *PTHREAD_START) (void *);
#define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
pvParam, fdwCreate, pdwThreadId) \
((HANDLE)_beginthreadex( \
(void *) (psa), \
(unsigned) (cbStack), \
(PTHREAD_START) (pfnStartAddr), \
(void *) (pvParam), \
(unsigned) (fdwCreate), \
(unsigned *) (pdwThreadId)))
////////////////// DebugBreak Improvement for x86 platforms ///////////////////
#ifdef _X86_
#define DebugBreak() _asm { int 3 }
#endif
/////////////////////////// Software Exception Macro //////////////////////////
// Useful macro for creating your own software exception codes
#define MAKESOFTWAREEXCEPTION(Severity, Facility, Exception) \
((DWORD) ( \
/* Severity code */ (Severity ) | \
/* MS(0) or Cust(1) */ (1 << 29) | \
/* Reserved(0) */ (0 << 28) | \
/* Facility code */ (Facility << 16) | \
/* Exception code */ (Exception << 0)))
/////////////////////////// Quick MessageBox Macro ////////////////////////////
inline void chMB(PCSTR s) {
char szTMP[256];
GetModuleFileNameA(NULL, szTMP, chDIMOF(szTMP));
HWND hwnd = GetActiveWindow();
MessageBoxA(hwnd, s, szTMP,
MB_OK | ((hwnd == NULL ) ? MB_SERVICE_NOTIFICATION : 0));
}
//////////////////////////// Assert/Verify Macros /////////////////////////////
inline void chMBANDDEBUG(PSTR szMsg) {
chMB(szMsg);
DebugBreak();
}
// Put up an assertion failure message box.
inline void chASSERTFAIL(LPCSTR file, int line, PCSTR expr) {
char sz[256];
wsprintfA(sz, "File %s, line %d : %s", file, line, expr);
chMBANDDEBUG(sz);
}
// Put up a message box if an assertion fails in a debug build.
#ifdef _DEBUG
#define chASSERT(x) if (!(x)) chASSERTFAIL(__FILE__, __LINE__, #x)
#else
#define chASSERT(x)
#endif
// Put up a failure message box in a debug build.
#ifdef _DEBUG
#define chFAIL() chASSERTFAIL(__FILE__, __LINE__, "")
#else
#define chFAIL()
#endif
// Assert in debug builds, but don't remove the code in retail builds.
#ifdef _DEBUG
#define chVERIFY(x) chASSERT(x)
#else
#define chVERIFY(x) (x)
#endif
/////////////////////////// chHANDLE_DLGMSG Macro /////////////////////////////
// The normal HANDLE_MSG macro in WindowsX.h does not work properly for dialog
// boxes because DlgProc return a BOOL instead of an LRESULT (like
// WndProcs). This chHANDLE_DLGMSG macro corrects the problem:
#define chHANDLE_DLGMSG(hwnd, message, fn) \
case (message): return (SetDlgMsgResult(hwnd, uMsg, \
HANDLE_##message((hwnd), (wParam), (lParam), (fn))))
//////////////////////// Dialog Box Icon Setting Macro ////////////////////////
// Sets the dialog box icons
inline void chSETDLGICONS(HWND hwnd, int idi) {
SendMessage(hwnd, WM_SETICON, TRUE, (LPARAM)
LoadIcon((HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
MAKEINTRESOURCE(idi)));
SendMessage(hwnd, WM_SETICON, FALSE, (LPARAM)
LoadIcon((HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
MAKEINTRESOURCE(idi)));
}
///////////////////////////// UNICODE Check Macro /////////////////////////////
// Since Windows 98 does not support Unicode, issue an error and terminate
// the process if this is a native Unicode build running on Windows 98
// This is accomplished by creating a global C++ object. Its constructor is
// executed before WinMain/main.
#ifdef UNICODE
class CUnicodeSupported {
public:
CUnicodeSupported() {
if (GetWindowsDirectoryW(NULL, 0) <= 0) {
chMB("This application requires an OS that supports Unicode.");
ExitProcess(0);
}
}
};
// "static" stops the linker from complaining that multiple instances of the
// object exist when a single project contains multiple source files.
static CUnicodeSupported g_UnicodeSupported;
#endif
/////////////////////////// OS Version Check Macros ///////////////////////////
inline void chWindows9xNotAllowed() {
OSVERSIONINFO vi = { sizeof(vi) };
GetVersionEx(&vi);
if (vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
chMB("This application requires features not present in Windows 9x.");
ExitProcess(0);
}
}
inline void chWindows2000Required() {
OSVERSIONINFO vi = { sizeof(vi) };
GetVersionEx(&vi);
if ((vi.dwPlatformId != VER_PLATFORM_WIN32_NT) && (vi.dwMajorVersion < 5)) {
chMB("This application requires features present in Windows 2000.");
ExitProcess(0);
}
}
////////////////////////// SYSTEM_INFO Wrapper Class //////////////////////////
class CSystemInfo : public SYSTEM_INFO {
public:
CSystemInfo() { GetSystemInfo(this); }
};
///////////////////////////////// End of File /////////////////////////////////