Source code of Windows XP (NT5)
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.

465 lines
14 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // File: misc.cxx
  4. //
  5. // Contents: Useful OLE helper and debugging functions
  6. //
  7. //----------------------------------------------------------------------
  8. #include "headers.hxx"
  9. #pragma hdrstop
  10. //#include <rpcferr.h>
  11. #if !defined(UNICODE) && !defined(OLE2ANSI)
  12. LPOLESTR ConvertMBToOLESTR(LPCSTR lpStr, int cchStr)
  13. {
  14. OLECHAR *lpwConverted;
  15. int cchW;
  16. //
  17. // UNICODE note: assuming we get an LPSTR here, find out how
  18. // many *bytes* are required and multiply by sizeof(TCHAR)
  19. //
  20. cchW = MultiByteToWideChar(CP_ACP, 0, lpStr, cchStr, NULL, 0);
  21. TaskAllocMem(sizeof(OLECHAR)*cchW, (LPVOID FAR *)&lpwConverted);
  22. if (lpwConverted == NULL)
  23. return NULL;
  24. MultiByteToWideChar(CP_ACP
  25. , 0
  26. , lpStr
  27. , cchStr
  28. , lpwConverted
  29. , cchW);
  30. return lpwConverted;
  31. }
  32. LPTSTR ConvertOLESTRToMB(LPCOLESTR lpStr, int cchStr)
  33. {
  34. LPTSTR lpConverted;
  35. int cch;
  36. //
  37. // UNICODE note: assuming we get an LPSTR here, find out how
  38. // many *bytes* are required and multiply by sizeof(TCHAR)
  39. //
  40. cch = WideCharToMultiByte(CP_ACP, 0, lpStr, cchStr, NULL, 0, NULL, NULL);
  41. TaskAllocMem(sizeof(TCHAR)*cch, (LPVOID FAR *)&lpConverted);
  42. if (lpConverted == NULL)
  43. return NULL;
  44. WideCharToMultiByte(CP_ACP
  45. , 0
  46. , lpStr
  47. , cchStr
  48. , lpConverted
  49. , cch
  50. , NULL
  51. , NULL);
  52. return lpConverted;
  53. }
  54. #endif
  55. //+---------------------------------------------------------------
  56. //
  57. // Function: IsCompatibleOleVersion
  58. //
  59. // Synopsis: Checks if the installed version of OLE is compatible with
  60. // the version of OLE for which the software was written.
  61. //
  62. // Arguments: [wMaj] -- the major version number
  63. // [wMin] -- the minor version number
  64. //
  65. // Returns: TRUE if the installed version of OLE is compatible
  66. //
  67. // Notes: This function combines a call to OleBuildVersion
  68. // with the proper checking of version numbers.
  69. // The major and minor values passed in should be
  70. // the major and minor values returned from
  71. // OleBuildVersion for the version of OLE current
  72. // when the software was written.
  73. //
  74. //----------------------------------------------------------------
  75. BOOL
  76. IsCompatibleOleVersion(WORD wMaj, WORD wMin)
  77. {
  78. // Check our compatibility with the OLE runtime.
  79. // We are compatible with any later major version,
  80. // or the same major version with equal or greater minor version.
  81. DWORD ov = OleBuildVersion();
  82. return HIWORD(ov) > wMaj || (HIWORD(ov) == wMaj && LOWORD(ov) >= wMin);
  83. }
  84. #if DBG
  85. //+---------------------------------------------------------------
  86. //
  87. // Function: AssertSFL
  88. //
  89. // Synopsis: Displays "Assertion Failed" message and puts up
  90. // a message box allowing the user to (1) exit the program,
  91. // (2) break into the debugger, or (3) ignore
  92. //
  93. // Arguments:
  94. // [lpszClause] -- The assertion clause
  95. // [lpszFileName] -- File where assertion failed
  96. // [nLine} -- Line in file where assertion failed
  97. //
  98. //----------------------------------------------------------------
  99. extern "C" void FAR PASCAL AssertSFL(LPSTR lpszClause,
  100. LPSTR lpszFileName, int nLine)
  101. {
  102. static TCHAR achMessage[] = TEXT("File %hs\n Line %d");
  103. TCHAR achTitle[256];
  104. TCHAR achFormatBuffer[256];
  105. wsprintf(achTitle,TEXT("%hs"), lpszClause);
  106. wsprintf(achFormatBuffer, achMessage, lpszFileName, nLine);
  107. OutputDebugString(achFormatBuffer);
  108. OutputDebugString(TEXT(", "));
  109. OutputDebugString(achTitle);
  110. OutputDebugString(TEXT("\n\r"));
  111. retry:
  112. int nCode = ::MessageBox(NULL, achFormatBuffer, achTitle,
  113. MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE);
  114. if (nCode == IDIGNORE)
  115. return; // ignore
  116. else if (nCode == IDRETRY)
  117. {
  118. // break into the debugger (or Dr Watson log)
  119. DebugBreak();
  120. goto retry;
  121. }
  122. // else fall through and exit
  123. FatalExit(2);
  124. }
  125. #endif // DBG
  126. #if DBG == 1
  127. //+---------------------------------------------------------------
  128. //
  129. // Function: TraceIID
  130. //
  131. // Synopsis: Outputs the name of the interface to the debugging device
  132. //
  133. // Arguments: [riid] -- the interface
  134. //
  135. // Notes: This function disappears in retail builds.
  136. //
  137. //----------------------------------------------------------------
  138. void
  139. TraceIID(REFIID riid)
  140. {
  141. LPTSTR lpstr = TEXT("UNKNOWN INTERFACE");
  142. #define CASE_IID(iid) \
  143. if (IsEqualIID(IID_##iid, riid)) lpstr = (LPTSTR)TEXT(#iid);
  144. CASE_IID(IUnknown)
  145. CASE_IID(IOleLink)
  146. CASE_IID(IOleCache)
  147. CASE_IID(IOleManager)
  148. CASE_IID(IOlePresObj)
  149. CASE_IID(IDebug)
  150. CASE_IID(IDebugStream)
  151. CASE_IID(IAdviseSink2)
  152. CASE_IID(IDataObject)
  153. CASE_IID(IViewObject)
  154. CASE_IID(IOleObject)
  155. CASE_IID(IOleInPlaceObject)
  156. CASE_IID(IParseDisplayName)
  157. CASE_IID(IOleContainer)
  158. CASE_IID(IOleItemContainer)
  159. CASE_IID(IOleClientSite)
  160. CASE_IID(IOleInPlaceSite)
  161. CASE_IID(IPersist)
  162. CASE_IID(IPersistStorage)
  163. CASE_IID(IPersistFile)
  164. CASE_IID(IPersistStream)
  165. CASE_IID(IOleClientSite)
  166. CASE_IID(IOleInPlaceSite)
  167. CASE_IID(IAdviseSink)
  168. CASE_IID(IDataAdviseHolder)
  169. CASE_IID(IOleAdviseHolder)
  170. CASE_IID(IClassFactory)
  171. CASE_IID(IOleWindow)
  172. CASE_IID(IOleInPlaceActiveObject)
  173. CASE_IID(IOleInPlaceUIWindow)
  174. CASE_IID(IOleInPlaceFrame)
  175. CASE_IID(IDropSource)
  176. CASE_IID(IDropTarget)
  177. CASE_IID(IBindCtx)
  178. CASE_IID(IEnumUnknown)
  179. CASE_IID(IEnumString)
  180. CASE_IID(IEnumFORMATETC)
  181. CASE_IID(IEnumSTATDATA)
  182. CASE_IID(IEnumOLEVERB)
  183. CASE_IID(IEnumMoniker)
  184. CASE_IID(IEnumGeneric)
  185. CASE_IID(IEnumHolder)
  186. CASE_IID(IEnumCallback)
  187. CASE_IID(ILockBytes)
  188. CASE_IID(IStorage)
  189. CASE_IID(IStream)
  190. //CASE_IID(IDispatch)
  191. //CASE_IID(IEnumVARIANT)
  192. //CASE_IID(ITypeInfo)
  193. //CASE_IID(ITypeLib)
  194. //CASE_IID(ITypeComp)
  195. //CASE_IID(ICreateTypeInfo)
  196. //CASE_IID(ICreateTypeLib)
  197. #undef CASE_IID
  198. TCHAR achTemp[256];
  199. #ifdef UNICODE
  200. wsprintf(achTemp, TEXT("%ws"), lpstr);
  201. #else
  202. wsprintf(achTemp, TEXT("%s"), lpstr);
  203. #endif
  204. OutputDebugString( achTemp );
  205. }
  206. //+---------------------------------------------------------------
  207. //
  208. // Function: TraceHRESULT
  209. //
  210. // Synopsis: Outputs the name of the SCODE to the debugging device
  211. //
  212. // Arguments: [scode] -- the status code to report
  213. //
  214. // Notes: This function disappears in retail builds.
  215. //
  216. //----------------------------------------------------------------
  217. HRESULT
  218. TraceHRESULT(HRESULT r)
  219. {
  220. LPTSTR lpstr;
  221. #ifdef UNICODE
  222. #define CASE_SCODE(sc) \
  223. case sc: lpstr = (LPWSTR)L#sc; break;
  224. #else
  225. #define CASE_SCODE(sc) \
  226. case sc: lpstr = (LPSTR)#sc; break;
  227. #endif
  228. switch (r) {
  229. /* SCODE's defined in SCODE.H */
  230. CASE_SCODE(S_OK)
  231. // same value as S_OK CASE_SCODE(S_TRUE)
  232. CASE_SCODE(S_FALSE)
  233. CASE_SCODE(E_UNEXPECTED)
  234. CASE_SCODE(E_NOTIMPL)
  235. CASE_SCODE(E_OUTOFMEMORY)
  236. CASE_SCODE(E_INVALIDARG)
  237. CASE_SCODE(E_NOINTERFACE)
  238. CASE_SCODE(E_POINTER)
  239. CASE_SCODE(E_HANDLE)
  240. CASE_SCODE(E_ABORT)
  241. CASE_SCODE(E_FAIL)
  242. CASE_SCODE(E_ACCESSDENIED)
  243. /* SCODE's defined in DVOBJ.H */
  244. CASE_SCODE(DATA_E_FORMATETC)
  245. // same as DATA_E_FORMATETC CASE_SCODE(DV_E_FORMATETC)
  246. CASE_SCODE(DATA_S_SAMEFORMATETC)
  247. CASE_SCODE(VIEW_E_DRAW)
  248. // same as VIEW_E_DRAW CASE_SCODE(E_DRAW)
  249. CASE_SCODE(VIEW_S_ALREADY_FROZEN)
  250. CASE_SCODE(CACHE_E_NOCACHE_UPDATED)
  251. CASE_SCODE(CACHE_S_FORMATETC_NOTSUPPORTED)
  252. CASE_SCODE(CACHE_S_SAMECACHE)
  253. CASE_SCODE(CACHE_S_SOMECACHES_NOTUPDATED)
  254. /* SCODE's defined in OLE2.H */
  255. CASE_SCODE(OLE_E_OLEVERB)
  256. CASE_SCODE(OLE_E_ADVF)
  257. CASE_SCODE(OLE_E_ENUM_NOMORE)
  258. CASE_SCODE(OLE_E_ADVISENOTSUPPORTED)
  259. CASE_SCODE(OLE_E_NOCONNECTION)
  260. CASE_SCODE(OLE_E_NOTRUNNING)
  261. CASE_SCODE(OLE_E_NOCACHE)
  262. CASE_SCODE(OLE_E_BLANK)
  263. CASE_SCODE(OLE_E_CLASSDIFF)
  264. CASE_SCODE(OLE_E_CANT_GETMONIKER)
  265. CASE_SCODE(OLE_E_CANT_BINDTOSOURCE)
  266. CASE_SCODE(OLE_E_STATIC)
  267. CASE_SCODE(OLE_E_PROMPTSAVECANCELLED)
  268. CASE_SCODE(OLE_E_INVALIDRECT)
  269. CASE_SCODE(OLE_E_WRONGCOMPOBJ)
  270. CASE_SCODE(OLE_E_INVALIDHWND)
  271. CASE_SCODE(DV_E_DVTARGETDEVICE)
  272. CASE_SCODE(DV_E_STGMEDIUM)
  273. CASE_SCODE(DV_E_STATDATA)
  274. CASE_SCODE(DV_E_LINDEX)
  275. CASE_SCODE(DV_E_TYMED)
  276. CASE_SCODE(DV_E_CLIPFORMAT)
  277. CASE_SCODE(DV_E_DVASPECT)
  278. CASE_SCODE(DV_E_DVTARGETDEVICE_SIZE)
  279. CASE_SCODE(DV_E_NOIVIEWOBJECT)
  280. CASE_SCODE(OLE_S_USEREG)
  281. CASE_SCODE(OLE_S_STATIC)
  282. CASE_SCODE(OLE_S_MAC_CLIPFORMAT)
  283. CASE_SCODE(CONVERT10_E_OLESTREAM_GET)
  284. CASE_SCODE(CONVERT10_E_OLESTREAM_PUT)
  285. CASE_SCODE(CONVERT10_E_OLESTREAM_FMT)
  286. CASE_SCODE(CONVERT10_E_OLESTREAM_BITMAP_TO_DIB)
  287. CASE_SCODE(CONVERT10_E_STG_FMT)
  288. CASE_SCODE(CONVERT10_E_STG_NO_STD_STREAM)
  289. CASE_SCODE(CONVERT10_E_STG_DIB_TO_BITMAP)
  290. CASE_SCODE(CONVERT10_S_NO_PRESENTATION)
  291. CASE_SCODE(CLIPBRD_E_CANT_OPEN)
  292. CASE_SCODE(CLIPBRD_E_CANT_EMPTY)
  293. CASE_SCODE(CLIPBRD_E_CANT_SET)
  294. CASE_SCODE(CLIPBRD_E_BAD_DATA)
  295. CASE_SCODE(CLIPBRD_E_CANT_CLOSE)
  296. CASE_SCODE(DRAGDROP_E_NOTREGISTERED)
  297. CASE_SCODE(DRAGDROP_E_ALREADYREGISTERED)
  298. CASE_SCODE(DRAGDROP_E_INVALIDHWND)
  299. CASE_SCODE(DRAGDROP_S_DROP)
  300. CASE_SCODE(DRAGDROP_S_CANCEL)
  301. CASE_SCODE(DRAGDROP_S_USEDEFAULTCURSORS)
  302. CASE_SCODE(OLEOBJ_E_NOVERBS)
  303. CASE_SCODE(OLEOBJ_S_INVALIDVERB)
  304. CASE_SCODE(OLEOBJ_S_CANNOT_DOVERB_NOW)
  305. CASE_SCODE(OLEOBJ_S_INVALIDHWND)
  306. CASE_SCODE(INPLACE_E_NOTUNDOABLE)
  307. CASE_SCODE(INPLACE_E_NOTOOLSPACE)
  308. CASE_SCODE(INPLACE_S_TRUNCATED)
  309. /* SCODE's defined in STORAGE.H */
  310. CASE_SCODE(STG_E_INVALIDFUNCTION)
  311. CASE_SCODE(STG_E_FILENOTFOUND)
  312. CASE_SCODE(STG_E_PATHNOTFOUND)
  313. CASE_SCODE(STG_E_TOOMANYOPENFILES)
  314. CASE_SCODE(STG_E_ACCESSDENIED)
  315. CASE_SCODE(STG_E_INVALIDHANDLE)
  316. CASE_SCODE(STG_E_INSUFFICIENTMEMORY)
  317. CASE_SCODE(STG_E_INVALIDPOINTER)
  318. CASE_SCODE(STG_E_NOMOREFILES)
  319. CASE_SCODE(STG_E_DISKISWRITEPROTECTED)
  320. CASE_SCODE(STG_E_SEEKERROR)
  321. CASE_SCODE(STG_E_WRITEFAULT)
  322. CASE_SCODE(STG_E_READFAULT)
  323. CASE_SCODE(STG_E_LOCKVIOLATION)
  324. CASE_SCODE(STG_E_FILEALREADYEXISTS)
  325. CASE_SCODE(STG_E_INVALIDPARAMETER)
  326. CASE_SCODE(STG_E_MEDIUMFULL)
  327. CASE_SCODE(STG_E_ABNORMALAPIEXIT)
  328. CASE_SCODE(STG_E_INVALIDHEADER)
  329. CASE_SCODE(STG_E_INVALIDNAME)
  330. CASE_SCODE(STG_E_UNKNOWN)
  331. CASE_SCODE(STG_E_UNIMPLEMENTEDFUNCTION)
  332. CASE_SCODE(STG_E_INVALIDFLAG)
  333. CASE_SCODE(STG_E_INUSE)
  334. CASE_SCODE(STG_E_NOTCURRENT)
  335. CASE_SCODE(STG_E_REVERTED)
  336. CASE_SCODE(STG_E_CANTSAVE)
  337. CASE_SCODE(STG_E_OLDFORMAT)
  338. CASE_SCODE(STG_E_OLDDLL)
  339. CASE_SCODE(STG_E_SHAREREQUIRED)
  340. CASE_SCODE(STG_S_CONVERTED)
  341. //CASE_SCODE(STG_S_BUFFEROVERFLOW)
  342. //CASE_SCODE(STG_S_TRYOVERWRITE)
  343. /* SCODE's defined in COMPOBJ.H */
  344. CASE_SCODE(CO_E_NOTINITIALIZED)
  345. CASE_SCODE(CO_E_ALREADYINITIALIZED)
  346. CASE_SCODE(CO_E_CANTDETERMINECLASS)
  347. CASE_SCODE(CO_E_CLASSSTRING)
  348. CASE_SCODE(CO_E_IIDSTRING)
  349. CASE_SCODE(CO_E_APPNOTFOUND)
  350. CASE_SCODE(CO_E_APPSINGLEUSE)
  351. CASE_SCODE(CO_E_ERRORINAPP)
  352. CASE_SCODE(CO_E_DLLNOTFOUND)
  353. CASE_SCODE(CO_E_ERRORINDLL)
  354. CASE_SCODE(CO_E_WRONGOSFORAPP)
  355. CASE_SCODE(CO_E_OBJNOTREG)
  356. CASE_SCODE(CO_E_OBJISREG)
  357. CASE_SCODE(CO_E_OBJNOTCONNECTED)
  358. CASE_SCODE(CO_E_APPDIDNTREG)
  359. CASE_SCODE(CLASS_E_NOAGGREGATION)
  360. CASE_SCODE(REGDB_E_READREGDB)
  361. CASE_SCODE(REGDB_E_WRITEREGDB)
  362. CASE_SCODE(REGDB_E_KEYMISSING)
  363. CASE_SCODE(REGDB_E_INVALIDVALUE)
  364. CASE_SCODE(REGDB_E_CLASSNOTREG)
  365. CASE_SCODE(REGDB_E_IIDNOTREG)
  366. #if defined(NO_NTOLEBUGS)
  367. CASE_SCODE(RPC_E_SERVER_DIED)
  368. CASE_SCODE(RPC_E_CALL_REJECTED)
  369. CASE_SCODE(RPC_E_CALL_CANCELED)
  370. CASE_SCODE(RPC_E_CANTPOST_INSENDCALL)
  371. CASE_SCODE(RPC_E_CANTCALLOUT_INASYNCCALL)
  372. CASE_SCODE(RPC_E_CANTCALLOUT_INEXTERNALCALL)
  373. CASE_SCODE(RPC_E_CONNECTION_TERMINATED)
  374. CASE_SCODE(RPC_E_CLIENT_DIED)
  375. CASE_SCODE(RPC_E_INVALID_DATAPACKET)
  376. CASE_SCODE(RPC_E_CANTTRANSMIT_CALL)
  377. CASE_SCODE(RPC_E_CLIENT_CANTMARSHAL_DATA)
  378. CASE_SCODE(RPC_E_CLIENT_CANTUNMARSHAL_DATA)
  379. CASE_SCODE(RPC_E_SERVER_CANTMARSHAL_DATA)
  380. CASE_SCODE(RPC_E_SERVER_CANTUNMARSHAL_DATA)
  381. CASE_SCODE(RPC_E_INVALID_DATA)
  382. CASE_SCODE(RPC_E_INVALID_PARAMETER)
  383. CASE_SCODE(RPC_E_UNEXPECTED)
  384. #endif // NO_NTOLEBUGS
  385. /* SCODE's defined in MONIKER.H */
  386. CASE_SCODE(MK_E_CONNECTMANUALLY)
  387. CASE_SCODE(MK_E_EXCEEDEDDEADLINE)
  388. CASE_SCODE(MK_E_NEEDGENERIC)
  389. CASE_SCODE(MK_E_UNAVAILABLE)
  390. CASE_SCODE(MK_E_SYNTAX)
  391. CASE_SCODE(MK_E_NOOBJECT)
  392. CASE_SCODE(MK_E_INVALIDEXTENSION)
  393. CASE_SCODE(MK_E_INTERMEDIATEINTERFACENOTSUPPORTED)
  394. CASE_SCODE(MK_E_NOTBINDABLE)
  395. CASE_SCODE(MK_E_NOTBOUND)
  396. CASE_SCODE(MK_E_CANTOPENFILE)
  397. CASE_SCODE(MK_E_MUSTBOTHERUSER)
  398. CASE_SCODE(MK_E_NOINVERSE)
  399. CASE_SCODE(MK_E_NOSTORAGE)
  400. CASE_SCODE(MK_S_REDUCED_TO_SELF)
  401. CASE_SCODE(MK_S_ME)
  402. CASE_SCODE(MK_S_HIM)
  403. CASE_SCODE(MK_S_US)
  404. #if defined(NO_NTOLEBUGS)
  405. CASE_SCODE(MK_S_MONIKERALREADYREGISTERED)
  406. #endif //NO_NTOLEBUGS
  407. default:
  408. lpstr = TEXT(" <UNKNOWN SCODE 0x%lx>\n");
  409. break;
  410. }
  411. #undef CASE_SCODE
  412. TCHAR achTemp[256];
  413. #ifdef UNICODE
  414. wsprintf(achTemp, TEXT("%ws"), lpstr);
  415. #else
  416. wsprintf(achTemp, TEXT("%s"), lpstr);
  417. #endif
  418. OutputDebugString( achTemp );
  419. return r;
  420. }
  421. #endif // DBG == 1