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.

300 lines
8.4 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // Common.h
  7. //
  8. // Description:
  9. // Common definitions.
  10. //
  11. // Maintained By:
  12. // David Potter (DavidP) 14-DEC-1999
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #pragma once
  16. //////////////////////////////////////////////////////////////////////////////
  17. // Macro Definitions
  18. //////////////////////////////////////////////////////////////////////////////
  19. #if !defined( ARRAYSIZE )
  20. #define ARRAYSIZE( _x ) RTL_NUMBER_OF( _x )
  21. #define NULLTERMINATEARRAY( _x ) ( _x[ RTL_NUMBER_OF( _x ) - 1 ] = NULL )
  22. #endif // ! defined( ARRAYSIZE )
  23. #if !defined( PtrToByteOffset )
  24. #define PtrToByteOffset(base, offset) (((LPBYTE)base)+offset)
  25. #endif // !defined( PtrToByteOffset )
  26. //
  27. // COM Macros to gain type checking.
  28. //
  29. #if !defined( TypeSafeParams )
  30. #define TypeSafeParams( _interface, _ppunk ) \
  31. IID_##_interface, reinterpret_cast< void ** >( static_cast< _interface ** >( _ppunk ) )
  32. #endif // !defined( TypeSafeParams )
  33. #if !defined( TypeSafeQI )
  34. #define TypeSafeQI( _interface, _ppunk ) \
  35. QueryInterface( TypeSafeParams( _interface, _ppunk ) )
  36. #endif // !defined( TypeSafeQI )
  37. #if !defined( TypeSafeQS )
  38. #define TypeSafeQS( _clsid, _interface, _ppunk ) \
  39. QueryService( _clsid, TypeSafeParams( _interface, _ppunk ) )
  40. #endif // !defined( TypeSafeQS )
  41. //
  42. // COM Constants for string manipulation.
  43. //
  44. #define MAX_COM_GUID_STRING_LEN 39 // "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" + NULL
  45. #define MAX_UUID_STRING_LEN 37 // "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + NULL
  46. //
  47. // Count to use for spin locks.
  48. //
  49. #define RECOMMENDED_SPIN_COUNT 4000
  50. /////////////////////////////////////////////////////////////////////////////
  51. // Global Functions from FormatErrorMessage.cpp
  52. /////////////////////////////////////////////////////////////////////////////
  53. HRESULT
  54. WINAPI
  55. HrFormatErrorMessage(
  56. LPWSTR pszErrorOut
  57. , UINT nMxErrorIn
  58. , DWORD scIn
  59. );
  60. HRESULT
  61. __cdecl
  62. HrFormatErrorMessageBoxText(
  63. LPWSTR pszMessageOut
  64. , UINT nMxMessageIn
  65. , HRESULT hrIn
  66. , LPCWSTR pszOperationIn
  67. , ...
  68. );
  69. HRESULT
  70. WINAPI
  71. HrGetComputerName(
  72. COMPUTER_NAME_FORMAT cnfIn
  73. , BSTR * pbstrComputerNameOut
  74. , BOOL fBestEffortIn
  75. );
  76. /////////////////////////////////////////////////////////////////////////////
  77. // Global Functions from DirectoryUtils.cpp
  78. /////////////////////////////////////////////////////////////////////////////
  79. HRESULT
  80. HrCreateDirectoryPath(
  81. LPWSTR pszDirectoryPath
  82. );
  83. DWORD
  84. DwRemoveDirectory(
  85. const WCHAR * pcszTargetDirIn
  86. , signed int iMaxDepthIn = 32
  87. );
  88. /////////////////////////////////////////////////////////////////////////////
  89. // Global Functions from RegistryUtils.cpp
  90. /////////////////////////////////////////////////////////////////////////////
  91. HRESULT
  92. HrGetDefaultComponentNameFromRegistry(
  93. CLSID * pclsidIn
  94. , BSTR * pbstrComponentNameOut
  95. );
  96. //////////////////////////////////////////////////////////////////////////////
  97. //++
  98. //
  99. // NStringCchCompareCase
  100. //
  101. // Description:
  102. // General strsafe-style strncmp wrapper.
  103. //
  104. // Arguments:
  105. // psz1In and psz2In
  106. // The strings to compare. Either or both can be null, which is
  107. // treated as equivalent to an empty string.
  108. //
  109. // cchsz1In and cchsz2In
  110. // The lengths of psz1In and psz2In, INCLUDING their terminating nulls.
  111. // Ignored if the corresponding sz parameter is null.
  112. //
  113. // Return Values:
  114. // Less than zero: psz1In is less than psz2In.
  115. // Zero: psz1In and psz2In are the same.
  116. // Greater than zero: psz1In is greater than psz2In.
  117. //
  118. //--
  119. //////////////////////////////////////////////////////////////////////////////
  120. inline
  121. INT
  122. NStringCchCompareCase(
  123. LPCWSTR psz1In
  124. , size_t cchsz1In
  125. , LPCWSTR psz2In
  126. , size_t cchsz2In
  127. )
  128. {
  129. INT nComparison = 0;
  130. size_t cchsz1 = ( psz1In == NULL? 0: cchsz1In );
  131. size_t cchsz2 = ( psz2In == NULL? 0: cchsz2In );
  132. size_t cchShorterString = min( cchsz1, cchsz2 );
  133. if ( cchsz1 > 0 )
  134. {
  135. if ( cchsz2 > 0 )
  136. {
  137. nComparison = wcsncmp( psz1In, psz2In, cchShorterString );
  138. }
  139. else // psz2 is empty
  140. {
  141. // psz1 is not empty, but psz2 is.
  142. // Any non-empty string is greater than an empty one.
  143. nComparison = 1;
  144. }
  145. }
  146. else if ( cchsz2 > 0 )
  147. {
  148. // psz1 is empty, but psz2 is not.
  149. // An empty string is less than any non-empty one.
  150. nComparison = -1;
  151. }
  152. // Otherwise, both strings are empty, so leave nComparison at zero.
  153. return nComparison;
  154. } //*** NStringCchCompareCase
  155. //////////////////////////////////////////////////////////////////////////////
  156. //++
  157. //
  158. // NStringCchCompareNoCase
  159. //
  160. // Description:
  161. // General strsafe-style strnicmp wrapper.
  162. //
  163. // Arguments:
  164. // psz1In and psz2In
  165. // The strings to compare. Either or both can be null, which is
  166. // treated as equivalent to an empty string.
  167. //
  168. // cchsz1In and cchsz2In
  169. // The lengths of psz1In and psz2In, INCLUDING their terminating nulls.
  170. // Ignored if the corresponding sz parameter is null.
  171. //
  172. // Return Values:
  173. // Less than zero: psz1In is less than psz2In.
  174. // Zero: psz1In and psz2In are the same.
  175. // Greater than zero: psz1In is greater than psz2In.
  176. //
  177. //--
  178. //////////////////////////////////////////////////////////////////////////////
  179. inline
  180. INT
  181. NStringCchCompareNoCase(
  182. LPCWSTR psz1In
  183. , size_t cchsz1In
  184. , LPCWSTR psz2In
  185. , size_t cchsz2In
  186. )
  187. {
  188. INT nComparison = 0;
  189. size_t cchsz1 = ( psz1In == NULL? 0: cchsz1In );
  190. size_t cchsz2 = ( psz2In == NULL? 0: cchsz2In );
  191. size_t cchShorterString = min( cchsz1, cchsz2 );
  192. if ( cchsz1 > 0 )
  193. {
  194. if ( cchsz2 > 0 )
  195. {
  196. nComparison = _wcsnicmp( psz1In, psz2In, cchShorterString );
  197. }
  198. else // psz2 is empty
  199. {
  200. // psz1 is not empty, but psz2 is.
  201. // Any non-empty string is greater than an empty one.
  202. nComparison = 1;
  203. }
  204. }
  205. else if ( cchsz2 > 0 )
  206. {
  207. // psz1 is empty, but psz2 is not.
  208. // An empty string is less than any non-empty one.
  209. nComparison = -1;
  210. }
  211. // Otherwise, both strings are empty, so leave nComparison at zero.
  212. return nComparison;
  213. } //*** NStringCchCompareNoCase
  214. //////////////////////////////////////////////////////////////////////////////
  215. //++
  216. //
  217. // NBSTRCompareCase
  218. //
  219. // Description:
  220. // strcmp for BSTRs.
  221. //
  222. // Arguments:
  223. // bstr1 and bstr2
  224. // The strings to compare. Either or both can be NULL, which is
  225. // equivalent to an empty string.
  226. //
  227. // Return Values:
  228. // Less than zero: bstr1 is less than bstr2.
  229. // Zero: bstr1 and bstr2 are the same.
  230. // Greater than zero: bstr1 is greater than bstr2.
  231. //
  232. //--
  233. //////////////////////////////////////////////////////////////////////////////
  234. inline
  235. INT
  236. NBSTRCompareCase(
  237. BSTR bstr1
  238. , BSTR bstr2
  239. )
  240. {
  241. return NStringCchCompareCase( bstr1, SysStringLen( bstr1 ) + 1, bstr2, SysStringLen( bstr2 ) + 1 );
  242. } //*** NBSTRCompareCase
  243. //////////////////////////////////////////////////////////////////////////////
  244. //++
  245. //
  246. // NBSTRCompareNoCase
  247. //
  248. // Description:
  249. // stricmp for BSTRs.
  250. //
  251. // Arguments:
  252. // bstr1 and bstr2
  253. // The strings to compare. Either or both can be NULL, which is
  254. // equivalent to an empty string.
  255. //
  256. // Return Values:
  257. // Less than zero: bstr1 is less than bstr2.
  258. // Zero: bstr1 and bstr2 are the same.
  259. // Greater than zero: bstr1 is greater than bstr2.
  260. //
  261. //--
  262. //////////////////////////////////////////////////////////////////////////////
  263. inline
  264. INT
  265. NBSTRCompareNoCase(
  266. BSTR bstr1
  267. , BSTR bstr2
  268. )
  269. {
  270. return NStringCchCompareNoCase( bstr1, SysStringLen( bstr1 ) + 1, bstr2, SysStringLen( bstr2 ) + 1 );
  271. } //*** NBSTRCompareNoCase