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.

315 lines
8.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft OLE
  4. // Copyright (C) Microsoft Corporation, 1996 - 1997.
  5. //
  6. // File: miscutil.cpp
  7. //
  8. // Contents: General utility functions for storage
  9. // VerifyResult
  10. // RunningDebugOle
  11. // WaitForObjectsAndProcessMessages
  12. // Hex
  13. // EnumLocalDrives
  14. //
  15. // Functions:
  16. //
  17. // History: 07/29/97 SCousens Created
  18. //
  19. //--------------------------------------------------------------------------
  20. #include <dfheader.hxx>
  21. #pragma hdrstop
  22. // Debug Object declaration
  23. DH_DECLARE;
  24. //+-------------------------------------------------------------------------
  25. //
  26. // Function: VerifyResult
  27. //
  28. // Synopsis: Check to see if a HRESULT has the expected value
  29. //
  30. // Parameters: [hrCheck] -- the HRESULT to check
  31. // [hrExpected] -- the HRESULT expected
  32. //
  33. // Returns: S_OK if the HRESULT's match. If they don't match and the
  34. // and the actual result was a failure, then that result is
  35. // returned (preserving the original failure). Otherwise
  36. // E_FAIL is returned.
  37. //
  38. // History: 28-Jun-95 MikeW Created
  39. //
  40. // Notes: The main advantages of this routine over DH_HRCHECK is that
  41. // it puts out more diagnostic messages and that it's easier
  42. // to verify values where hr != S_OK.
  43. //
  44. //--------------------------------------------------------------------------
  45. HRESULT VerifyResult(HRESULT hrCheck, HRESULT hrExpected)
  46. {
  47. HRESULT hr = S_OK;
  48. DH_FUNCENTRY(&hr, DH_LVL_TRACE2, TEXT("VerifyResult"));
  49. hrCheck = FixHr (hrCheck);
  50. if (hrCheck != hrExpected)
  51. {
  52. DH_TRACE((
  53. DH_LVL_ERROR,
  54. TEXT("HRESULT == 0x%08x, expected 0x%08x"),
  55. hrCheck,
  56. hrExpected));
  57. if (FAILED(hrCheck))
  58. {
  59. hr = hrCheck;
  60. }
  61. else
  62. {
  63. hr = E_FAIL;
  64. }
  65. }
  66. return hr;
  67. }
  68. //---------------------------------------------------------------------------
  69. //
  70. // Method: Hex
  71. //
  72. // Synopsis: Converts a hex char to integer
  73. //
  74. // Parameters: [ch] -- character to convert
  75. //
  76. // History: 03-Nov-97 BogdanT Created
  77. //
  78. // Comments: This function asserts if the character is passing chars
  79. // outside the hexadecimal range
  80. //
  81. //---------------------------------------------------------------------------
  82. UINT Hex(CHAR ch)
  83. {
  84. if('0'<=ch && ch<='9')
  85. return ch-'0';
  86. if('a'<=ch && ch<='f')
  87. return 10+ch-'a';
  88. if('A'<=ch && ch<='F')
  89. return 10+ch-'A';
  90. DH_ASSERT(!TEXT("Non hexadecimal char passed to Hex()"));
  91. return '\0';
  92. }
  93. //////////////////////////////////////////////////////////////
  94. //
  95. // Function: EnumLocalDrives
  96. //
  97. // Synopsis: Enumerates all local drives (except A:, B:)
  98. // and returns a 32bit drive mask to show
  99. // availablity of FIXED, REMOVABLE, RAM disks.
  100. //
  101. // Return : ULONG bitmask
  102. // NOTES:
  103. // return value: bit set drive present.
  104. // A:=bit 0, B:=bit 1, C:=bit 2 D:=bit 3.
  105. // bits 0,1 always off (ignore A:, B:)
  106. //
  107. // History: 10-Oct-97 scousens created.
  108. //
  109. //////////////////////////////////////////////////////////////
  110. ULONG EnumLocalDrives()
  111. {
  112. #ifdef _MAC
  113. return 0L;
  114. #else
  115. ULONG ulMask, ulMap = 0L;
  116. TCHAR szDrive[3] = {TEXT("C:")};
  117. ulMap = 0L; // We don't have A:, B:
  118. ulMask = 0x04L ;
  119. do
  120. {
  121. switch (GetDriveType(szDrive))
  122. {
  123. case DRIVE_FIXED : //The disk cannot be removed from the drive.
  124. case DRIVE_RAMDISK : //The drive is a RAM disk.
  125. case DRIVE_REMOVABLE : //The disk can be removed from the drive.
  126. ulMap |= ulMask;
  127. break;
  128. //case DRIVE_UNKNOWN : //The drive type cannot be determined.
  129. //case DRIVE_NO_ROOT_DIR : //The root directory does not exist.
  130. //case DRIVE_REMOTE : //The drive is a remote (network) drive.
  131. //case DRIVE_CDROM : //The drive is a CD-ROM drive.
  132. default:
  133. break;
  134. }
  135. ulMask <<= 1;
  136. ++*szDrive;
  137. } while (*szDrive <= TCHAR('Z'));
  138. return (ulMap);
  139. #endif //_MAC
  140. }
  141. //+-------------------------------------------------------------------
  142. //
  143. // Function: WaitForObjectsAndProcessMessages
  144. //
  145. // Synopsis: Processes windows messages for all windows on the
  146. // current thread and waits for provided events to be
  147. // signalled.
  148. //
  149. // Arguments: [pHandles] - A pointer to an array of handles to wait
  150. // on. These handles must be Windows thread
  151. // synchronization objects.
  152. //
  153. // [dwCount] - The count of handles in [pHandles]
  154. //
  155. // [fWaitAll] - TRUE to wait for all objects in [pHandles];
  156. // FALSE to wait for only one.
  157. //
  158. // [dwMilliSeconds] - Milliseconds to wait; can be INFINITE
  159. //
  160. // Returns: The return value from MsgWaitForMultipleObjects()
  161. //
  162. // History: 18-Sept-1995 AlexE Created
  163. //
  164. //--------------------------------------------------------------------
  165. DWORD WaitForObjectsAndProcessMessages(
  166. LPHANDLE pHandles,
  167. DWORD dwCount,
  168. BOOL fWaitAll,
  169. DWORD dwMilliSeconds)
  170. {
  171. MSG msg ;
  172. DWORD dwWaitResult ;
  173. BOOL fGotQuitMessage = FALSE ;
  174. int nQuitExitCode = 0;
  175. for (;;)
  176. {
  177. //
  178. // Wait on supplied events; only wake up if we need
  179. // to process a message - we force QS_ALLINPUT here
  180. // to make sure that ALL windows on this thread have
  181. // their messages processed.
  182. //
  183. dwWaitResult = MsgWaitForMultipleObjects(
  184. dwCount,
  185. pHandles,
  186. fWaitAll,
  187. dwMilliSeconds,
  188. QS_ALLINPUT) ;
  189. //
  190. // If one or all of our objects has become signalled,
  191. // return the value to the caller
  192. //
  193. if ( (dwWaitResult < (WAIT_OBJECT_0 + dwCount)) &&
  194. (dwWaitResult >= WAIT_OBJECT_0) )
  195. {
  196. break ;
  197. }
  198. //
  199. // If a message is in the queue, wake up, process the
  200. // message, and call MsgWaitForMultipleObjects() again.
  201. //
  202. else if ( (WAIT_OBJECT_0 + dwCount) == dwWaitResult)
  203. {
  204. while (FALSE != PeekMessage(
  205. &msg,
  206. (HWND) 0,
  207. 0,
  208. 0,
  209. PM_REMOVE))
  210. {
  211. if (WM_QUIT == msg.message)
  212. {
  213. DH_ASSERT(FALSE == fGotQuitMessage) ;
  214. fGotQuitMessage = TRUE ;
  215. nQuitExitCode = (int) msg.wParam ;
  216. }
  217. else
  218. {
  219. TranslateMessage(&msg) ;
  220. DispatchMessage(&msg) ;
  221. }
  222. }
  223. }
  224. //
  225. // Else, some unusual situation has occurred; we
  226. // can just assert in this case and break out of
  227. // the loop
  228. //
  229. else
  230. {
  231. DH_ASSERT(!"MsgWaitForMultipleObjects() error") ;
  232. break ;
  233. }
  234. }
  235. //
  236. // If we got a WM_QUIT while waiting for the event re-post it now
  237. //
  238. if (fGotQuitMessage)
  239. {
  240. PostQuitMessage(nQuitExitCode);
  241. }
  242. return dwWaitResult ;
  243. }
  244. //+-------------------------------------------------------------------------
  245. //
  246. // Function: RunningDebugOle
  247. //
  248. // Synopsis: Determines if were running under debug Ole
  249. //
  250. // Parameters: None
  251. //
  252. // Returns: S_OK -- Debug Ole
  253. // S_FALSE -- Retail Ole
  254. // HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND) -- Ole is not loaded
  255. //
  256. // Algorithm: Since there's no official way to tell if were running debug
  257. // Ole or not, we have to fall back to checking for debug-only
  258. // exports - we use "DumpATOM" here.
  259. //
  260. // History: 13-Nov-95 MikeW Created
  261. //
  262. //---------------------------------------------------------------------------
  263. HRESULT RunningDebugOle()
  264. {
  265. HMODULE hModOle;
  266. hModOle = GetModuleHandle(TEXT("OLE32"));
  267. if (NULL == hModOle)
  268. {
  269. return HRESULT_FROM_WIN32(GetLastError());
  270. }
  271. if (NULL == GetProcAddress(hModOle, "DumpATOM"))
  272. {
  273. return S_FALSE;
  274. }
  275. return S_OK;
  276. }