home *** CD-ROM | disk | FTP | other *** search
/ PC World 1999 October / PCWorld_1999-10_cd1.bin / Hardware / Drivers / APISpy / CmnHdr.h < prev    next >
C/C++ Source or Header  |  1996-10-09  |  9KB  |  242 lines

  1. /******************************************************************************
  2. Module name: CmnHdr.h
  3. Written by: Jeffrey Richter
  4. Notices: Copyright (c) 1995-1997 Jeffrey Richter
  5. Purpose: Common header file containing handy macros and definitions used
  6.          throughout all the applications in the book.
  7. ******************************************************************************/
  8.  
  9.  
  10. /* Disable ridiculous warnings so that the code */
  11. /* compiles cleanly using warning level 4.      */
  12.  
  13. /* nonstandard extension 'single line comment' was used */
  14. #pragma warning(disable: 4001)
  15.  
  16. // nonstandard extension used : nameless struct/union
  17. #pragma warning(disable: 4201)
  18.  
  19. // nonstandard extension used : bit field types other than int
  20. #pragma warning(disable: 4214)
  21.  
  22. // Note: Creating precompiled header 
  23. #pragma warning(disable: 4699)
  24.  
  25. // unreferenced inline function has been removed
  26. #pragma warning(disable: 4514)
  27.  
  28. // unreferenced formal parameter
  29. #pragma warning(disable: 4100)
  30.  
  31. // indirection to slightly different base types
  32. #pragma warning(disable: 4057)
  33.  
  34. // named type definition in parentheses
  35. #pragma warning(disable: 4115)
  36.  
  37. // nonstandard extension used : benign typedef redefinition
  38. #pragma warning(disable: 4209)
  39.  
  40.  
  41. //////////////////////// Windows Version Build Option /////////////////////////
  42.  
  43.  
  44. #define _WIN32_WINNT 0x0400
  45.  
  46.  
  47. ///////////////////////////// STRICT Build Option /////////////////////////////
  48.  
  49.  
  50. // Force all EXEs/DLLs to use STRICT type checking.
  51. #define STRICT
  52.  
  53.  
  54. /////////////////////////// CPU Portability Macros ////////////////////////////
  55.  
  56.  
  57. // If no CPU platform was specified, default to the current platform.
  58. #if !defined(_PPC_) && !defined(_ALPHA_) && !defined(_MIPS_) && !defined(_X86_)
  59.    #if defined(_M_IX86)
  60.       #define _X86_
  61.    #endif
  62.    #if defined(_M_MRX000)
  63.       #define _MIPS_
  64.    #endif
  65.    #if defined(_M_ALPHA)
  66.       #define _ALPHA_
  67.    #endif
  68.    #if defined(_M_PPC)
  69.       #define _PPC_
  70.    #endif
  71. #endif
  72.  
  73.  
  74. //////////////////////////// Unicode Build Option /////////////////////////////
  75.  
  76.  
  77. // If we are not compiling for an x86 CPU, we always compile using Unicode.
  78. #ifndef _X86_
  79. #define UNICODE
  80. #endif
  81.  
  82.  
  83. // To compile using Unicode on the x86 CPU, uncomment the line below.
  84. //#define UNICODE
  85.  
  86. // When using Unicode Win32 functions, use Unicode C-Runtime functions too.
  87. #ifdef UNICODE
  88. #define _UNICODE
  89. #endif
  90.  
  91.  
  92. //////////////////////////////// chDIMOF Macro ////////////////////////////////
  93.  
  94.  
  95. // This macro evaluates to the number of elements in an array. 
  96. #define chDIMOF(Array) (sizeof(Array) / sizeof(Array[0]))
  97.  
  98.  
  99. ///////////////////////////// chBEGINTHREADEX Macro ///////////////////////////
  100.  
  101.  
  102. // Create a chBEGINTHREADEX macro that calls the C run-time's
  103. // _beginthreadex function. The C run-time library doesn't
  104. // want to have any reliance on Win32 data types such as
  105. // HANDLE. This means that a Win32 programmer needs to cast
  106. // the return value to a HANDLE. This is terribly inconvenient,
  107. // so I have created this macro to perform the casting.
  108. typedef unsigned (__stdcall *PTHREAD_START) (void *);
  109.  
  110. #define chBEGINTHREADEX(lpsa, cbStack, lpStartAddr, \
  111.    lpvThreadParm, fdwCreate, lpIDThread)            \
  112.       ((HANDLE)_beginthreadex(                      \
  113.          (void *) (lpsa),                           \
  114.          (unsigned) (cbStack),                      \
  115.          (PTHREAD_START) (lpStartAddr),             \
  116.          (void *) (lpvThreadParm),                  \
  117.          (unsigned) (fdwCreate),                    \
  118.          (unsigned *) (lpIDThread)))
  119.  
  120.  
  121. //////////////////////////// Assert/Verify Macros /////////////////////////////
  122.  
  123.  
  124. #define chFAIL(szMSG) {                                                   \
  125.       MessageBox(GetActiveWindow(), szMSG,                                \
  126.          __TEXT("Assertion Failed"), MB_OK | MB_ICONERROR);               \
  127.       DebugBreak();                                                       \
  128.    }
  129.  
  130. // Put up an assertion failure message box.
  131. #define chASSERTFAIL(file,line,expr) {                                    \
  132.       TCHAR sz[128];                                                      \
  133.       wsprintf(sz, __TEXT("File %hs, line %d : %hs"), file, line, expr);  \
  134.       chFAIL(sz);                                                         \
  135.    }
  136.  
  137. // Put up a message box if an assertion fails in a debug build.
  138. #ifdef _DEBUG
  139. #define chASSERT(x) if (!(x)) chASSERTFAIL(__FILE__, __LINE__, #x)
  140. #else
  141. #define chASSERT(x)
  142. #endif
  143.  
  144. // Assert in debug builds, but don't remove the code in retail builds.
  145. #ifdef _DEBUG
  146. #define chVERIFY(x) chASSERT(x)
  147. #else
  148. #define chVERIFY(x) (x)
  149. #endif
  150.  
  151.  
  152. /////////////////////////// chHANDLE_DLGMSG Macro /////////////////////////////
  153.  
  154.  
  155. // The normal HANDLE_MSG macro in WINDOWSX.H does not work properly for dialog
  156. // boxes because DlgProc return a BOOL instead of an LRESULT (like
  157. // WndProcs). This chHANDLE_DLGMSG macro corrects the problem:
  158. #define chHANDLE_DLGMSG(hwnd, message, fn)                           \
  159.    case (message): return (SetDlgMsgResult(hwnd, uMsg,               \
  160.       HANDLE_##message((hwnd), (wParam), (lParam), (fn))))
  161.  
  162.  
  163. /////////////////////////// Quick MessageBox Macro ////////////////////////////
  164.  
  165.  
  166. #define chMB(s) {                                                    \
  167.       TCHAR szTMP[128];                                              \
  168.       GetModuleFileName(NULL, szTMP, chDIMOF(szTMP));                \
  169.       MessageBox(GetActiveWindow(), s, szTMP, MB_OK);                \
  170.    }
  171.  
  172.  
  173. ///////////////////////////// Zero Variable Macro /////////////////////////////
  174.  
  175.  
  176. // Zero out a structure. If fInitSize is TRUE, initialize the first int to
  177. // the size of the structure. Many structures like WNDCLASSEX and STARTUPINFO
  178. // require that their first member be set to the size of the structure itself.
  179. #define chINITSTRUCT(structure, fInitSize)                           \
  180.    (ZeroMemory(&(structure), sizeof(structure)),                     \
  181.    fInitSize ? (*(int*) &(structure) = sizeof(structure)) : 0)
  182.  
  183.  
  184. //////////////////////// Dialog Box Icon Setting Macro ////////////////////////
  185.  
  186.  
  187. // The call to SetClassLong is for Windows NT 3.51 or less.  The WM_SETICON
  188. // messages are for Windows NT 4.0 and Windows 95.
  189. #define chSETDLGICONS(hwnd, idiLarge, idiSmall)                               \
  190.    {                                                                          \
  191.       OSVERSIONINFO VerInfo;                                                  \
  192.       chINITSTRUCT(VerInfo, TRUE);                                            \
  193.       GetVersionEx(&VerInfo);                                                 \
  194.       if ((VerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&                  \
  195.           (VerInfo.dwMajorVersion <= 3 && VerInfo.dwMinorVersion <= 51)) {    \
  196.          SetClassLong(hwnd, GCL_HICON, (LONG)                                 \
  197.             LoadIcon(GetWindowInstance(hwnd), MAKEINTRESOURCE(idiLarge)));    \
  198.       } else {                                                                \
  199.          SendMessage(hwnd, WM_SETICON, TRUE,  (LPARAM)                        \
  200.             LoadIcon(GetWindowInstance(hwnd), MAKEINTRESOURCE(idiLarge)));    \
  201.          SendMessage(hwnd, WM_SETICON, FALSE, (LPARAM)                        \
  202.             LoadIcon(GetWindowInstance(hwnd), MAKEINTRESOURCE(idiSmall)));    \
  203.       }                                                                       \
  204.    }
  205.     
  206.  
  207. ///////////////////////////// UNICODE Check Macro /////////////////////////////
  208.  
  209.  
  210. #ifdef UNICODE
  211.  
  212. #define chWARNIFUNICODEUNDERWIN95()                                        \
  213.    if (GetWindowsDirectoryW(NULL, 0) <= 0)                                 \
  214.       MessageBoxA(NULL, "This operating system doesn't support Unicode.",  \
  215.          NULL, MB_OK)
  216.  
  217. #else
  218.  
  219. #define chWARNIFUNICODEUNDERWIN95()
  220.  
  221. #endif
  222.  
  223.  
  224. ///////////////////////// Pragma message helper macro /////////////////////////
  225.  
  226.  
  227. /* 
  228. When the compiler sees a line like this:
  229. #pragma chMSG(Fix this later)
  230.  
  231. it outputs a line like this:
  232. C:\Document\AdvWin\Code\Sysinfo.06\..\CmnHdr.H(296):Fix this later
  233.  
  234. You can easily jump directly to this line and examine the surrounding code.
  235. */
  236. #define chSTR(x)       #x
  237. #define chSTR2(x)    chSTR(x)
  238. #define chMSG(desc) message(__FILE__ "(" chSTR2(__LINE__) "):" #desc)
  239.  
  240.  
  241. ///////////////////////////////// End of File /////////////////////////////////
  242.