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.

444 lines
14 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // File: misc.cxx
  4. //
  5. // Contents: Useful OLE helper and debugging functions
  6. //
  7. //----------------------------------------------------------------------
  8. extern "C" {
  9. #include <nt.h>
  10. #include <ntrtl.h>
  11. #include <nturtl.h>
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <basetyps.h>
  16. #include "dswarn.h"
  17. #include "oledsdbg.h"
  18. }
  19. #if (defined(BUILD_FOR_NT40))
  20. typedef unsigned long HRESULT;
  21. #endif
  22. static HRESULT ADsDebugOutHRESULT(DWORD dwFlags, HRESULT r);
  23. #if DBG == 1
  24. //+---------------------------------------------------------------
  25. //
  26. // Function: PrintHRESULT
  27. //
  28. // Synopsis: Outputs the name of the SCODE and a carriage return
  29. // to the debugging device.
  30. //
  31. // Arguments: [dwFlags] -- Flags to ADsDebugOut.
  32. // [scode] -- The status code to report.
  33. //
  34. // Notes: This function disappears in retail builds.
  35. //
  36. //----------------------------------------------------------------
  37. STDAPI
  38. PrintHRESULT(DWORD dwFlags, HRESULT hr)
  39. {
  40. ADsDebugOut((dwFlags | DEB_NOCOMPNAME, " "));
  41. ADsDebugOutHRESULT(dwFlags | DEB_NOCOMPNAME, hr);
  42. ADsDebugOut((dwFlags | DEB_NOCOMPNAME, "\n"));
  43. return hr;
  44. }
  45. //+---------------------------------------------------------------
  46. //
  47. // Function: ADsDebugOutHRESULT
  48. //
  49. // Synopsis: Outputs the name of the SCODE to the debugging device.
  50. //
  51. // Arguments: [dwFlags] -- Flags to ADsDebugOut.
  52. // [scode] -- The status code to report.
  53. //
  54. // Notes: This function disappears in retail builds.
  55. //
  56. //----------------------------------------------------------------
  57. static HRESULT
  58. ADsDebugOutHRESULT(DWORD dwFlags, HRESULT r)
  59. {
  60. LPWSTR lpstr;
  61. #define CASE_SCODE(sc) \
  62. case sc: lpstr = (LPWSTR)L#sc; break;
  63. switch (r) {
  64. /* SCODE's defined in SCODE.H */
  65. CASE_SCODE(S_OK)
  66. CASE_SCODE(S_FALSE)
  67. CASE_SCODE(OLE_S_USEREG)
  68. CASE_SCODE(OLE_S_STATIC)
  69. CASE_SCODE(OLE_S_MAC_CLIPFORMAT)
  70. CASE_SCODE(DRAGDROP_S_DROP)
  71. CASE_SCODE(DRAGDROP_S_USEDEFAULTCURSORS)
  72. CASE_SCODE(DRAGDROP_S_CANCEL)
  73. CASE_SCODE(DATA_S_SAMEFORMATETC)
  74. CASE_SCODE(VIEW_S_ALREADY_FROZEN)
  75. CASE_SCODE(CACHE_S_FORMATETC_NOTSUPPORTED)
  76. CASE_SCODE(CACHE_S_SAMECACHE)
  77. CASE_SCODE(CACHE_S_SOMECACHES_NOTUPDATED)
  78. CASE_SCODE(OLEOBJ_S_INVALIDVERB)
  79. CASE_SCODE(OLEOBJ_S_CANNOT_DOVERB_NOW)
  80. CASE_SCODE(OLEOBJ_S_INVALIDHWND)
  81. CASE_SCODE(INPLACE_S_TRUNCATED)
  82. CASE_SCODE(CONVERT10_S_NO_PRESENTATION)
  83. CASE_SCODE(MK_S_REDUCED_TO_SELF)
  84. CASE_SCODE(MK_S_ME)
  85. CASE_SCODE(MK_S_HIM)
  86. CASE_SCODE(MK_S_US)
  87. CASE_SCODE(MK_S_MONIKERALREADYREGISTERED)
  88. CASE_SCODE(STG_S_CONVERTED)
  89. CASE_SCODE(E_UNEXPECTED)
  90. CASE_SCODE(E_NOTIMPL)
  91. CASE_SCODE(E_OUTOFMEMORY)
  92. CASE_SCODE(E_INVALIDARG)
  93. CASE_SCODE(E_NOINTERFACE)
  94. CASE_SCODE(E_POINTER)
  95. CASE_SCODE(E_HANDLE)
  96. CASE_SCODE(E_ABORT)
  97. CASE_SCODE(E_FAIL)
  98. CASE_SCODE(E_ACCESSDENIED)
  99. /* SCODE's defined in DVOBJ.H */
  100. // CASE_SCODE(DATA_E_FORMATETC)
  101. // same as DATA_E_FORMATETC CASE_SCODE(DV_E_FORMATETC)
  102. CASE_SCODE(VIEW_E_DRAW)
  103. // same as VIEW_E_DRAW CASE_SCODE(E_DRAW)
  104. CASE_SCODE(CACHE_E_NOCACHE_UPDATED)
  105. /* SCODE's defined in OLE2.H */
  106. CASE_SCODE(OLE_E_OLEVERB)
  107. CASE_SCODE(OLE_E_ADVF)
  108. CASE_SCODE(OLE_E_ENUM_NOMORE)
  109. CASE_SCODE(OLE_E_ADVISENOTSUPPORTED)
  110. CASE_SCODE(OLE_E_NOCONNECTION)
  111. CASE_SCODE(OLE_E_NOTRUNNING)
  112. CASE_SCODE(OLE_E_NOCACHE)
  113. CASE_SCODE(OLE_E_BLANK)
  114. CASE_SCODE(OLE_E_CLASSDIFF)
  115. CASE_SCODE(OLE_E_CANT_GETMONIKER)
  116. CASE_SCODE(OLE_E_CANT_BINDTOSOURCE)
  117. CASE_SCODE(OLE_E_STATIC)
  118. CASE_SCODE(OLE_E_PROMPTSAVECANCELLED)
  119. CASE_SCODE(OLE_E_INVALIDRECT)
  120. CASE_SCODE(OLE_E_WRONGCOMPOBJ)
  121. CASE_SCODE(OLE_E_INVALIDHWND)
  122. CASE_SCODE(DV_E_DVTARGETDEVICE)
  123. CASE_SCODE(DV_E_STGMEDIUM)
  124. CASE_SCODE(DV_E_STATDATA)
  125. CASE_SCODE(DV_E_LINDEX)
  126. CASE_SCODE(DV_E_TYMED)
  127. CASE_SCODE(DV_E_CLIPFORMAT)
  128. CASE_SCODE(DV_E_DVASPECT)
  129. CASE_SCODE(DV_E_DVTARGETDEVICE_SIZE)
  130. CASE_SCODE(DV_E_NOIVIEWOBJECT)
  131. CASE_SCODE(CONVERT10_E_OLESTREAM_GET)
  132. CASE_SCODE(CONVERT10_E_OLESTREAM_PUT)
  133. CASE_SCODE(CONVERT10_E_OLESTREAM_FMT)
  134. CASE_SCODE(CONVERT10_E_OLESTREAM_BITMAP_TO_DIB)
  135. CASE_SCODE(CONVERT10_E_STG_FMT)
  136. CASE_SCODE(CONVERT10_E_STG_NO_STD_STREAM)
  137. CASE_SCODE(CONVERT10_E_STG_DIB_TO_BITMAP)
  138. CASE_SCODE(CLIPBRD_E_CANT_OPEN)
  139. CASE_SCODE(CLIPBRD_E_CANT_EMPTY)
  140. CASE_SCODE(CLIPBRD_E_CANT_SET)
  141. CASE_SCODE(CLIPBRD_E_BAD_DATA)
  142. CASE_SCODE(CLIPBRD_E_CANT_CLOSE)
  143. CASE_SCODE(DRAGDROP_E_NOTREGISTERED)
  144. CASE_SCODE(DRAGDROP_E_ALREADYREGISTERED)
  145. CASE_SCODE(DRAGDROP_E_INVALIDHWND)
  146. CASE_SCODE(OLEOBJ_E_NOVERBS)
  147. CASE_SCODE(INPLACE_E_NOTUNDOABLE)
  148. CASE_SCODE(INPLACE_E_NOTOOLSPACE)
  149. /* SCODE's defined in STORAGE.H */
  150. CASE_SCODE(STG_E_INVALIDFUNCTION)
  151. CASE_SCODE(STG_E_FILENOTFOUND)
  152. CASE_SCODE(STG_E_PATHNOTFOUND)
  153. CASE_SCODE(STG_E_TOOMANYOPENFILES)
  154. CASE_SCODE(STG_E_ACCESSDENIED)
  155. CASE_SCODE(STG_E_INVALIDHANDLE)
  156. CASE_SCODE(STG_E_INSUFFICIENTMEMORY)
  157. CASE_SCODE(STG_E_INVALIDPOINTER)
  158. CASE_SCODE(STG_E_NOMOREFILES)
  159. CASE_SCODE(STG_E_DISKISWRITEPROTECTED)
  160. CASE_SCODE(STG_E_SEEKERROR)
  161. CASE_SCODE(STG_E_WRITEFAULT)
  162. CASE_SCODE(STG_E_READFAULT)
  163. CASE_SCODE(STG_E_LOCKVIOLATION)
  164. CASE_SCODE(STG_E_FILEALREADYEXISTS)
  165. CASE_SCODE(STG_E_INVALIDPARAMETER)
  166. CASE_SCODE(STG_E_MEDIUMFULL)
  167. CASE_SCODE(STG_E_ABNORMALAPIEXIT)
  168. CASE_SCODE(STG_E_INVALIDHEADER)
  169. CASE_SCODE(STG_E_INVALIDNAME)
  170. CASE_SCODE(STG_E_UNKNOWN)
  171. CASE_SCODE(STG_E_UNIMPLEMENTEDFUNCTION)
  172. CASE_SCODE(STG_E_INVALIDFLAG)
  173. CASE_SCODE(STG_E_INUSE)
  174. CASE_SCODE(STG_E_NOTCURRENT)
  175. CASE_SCODE(STG_E_REVERTED)
  176. CASE_SCODE(STG_E_CANTSAVE)
  177. CASE_SCODE(STG_E_OLDFORMAT)
  178. CASE_SCODE(STG_E_OLDDLL)
  179. CASE_SCODE(STG_E_SHAREREQUIRED)
  180. /* SCODE's defined in COMPOBJ.H */
  181. CASE_SCODE(CO_E_NOTINITIALIZED)
  182. CASE_SCODE(CO_E_ALREADYINITIALIZED)
  183. CASE_SCODE(CO_E_CANTDETERMINECLASS)
  184. CASE_SCODE(CO_E_CLASSSTRING)
  185. CASE_SCODE(CO_E_IIDSTRING)
  186. CASE_SCODE(CO_E_APPNOTFOUND)
  187. CASE_SCODE(CO_E_APPSINGLEUSE)
  188. CASE_SCODE(CO_E_ERRORINAPP)
  189. CASE_SCODE(CO_E_DLLNOTFOUND)
  190. CASE_SCODE(CO_E_ERRORINDLL)
  191. CASE_SCODE(CO_E_WRONGOSFORAPP)
  192. CASE_SCODE(CO_E_OBJNOTREG)
  193. CASE_SCODE(CO_E_OBJISREG)
  194. CASE_SCODE(CO_E_OBJNOTCONNECTED)
  195. CASE_SCODE(CO_E_APPDIDNTREG)
  196. CASE_SCODE(CLASS_E_NOAGGREGATION)
  197. CASE_SCODE(REGDB_E_READREGDB)
  198. CASE_SCODE(REGDB_E_WRITEREGDB)
  199. CASE_SCODE(REGDB_E_KEYMISSING)
  200. CASE_SCODE(REGDB_E_INVALIDVALUE)
  201. CASE_SCODE(REGDB_E_CLASSNOTREG)
  202. CASE_SCODE(REGDB_E_IIDNOTREG)
  203. CASE_SCODE(RPC_E_CALL_REJECTED)
  204. CASE_SCODE(RPC_E_CALL_CANCELED)
  205. CASE_SCODE(RPC_E_CANTPOST_INSENDCALL)
  206. CASE_SCODE(RPC_E_CANTCALLOUT_INASYNCCALL)
  207. CASE_SCODE(RPC_E_CANTCALLOUT_INEXTERNALCALL)
  208. CASE_SCODE(RPC_E_CONNECTION_TERMINATED)
  209. #if defined(NO_NTOLEBUGS)
  210. CASE_SCODE(RPC_E_SERVER_DIED)
  211. #endif // NO_NTOLEBUGS
  212. CASE_SCODE(RPC_E_CLIENT_DIED)
  213. CASE_SCODE(RPC_E_INVALID_DATAPACKET)
  214. CASE_SCODE(RPC_E_CANTTRANSMIT_CALL)
  215. CASE_SCODE(RPC_E_CLIENT_CANTMARSHAL_DATA)
  216. CASE_SCODE(RPC_E_CLIENT_CANTUNMARSHAL_DATA)
  217. CASE_SCODE(RPC_E_SERVER_CANTMARSHAL_DATA)
  218. CASE_SCODE(RPC_E_SERVER_CANTUNMARSHAL_DATA)
  219. CASE_SCODE(RPC_E_INVALID_DATA)
  220. CASE_SCODE(RPC_E_INVALID_PARAMETER)
  221. CASE_SCODE(RPC_E_UNEXPECTED)
  222. /* SCODE's defined in MONIKER.H */
  223. CASE_SCODE(MK_E_CONNECTMANUALLY)
  224. CASE_SCODE(MK_E_EXCEEDEDDEADLINE)
  225. CASE_SCODE(MK_E_NEEDGENERIC)
  226. CASE_SCODE(MK_E_UNAVAILABLE)
  227. CASE_SCODE(MK_E_SYNTAX)
  228. CASE_SCODE(MK_E_NOOBJECT)
  229. CASE_SCODE(MK_E_INVALIDEXTENSION)
  230. CASE_SCODE(MK_E_INTERMEDIATEINTERFACENOTSUPPORTED)
  231. CASE_SCODE(MK_E_NOTBINDABLE)
  232. CASE_SCODE(MK_E_NOTBOUND)
  233. CASE_SCODE(MK_E_CANTOPENFILE)
  234. CASE_SCODE(MK_E_MUSTBOTHERUSER)
  235. CASE_SCODE(MK_E_NOINVERSE)
  236. CASE_SCODE(MK_E_NOSTORAGE)
  237. #if defined(NO_NTOLEBUGS)
  238. CASE_SCODE(MK_S_MONIKERALREADYREGISTERED)
  239. #endif //NO_NTOLEBUGS
  240. // Dispatch error codes
  241. CASE_SCODE(DISP_E_MEMBERNOTFOUND)
  242. default:
  243. ADsDebugOut((dwFlags, "<UNKNOWN SCODE 0x%lx>", r));
  244. return r;
  245. }
  246. #undef CASE_SCODE
  247. ADsDebugOut((dwFlags, "<%ws (0x%lx)>", lpstr, r));
  248. return r;
  249. }
  250. //+---------------------------------------------------------------------------
  251. //
  252. // Function: BreakOnFailed
  253. //
  254. // Synopsis: Function called when CheckAndReturnResult or CheckResult
  255. // examines a failure code. Set a breakpoint on this function
  256. // to break on failures.
  257. //
  258. // History: 5-18-94 adams Created
  259. //
  260. //----------------------------------------------------------------------------
  261. static void
  262. BreakOnFailed(void)
  263. {
  264. int x;
  265. x = 1;
  266. }
  267. //+---------------------------------------------------------------
  268. //
  269. // Function: CheckAndReturnResult
  270. //
  271. // Synopsis: Issues a warning if the HRESULT indicates failure,
  272. // and asserts if the HRESULT is not a permitted success code.
  273. //
  274. // Arguments: [hr] -- the HRESULT to be checked.
  275. // [lpstrFile] -- the file where the HRESULT is being checked.
  276. // [line] -- the line in the file where the HRESULT is
  277. // being checked.
  278. // [cSuccess] -- the number of permitted non-zero success codes.
  279. // [...] -- list of success codes.
  280. //
  281. // Returns: The return value is exactly the HRESULT passed in.
  282. //
  283. // History: 1-06-94 adams Created.
  284. // 5-24-94 adams Added call to BreakOnFailed.
  285. //
  286. // Notes: This function should not be used directly. Use
  287. // the SRETURN and RRETURN macros instead.
  288. //
  289. //----------------------------------------------------------------
  290. STDAPI
  291. CheckAndReturnResult(
  292. HRESULT hr,
  293. LPSTR lpstrFile,
  294. UINT line,
  295. int cSuccess,
  296. ...)
  297. {
  298. BOOL fOKReturnCode;
  299. va_list va;
  300. int i;
  301. HRESULT hrSuccess;
  302. //
  303. // Check if code is an error or permitted success.
  304. //
  305. fOKReturnCode = (FAILED(hr) || hr == S_OK || hr == S_FALSE ||
  306. cSuccess == -1);
  307. if (!fOKReturnCode && cSuccess > 0)
  308. {
  309. va_start(va, cSuccess);
  310. for (i = 0; i < cSuccess; i++)
  311. {
  312. hrSuccess = va_arg(va, HRESULT);
  313. ADsAssert(SUCCEEDED(hrSuccess));
  314. if (hr == hrSuccess)
  315. {
  316. fOKReturnCode = TRUE;
  317. break;
  318. }
  319. }
  320. va_end(va);
  321. }
  322. //
  323. // Assert on non-permitted success code.
  324. //
  325. if (!fOKReturnCode)
  326. {
  327. /* ADsDebugOut((
  328. DEB_ERROR,
  329. "ERROR: %s:%d returned bad success code",
  330. lpstrFile,
  331. line)); */
  332. // (void) PrintHRESULT(DEB_ERROR | DEB_NOCOMPNAME, hr);
  333. //
  334. // I've removed the Assert but we should enable this
  335. // to monitor all functions that are returning S_FALSE
  336. // As far as possible, functions should not return
  337. // S_FALSE except for the Next function of an Enumerator
  338. // object.
  339. // ADsAssert(0 && "An unpermitted success code was returned.");
  340. }
  341. //
  342. // Warn on error result.
  343. //
  344. if (FAILED(hr))
  345. {
  346. ADsDebugOut((
  347. DEB_IWARN,
  348. "WARNING: %s:%d returning",
  349. lpstrFile,
  350. line));
  351. PrintHRESULT(DEB_IWARN | DEB_NOCOMPNAME, hr);
  352. BreakOnFailed();
  353. }
  354. return hr;
  355. }
  356. //+---------------------------------------------------------------
  357. //
  358. // Function: CheckResult
  359. //
  360. // Synopsis: Issues a warning if the HRESULT indicates failure
  361. //
  362. // Arguments: [hr] -- the HRESULT to be checked
  363. // [lpstrFile] -- the file where the HRESULT is being checked
  364. // [line] -- the line in the file where the HRESULT is being checked
  365. //
  366. //
  367. // History: 1-06-94 adams Error printed only on FAILURE, not also
  368. // on non-zero success.
  369. // 5-24-94 adams Added call to BreakOnFailed.
  370. //
  371. // Notes: This function should not be used directly. The RRETURN
  372. // macro is provided for convenience.
  373. //
  374. //----------------------------------------------------------------
  375. STDAPI_(void)
  376. CheckResult(HRESULT hr, LPSTR lpstrFile, UINT line)
  377. {
  378. if (FAILED(hr))
  379. {
  380. ADsDebugOut((DEB_IWARN, "WARNING: "));
  381. ADsDebugOutHRESULT(DEB_IWARN | DEB_NOCOMPNAME, hr);
  382. ADsDebugOut((
  383. DEB_IWARN | DEB_NOCOMPNAME,
  384. " occurred at %s:%d.\n",
  385. lpstrFile,
  386. line));
  387. BreakOnFailed();
  388. }
  389. }
  390. #endif // DBG == 1