Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.8 KiB

  1. #ifndef __WMSSTD_H__
  2. #define __WMSSTD_H__
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. #include <windows.h>
  7. #include <string.h>
  8. #include <tchar.h>
  9. #include <winnls.h>
  10. /////////////////////////////////////////////////////////////////////////////////////////
  11. /////////////////////////////// GENERIC MACROS //////////////////////////////////////////
  12. /////////////////////////////////////////////////////////////////////////////////////////
  13. #define MAX(a, b) (max((a), (b)))
  14. #define MIN(a, b) (min((a), (b)))
  15. #define MAKEULONGLONG(a, b) ((ULONGLONG)(((DWORD)(a)) | ((ULONGLONG)((DWORD)(b))) << 32))
  16. #define MAKEUINT64(a, b) MAKEULONGLONG((a),(b))
  17. #define MAKEQWORD(a, b) MAKEULONGLONG((a),(b))
  18. #define LODWORD(l) ((DWORD)(l))
  19. #define HIDWORD(l) ((DWORD)(((ULONGLONG)(l) >> 32) & 0xFFFFFFFF))
  20. #if !defined(COUNTOF) // macro to obtain the storage size of an array.
  21. #define COUNTOF( x ) ( sizeof(x) / sizeof( (x)[0] ) )
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////////////////
  24. /////////////////////////////// WINSOCK MACROS //////////////////////////////////////////
  25. /////////////////////////////////////////////////////////////////////////////////////////
  26. #define NTOHULL(ull) MAKEULONGLONG(ntohl(LODWORD((ull))), ntohl(HIDWORD((ull))))
  27. #define HTONULL(ull) MAKEULONGLONG(htonl(LODWORD((ull))), htonl(HIDWORD((ull))))
  28. /////////////////////////////////////////////////////////////////////////////////////////
  29. /////////////////////////////////// TYPES ///////////////////////////////////////////////
  30. /////////////////////////////////////////////////////////////////////////////////////////
  31. typedef ULONGLONG QWORD;
  32. typedef QWORD *LPQWORD;
  33. #ifndef _WIN64
  34. typedef ULONG ULONG_PTR;
  35. typedef LONG LONG_PTR;
  36. #endif // _WIN64
  37. /////////////////////////////////////////////////////////////////////////////////////////
  38. /////////////////////////////// COMMENT MACROS //////////////////////////////////////////
  39. /////////////////////////////////////////////////////////////////////////////////////////
  40. #define PUBLIC
  41. #define PROTECTED
  42. #define PRIVATE
  43. #define STATIC
  44. #define VIRTUAL
  45. /////////////////////////////////////////////////////////////////////////////////////////
  46. //////////////////////////// DEBUG/RETAIL MACROS ////////////////////////////////////////
  47. /////////////////////////////////////////////////////////////////////////////////////////
  48. #if DBG
  49. #define Debug(s) s // Statement/declaration included only if DBG
  50. #define Retail(s) // Statement/declaration included only if !DBG
  51. #define DebugOrRetail(d, r) d // d if DBG; r if !DBG
  52. #else // !DBG
  53. #define Debug(s) // Statement/declaration included only if DBG
  54. #define Retail(s) s // Statement/declaration included only if !DBG
  55. #define DebugOrRetail(d, r) r // d if DBG; r if !DBG
  56. #endif // !DBG
  57. /////////////////////////////////////////////////////////////////////////////////////////
  58. /////////////////////////////// STRING MACROS ///////////////////////////////////////////
  59. /////////////////////////////////////////////////////////////////////////////////////////
  60. #if UNICODE
  61. #define Unicode(s) (s)
  62. #define Ansi(s)
  63. #define UnicodeOrAnsi(u, a) (u)
  64. #define AnsiOrUnicode(a, u) (u)
  65. #else
  66. #define Unicode(s)
  67. #define Ansi(s) (s)
  68. #define UnicodeOrAnsi(u, a) (a)
  69. #define AnsiOrUnicode(a, u) (a)
  70. #endif
  71. #define WcharToAchar(a, w, l) \
  72. WideCharToMultiByte( \
  73. CP_ACP, \
  74. 0, \
  75. (w), \
  76. -1, \
  77. (a), \
  78. (l), \
  79. NULL, \
  80. NULL \
  81. )
  82. #define AcharToWchar(w, a, l) \
  83. MultiByteToWideChar( \
  84. CP_ACP, \
  85. 0, \
  86. (a), \
  87. -1, \
  88. (w), \
  89. (l) \
  90. )
  91. #define WcharToTchar(t, w, l) UnicodeOrAnsi(wcscpy((t), (w)), WcharToAchar((t), (w), (l)))
  92. #define WcharToTcharEx(t, w, l, p) UnicodeOrAnsi((p) = (w), (WcharToAchar((t), (w), (l)), (p) = (t)))
  93. #define TcharToWchar(w, t, l) UnicodeOrAnsi(wcscpy((w), (t)), AcharToWchar((w), (t), (l)))
  94. #define TcharToWcharEx(w, t, l, p) UnicodeOrAnsi((p) = (t), (AcharToWchar((w), (t), (l)), (p) = (w)))
  95. #define AcharToTchar(t, a, l) AnsiOrUnicode(strcpy((t), (a)), AcharToWchar((t), (a), (l)))
  96. #define AcharToTcharEx(t, a, l, p) AnsiOrUnicode((p) = (a), (AcharToWchar((t), (a), (l)), (p) = (t)))
  97. #define TcharToAchar(a, t, l) AnsiOrUnicode(strcpy((a), (t)), WcharToAchar((a), (t), (l)))
  98. #define TcharToAcharEx(a, t, l, p) AnsiOrUnicode((p) = (t), (WcharToAchar((a), (t), (l)), (p) = (a)))
  99. /////////////////////////////////////////////////////////////////////////////////////////
  100. //////////////////////// REFCOUNT AND SAFE_* MACROS /////////////////////////////////////
  101. /////////////////////////////////////////////////////////////////////////////////////////
  102. #ifdef INC_REF
  103. #undef INC_REF
  104. #endif
  105. #ifdef DEC_REF
  106. #undef DEC_REF
  107. #endif
  108. #ifdef SINGLE_THREADED
  109. #define INC_REF(u) (++u)
  110. #define DEC_REF(u) (--u)
  111. #else // !SINGLE_THREADED
  112. #define INC_REF(u) InterlockedIncrement((LONG*)&u)
  113. #define DEC_REF(u) InterlockedDecrement((LONG*)&u)
  114. #endif // SINGLE_THREADED
  115. #ifndef _REF_ASSERT
  116. #ifdef NO_DEBUG_SYNCHRO
  117. #define _REF_ASSERT(x)
  118. #else // !NO_DEBUG_SYNCHRO
  119. #ifdef USE_MFC
  120. #define _REF_ASSERT(x) ASSERT(x)
  121. #else // !USE_MFC
  122. #include <assert.h>
  123. #define _REF_ASSERT(x) assert(x)
  124. #endif // USE_MFC
  125. #endif // NO_DEBUG_SYNCHRO
  126. #endif
  127. #ifdef WIN32
  128. #define YIELD(hwnd,reps) (void)0
  129. #else
  130. #define YIELD(hwnd,reps) \
  131. {MSG msg;int n=(int)(reps);while( n-- && PeekMessage( &msg, hwnd, 0, 0, PM_REMOVE ) ){TranslateMessage( &msg );DispatchMessage( &msg );}}
  132. #endif
  133. #define SAFE_ADDREF(p) if( NULL != p ) { ( p )->AddRef(); }
  134. #define SAFE_DELETE(p) if( NULL != p ) { delete p; p = NULL; }
  135. #define SAFE_ARRAY_DELETE(p) if( NULL != p ) { delete [] p; p = NULL; }
  136. #define SAFE_RELEASE(p) if( NULL != p ) { ( p )->Release(); p = NULL; }
  137. #define SAFE_SHUTRELEASE(p) if( NULL != p ) { ( p )->Shutdown(); ( p )->Release(); p = NULL; }
  138. #define SAFE_COTASKMEMFREE(p) if( NULL != p ) { CoTaskMemFree( p ); p = NULL; }
  139. #define SAFE_SYSFREESTRING(p) if( NULL != p ) { SysFreeString( p ); p = NULL; }
  140. #define SAFE_ARRAYDELETE(p) if( NULL != p ) { delete [] p; p = NULL; }
  141. #define SAFE_CLOSEHANDLE( h ) if( NULL != h ) { CloseHandle( h ); h = NULL; }
  142. #define SAFE_CLOSEFILEHANDLE( h ) if( INVALID_HANDLE_VALUE != h ) { CloseHandle( h ); h = INVALID_HANDLE_VALUE; }
  143. #define SAFE_GLOBALFREE(p) if( NULL != p ) { GlobalFree( p ); p = NULL; }
  144. //
  145. // Ref-count safe assignment of 'src' to 'dst'
  146. //
  147. #define SAFE_REPLACEREF( dst, src ) \
  148. { \
  149. if( NULL != ( src ) ) \
  150. { \
  151. ( src )->AddRef( ); \
  152. } \
  153. if( NULL != ( dst ) ) \
  154. { \
  155. ( dst )->Release( ); \
  156. } \
  157. dst = src; \
  158. }
  159. /////////////////////////////////////////////////////////////////////////////////////////
  160. /////////////////////// OUTPARAMETER VALIDATION AND INITIALIZATION //////////////////////
  161. /////////////////////////////////////////////////////////////////////////////////////////
  162. #ifdef __tagVARIANT
  163. inline HRESULT ValidateOutPtr( VARIANT* pDest, VARIANT& /*bogus*/ )
  164. {
  165. if( reinterpret_cast< VARIANT* >( NULL ) == pDest )
  166. {
  167. return( E_POINTER );
  168. }
  169. ::VariantClear( pDest );
  170. return( S_OK );
  171. }
  172. #endif
  173. template< class Tptr, class T > HRESULT ValidateOutPtr( Tptr& pVal, T&/* bogus*/ )
  174. {
  175. if( reinterpret_cast< Tptr >( NULL ) == pVal )
  176. {
  177. return( E_POINTER );
  178. }
  179. *pVal = static_cast< T >( 0 );
  180. return( S_OK );
  181. }
  182. // Validate a pointer argument and initialize it if valid.
  183. #define VALIDOUT_PTR( pVal ) \
  184. { \
  185. HRESULT hr = ValidateOutPtr( pVal, *pVal ); \
  186. if( FAILED( hr ) ) \
  187. { \
  188. return( hr ); \
  189. } \
  190. }
  191. /////////////////////////////////////////////////////////////////////////////////////////
  192. /////////////////////// UTILITY MACROS//////////////////////
  193. /////////////////////////////////////////////////////////////////////////////////////////
  194. #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
  195. #endif // __WMSSTD_H__