UTIL.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:24k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // util.cpp
  2. // This is a part of the Microsoft Foundation Classes C++ library.
  3. // Copyright (C) 1992-1998 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // This source code is only intended as a supplement to the
  7. // Microsoft Foundation Classes Reference and related
  8. // electronic documentation provided with the library.
  9. // See these sources for detailed information regarding the
  10. // Microsoft Foundation Classes product.
  11. #include "stdafx.h"
  12. #include "util.h"
  13. #include "shadow.h"
  14. /////
  15. // Utility functions
  16. //
  17. static TCHAR szBold[]       = _T("Bold") ;
  18. static TCHAR szItalic[]      = _T("Italic") ;
  19. static TCHAR szBoldItalic[]  = _T("Bold Italic") ;
  20. static TCHAR szRegular[]     = _T("Regular") ;
  21. void ErrorMessage( const CString& str, HRESULT hr )
  22. {
  23. LPVOID lpMessageBuffer = NULL ;
  24. if (FormatMessage(
  25.   FORMAT_MESSAGE_ALLOCATE_BUFFER |
  26.   FORMAT_MESSAGE_FROM_SYSTEM,
  27.   NULL,
  28.   GetScode(hr),
  29.   MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  30.   (LPTSTR) &lpMessageBuffer,
  31.   0,
  32.   NULL ))
  33. {
  34. CString str2 ;
  35. str2.Format( _T("n%s %s"), (LPCTSTR)lpMessageBuffer, (LPCTSTR)HRtoString(hr) ) ;
  36. str2 = str + str2 ;
  37. LocalFree(lpMessageBuffer) ;
  38. AfxMessageBox( str2 ) ;
  39. }
  40. else
  41. {
  42. CString str2 ;
  43. str2.Format( _T("n<No system message defined> %s"), (LPCTSTR)HRtoString(hr) ) ;
  44. str2 = str + str2 ;
  45. AfxMessageBox( str2 ) ;
  46. }
  47. }
  48. // Stolen from OLEMISC.CPP in the MFC 3.0 source.  Function names
  49. // changed from Afx* to _*.
  50. //
  51. #define _countof(array) (sizeof(array)/sizeof(array[0]))
  52. LPCTSTR AFXAPI _GetScodeString(SCODE sc)
  53. {
  54. struct SCODE_ENTRY
  55. {
  56. SCODE sc;
  57. LPCTSTR lpszName;
  58. };
  59. #define MAKE_SCODE_ENTRY(sc)    { sc, _T(#sc) }
  60. static const SCODE_ENTRY scNameTable[] =
  61. {
  62. MAKE_SCODE_ENTRY(S_OK),
  63. MAKE_SCODE_ENTRY(S_FALSE),
  64. MAKE_SCODE_ENTRY(CACHE_S_FORMATETC_NOTSUPPORTED),
  65. MAKE_SCODE_ENTRY(CACHE_S_SAMECACHE),
  66. MAKE_SCODE_ENTRY(CACHE_S_SOMECACHES_NOTUPDATED),
  67. MAKE_SCODE_ENTRY(CONVERT10_S_NO_PRESENTATION),
  68. MAKE_SCODE_ENTRY(DATA_S_SAMEFORMATETC),
  69. MAKE_SCODE_ENTRY(DRAGDROP_S_CANCEL),
  70. MAKE_SCODE_ENTRY(DRAGDROP_S_DROP),
  71. MAKE_SCODE_ENTRY(DRAGDROP_S_USEDEFAULTCURSORS),
  72. MAKE_SCODE_ENTRY(INPLACE_S_TRUNCATED),
  73. MAKE_SCODE_ENTRY(MK_S_HIM),
  74. MAKE_SCODE_ENTRY(MK_S_ME),
  75. MAKE_SCODE_ENTRY(MK_S_MONIKERALREADYREGISTERED),
  76. MAKE_SCODE_ENTRY(MK_S_REDUCED_TO_SELF),
  77. MAKE_SCODE_ENTRY(MK_S_US),
  78. MAKE_SCODE_ENTRY(OLE_S_MAC_CLIPFORMAT),
  79. MAKE_SCODE_ENTRY(OLE_S_STATIC),
  80. MAKE_SCODE_ENTRY(OLE_S_USEREG),
  81. MAKE_SCODE_ENTRY(OLEOBJ_S_CANNOT_DOVERB_NOW),
  82. MAKE_SCODE_ENTRY(OLEOBJ_S_INVALIDHWND),
  83. MAKE_SCODE_ENTRY(OLEOBJ_S_INVALIDVERB),
  84. MAKE_SCODE_ENTRY(OLEOBJ_S_LAST),
  85. MAKE_SCODE_ENTRY(STG_S_CONVERTED),
  86. MAKE_SCODE_ENTRY(VIEW_S_ALREADY_FROZEN),
  87. MAKE_SCODE_ENTRY(E_UNEXPECTED),
  88. MAKE_SCODE_ENTRY(E_NOTIMPL),
  89. MAKE_SCODE_ENTRY(E_OUTOFMEMORY),
  90. MAKE_SCODE_ENTRY(E_INVALIDARG),
  91. MAKE_SCODE_ENTRY(E_NOINTERFACE),
  92. MAKE_SCODE_ENTRY(E_POINTER),
  93. MAKE_SCODE_ENTRY(E_HANDLE),
  94. MAKE_SCODE_ENTRY(E_ABORT),
  95. MAKE_SCODE_ENTRY(E_FAIL),
  96. MAKE_SCODE_ENTRY(E_ACCESSDENIED),
  97. MAKE_SCODE_ENTRY(CACHE_E_NOCACHE_UPDATED),
  98. MAKE_SCODE_ENTRY(CLASS_E_CLASSNOTAVAILABLE),
  99. MAKE_SCODE_ENTRY(CLASS_E_NOAGGREGATION),
  100. MAKE_SCODE_ENTRY(CLIPBRD_E_BAD_DATA),
  101. MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_CLOSE),
  102. MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_EMPTY),
  103. MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_OPEN),
  104. MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_SET),
  105. MAKE_SCODE_ENTRY(CO_E_ALREADYINITIALIZED),
  106. MAKE_SCODE_ENTRY(CO_E_APPDIDNTREG),
  107. MAKE_SCODE_ENTRY(CO_E_APPNOTFOUND),
  108. MAKE_SCODE_ENTRY(CO_E_APPSINGLEUSE),
  109. MAKE_SCODE_ENTRY(CO_E_BAD_PATH),
  110. MAKE_SCODE_ENTRY(CO_E_CANTDETERMINECLASS),
  111. MAKE_SCODE_ENTRY(CO_E_CLASS_CREATE_FAILED),
  112. MAKE_SCODE_ENTRY(CO_E_CLASSSTRING),
  113. MAKE_SCODE_ENTRY(CO_E_DLLNOTFOUND),
  114. MAKE_SCODE_ENTRY(CO_E_ERRORINAPP),
  115. MAKE_SCODE_ENTRY(CO_E_ERRORINDLL),
  116. MAKE_SCODE_ENTRY(CO_E_IIDSTRING),
  117. MAKE_SCODE_ENTRY(CO_E_NOTINITIALIZED),
  118. MAKE_SCODE_ENTRY(CO_E_OBJISREG),
  119. MAKE_SCODE_ENTRY(CO_E_OBJNOTCONNECTED),
  120. MAKE_SCODE_ENTRY(CO_E_OBJNOTREG),
  121. MAKE_SCODE_ENTRY(CO_E_OBJSRV_RPC_FAILURE),
  122. MAKE_SCODE_ENTRY(CO_E_SCM_ERROR),
  123. MAKE_SCODE_ENTRY(CO_E_SCM_RPC_FAILURE),
  124. MAKE_SCODE_ENTRY(CO_E_SERVER_EXEC_FAILURE),
  125. MAKE_SCODE_ENTRY(CO_E_SERVER_STOPPING),
  126. MAKE_SCODE_ENTRY(CO_E_WRONGOSFORAPP),
  127. MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_BITMAP_TO_DIB),
  128. MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_FMT),
  129. MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_GET),
  130. MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_PUT),
  131. MAKE_SCODE_ENTRY(CONVERT10_E_STG_DIB_TO_BITMAP),
  132. MAKE_SCODE_ENTRY(CONVERT10_E_STG_FMT),
  133. MAKE_SCODE_ENTRY(CONVERT10_E_STG_NO_STD_STREAM),
  134. MAKE_SCODE_ENTRY(DISP_E_ARRAYISLOCKED),
  135. MAKE_SCODE_ENTRY(DISP_E_BADCALLEE),
  136. MAKE_SCODE_ENTRY(DISP_E_BADINDEX),
  137. MAKE_SCODE_ENTRY(DISP_E_BADPARAMCOUNT),
  138. MAKE_SCODE_ENTRY(DISP_E_BADVARTYPE),
  139. MAKE_SCODE_ENTRY(DISP_E_EXCEPTION),
  140. MAKE_SCODE_ENTRY(DISP_E_MEMBERNOTFOUND),
  141. MAKE_SCODE_ENTRY(DISP_E_NONAMEDARGS),
  142. MAKE_SCODE_ENTRY(DISP_E_NOTACOLLECTION),
  143. MAKE_SCODE_ENTRY(DISP_E_OVERFLOW),
  144. MAKE_SCODE_ENTRY(DISP_E_PARAMNOTFOUND),
  145. MAKE_SCODE_ENTRY(DISP_E_PARAMNOTOPTIONAL),
  146. MAKE_SCODE_ENTRY(DISP_E_TYPEMISMATCH),
  147. MAKE_SCODE_ENTRY(DISP_E_UNKNOWNINTERFACE),
  148. MAKE_SCODE_ENTRY(DISP_E_UNKNOWNLCID),
  149. MAKE_SCODE_ENTRY(DISP_E_UNKNOWNNAME),
  150. MAKE_SCODE_ENTRY(DRAGDROP_E_ALREADYREGISTERED),
  151. MAKE_SCODE_ENTRY(DRAGDROP_E_INVALIDHWND),
  152. MAKE_SCODE_ENTRY(DRAGDROP_E_NOTREGISTERED),
  153. MAKE_SCODE_ENTRY(DV_E_CLIPFORMAT),
  154. MAKE_SCODE_ENTRY(DV_E_DVASPECT),
  155. MAKE_SCODE_ENTRY(DV_E_DVTARGETDEVICE),
  156. MAKE_SCODE_ENTRY(DV_E_DVTARGETDEVICE_SIZE),
  157. MAKE_SCODE_ENTRY(DV_E_FORMATETC),
  158. MAKE_SCODE_ENTRY(DV_E_LINDEX),
  159. MAKE_SCODE_ENTRY(DV_E_NOIVIEWOBJECT),
  160. MAKE_SCODE_ENTRY(DV_E_STATDATA),
  161. MAKE_SCODE_ENTRY(DV_E_STGMEDIUM),
  162. MAKE_SCODE_ENTRY(DV_E_TYMED),
  163. MAKE_SCODE_ENTRY(INPLACE_E_NOTOOLSPACE),
  164. MAKE_SCODE_ENTRY(INPLACE_E_NOTUNDOABLE),
  165. MAKE_SCODE_ENTRY(MEM_E_INVALID_LINK),
  166. MAKE_SCODE_ENTRY(MEM_E_INVALID_ROOT),
  167. MAKE_SCODE_ENTRY(MEM_E_INVALID_SIZE),
  168. MAKE_SCODE_ENTRY(MK_E_CANTOPENFILE),
  169. MAKE_SCODE_ENTRY(MK_E_CONNECTMANUALLY),
  170. MAKE_SCODE_ENTRY(MK_E_ENUMERATION_FAILED),
  171. MAKE_SCODE_ENTRY(MK_E_EXCEEDEDDEADLINE),
  172. MAKE_SCODE_ENTRY(MK_E_INTERMEDIATEINTERFACENOTSUPPORTED),
  173. MAKE_SCODE_ENTRY(MK_E_INVALIDEXTENSION),
  174. MAKE_SCODE_ENTRY(MK_E_MUSTBOTHERUSER),
  175. MAKE_SCODE_ENTRY(MK_E_NEEDGENERIC),
  176. MAKE_SCODE_ENTRY(MK_E_NO_NORMALIZED),
  177. MAKE_SCODE_ENTRY(MK_E_NOINVERSE),
  178. MAKE_SCODE_ENTRY(MK_E_NOOBJECT),
  179. MAKE_SCODE_ENTRY(MK_E_NOPREFIX),
  180. MAKE_SCODE_ENTRY(MK_E_NOSTORAGE),
  181. MAKE_SCODE_ENTRY(MK_E_NOTBINDABLE),
  182. MAKE_SCODE_ENTRY(MK_E_NOTBOUND),
  183. MAKE_SCODE_ENTRY(MK_E_SYNTAX),
  184. MAKE_SCODE_ENTRY(MK_E_UNAVAILABLE),
  185. MAKE_SCODE_ENTRY(OLE_E_ADVF),
  186. MAKE_SCODE_ENTRY(OLE_E_ADVISENOTSUPPORTED),
  187. MAKE_SCODE_ENTRY(OLE_E_BLANK),
  188. MAKE_SCODE_ENTRY(OLE_E_CANT_BINDTOSOURCE),
  189. MAKE_SCODE_ENTRY(OLE_E_CANT_GETMONIKER),
  190. MAKE_SCODE_ENTRY(OLE_E_CANTCONVERT),
  191. MAKE_SCODE_ENTRY(OLE_E_CLASSDIFF),
  192. MAKE_SCODE_ENTRY(OLE_E_ENUM_NOMORE),
  193. MAKE_SCODE_ENTRY(OLE_E_INVALIDHWND),
  194. MAKE_SCODE_ENTRY(OLE_E_INVALIDRECT),
  195. MAKE_SCODE_ENTRY(OLE_E_NOCACHE),
  196. MAKE_SCODE_ENTRY(OLE_E_NOCONNECTION),
  197. MAKE_SCODE_ENTRY(OLE_E_NOSTORAGE),
  198. MAKE_SCODE_ENTRY(OLE_E_NOT_INPLACEACTIVE),
  199. MAKE_SCODE_ENTRY(OLE_E_NOTRUNNING),
  200. MAKE_SCODE_ENTRY(OLE_E_OLEVERB),
  201. MAKE_SCODE_ENTRY(OLE_E_PROMPTSAVECANCELLED),
  202. MAKE_SCODE_ENTRY(OLE_E_STATIC),
  203. MAKE_SCODE_ENTRY(OLE_E_WRONGCOMPOBJ),
  204. MAKE_SCODE_ENTRY(OLEOBJ_E_INVALIDVERB),
  205. MAKE_SCODE_ENTRY(OLEOBJ_E_NOVERBS),
  206. MAKE_SCODE_ENTRY(REGDB_E_CLASSNOTREG),
  207. MAKE_SCODE_ENTRY(REGDB_E_IIDNOTREG),
  208. MAKE_SCODE_ENTRY(REGDB_E_INVALIDVALUE),
  209. MAKE_SCODE_ENTRY(REGDB_E_KEYMISSING),
  210. MAKE_SCODE_ENTRY(REGDB_E_READREGDB),
  211. MAKE_SCODE_ENTRY(REGDB_E_WRITEREGDB),
  212. MAKE_SCODE_ENTRY(RPC_E_ATTEMPTED_MULTITHREAD),
  213. MAKE_SCODE_ENTRY(RPC_E_CALL_CANCELED),
  214. MAKE_SCODE_ENTRY(RPC_E_CALL_REJECTED),
  215. MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_AGAIN),
  216. MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_INASYNCCALL),
  217. MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_INEXTERNALCALL),
  218. MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_ININPUTSYNCCALL),
  219. MAKE_SCODE_ENTRY(RPC_E_CANTPOST_INSENDCALL),
  220. MAKE_SCODE_ENTRY(RPC_E_CANTTRANSMIT_CALL),
  221. MAKE_SCODE_ENTRY(RPC_E_CHANGED_MODE),
  222. MAKE_SCODE_ENTRY(RPC_E_CLIENT_CANTMARSHAL_DATA),
  223. MAKE_SCODE_ENTRY(RPC_E_CLIENT_CANTUNMARSHAL_DATA),
  224. MAKE_SCODE_ENTRY(RPC_E_CLIENT_DIED),
  225. MAKE_SCODE_ENTRY(RPC_E_CONNECTION_TERMINATED),
  226. MAKE_SCODE_ENTRY(RPC_E_DISCONNECTED),
  227. MAKE_SCODE_ENTRY(RPC_E_FAULT),
  228. MAKE_SCODE_ENTRY(RPC_E_INVALID_CALLDATA),
  229. MAKE_SCODE_ENTRY(RPC_E_INVALID_DATA),
  230. MAKE_SCODE_ENTRY(RPC_E_INVALID_DATAPACKET),
  231. MAKE_SCODE_ENTRY(RPC_E_INVALID_PARAMETER),
  232. MAKE_SCODE_ENTRY(RPC_E_INVALIDMETHOD),
  233. MAKE_SCODE_ENTRY(RPC_E_NOT_REGISTERED),
  234. MAKE_SCODE_ENTRY(RPC_E_OUT_OF_RESOURCES),
  235. MAKE_SCODE_ENTRY(RPC_E_RETRY),
  236. MAKE_SCODE_ENTRY(RPC_E_SERVER_CANTMARSHAL_DATA),
  237. MAKE_SCODE_ENTRY(RPC_E_SERVER_CANTUNMARSHAL_DATA),
  238. MAKE_SCODE_ENTRY(RPC_E_SERVER_DIED),
  239. MAKE_SCODE_ENTRY(RPC_E_SERVER_DIED_DNE),
  240. MAKE_SCODE_ENTRY(RPC_E_SERVERCALL_REJECTED),
  241. MAKE_SCODE_ENTRY(RPC_E_SERVERCALL_RETRYLATER),
  242. MAKE_SCODE_ENTRY(RPC_E_SERVERFAULT),
  243. MAKE_SCODE_ENTRY(RPC_E_SYS_CALL_FAILED),
  244. MAKE_SCODE_ENTRY(RPC_E_THREAD_NOT_INIT),
  245. MAKE_SCODE_ENTRY(RPC_E_UNEXPECTED),
  246. MAKE_SCODE_ENTRY(RPC_E_WRONG_THREAD),
  247. MAKE_SCODE_ENTRY(STG_E_ABNORMALAPIEXIT),
  248. MAKE_SCODE_ENTRY(STG_E_ACCESSDENIED),
  249. MAKE_SCODE_ENTRY(STG_E_CANTSAVE),
  250. MAKE_SCODE_ENTRY(STG_E_DISKISWRITEPROTECTED),
  251. MAKE_SCODE_ENTRY(STG_E_EXTANTMARSHALLINGS),
  252. MAKE_SCODE_ENTRY(STG_E_FILEALREADYEXISTS),
  253. MAKE_SCODE_ENTRY(STG_E_FILENOTFOUND),
  254. MAKE_SCODE_ENTRY(STG_E_INSUFFICIENTMEMORY),
  255. MAKE_SCODE_ENTRY(STG_E_INUSE),
  256. MAKE_SCODE_ENTRY(STG_E_INVALIDFLAG),
  257. MAKE_SCODE_ENTRY(STG_E_INVALIDFUNCTION),
  258. MAKE_SCODE_ENTRY(STG_E_INVALIDHANDLE),
  259. MAKE_SCODE_ENTRY(STG_E_INVALIDHEADER),
  260. MAKE_SCODE_ENTRY(STG_E_INVALIDNAME),
  261. MAKE_SCODE_ENTRY(STG_E_INVALIDPARAMETER),
  262. MAKE_SCODE_ENTRY(STG_E_INVALIDPOINTER),
  263. MAKE_SCODE_ENTRY(STG_E_LOCKVIOLATION),
  264. MAKE_SCODE_ENTRY(STG_E_MEDIUMFULL),
  265. MAKE_SCODE_ENTRY(STG_E_NOMOREFILES),
  266. MAKE_SCODE_ENTRY(STG_E_NOTCURRENT),
  267. MAKE_SCODE_ENTRY(STG_E_NOTFILEBASEDSTORAGE),
  268. MAKE_SCODE_ENTRY(STG_E_OLDDLL),
  269. MAKE_SCODE_ENTRY(STG_E_OLDFORMAT),
  270. MAKE_SCODE_ENTRY(STG_E_PATHNOTFOUND),
  271. MAKE_SCODE_ENTRY(STG_E_READFAULT),
  272. MAKE_SCODE_ENTRY(STG_E_REVERTED),
  273. MAKE_SCODE_ENTRY(STG_E_SEEKERROR),
  274. MAKE_SCODE_ENTRY(STG_E_SHAREREQUIRED),
  275. MAKE_SCODE_ENTRY(STG_E_SHAREVIOLATION),
  276. MAKE_SCODE_ENTRY(STG_E_TOOMANYOPENFILES),
  277. MAKE_SCODE_ENTRY(STG_E_UNIMPLEMENTEDFUNCTION),
  278. MAKE_SCODE_ENTRY(STG_E_UNKNOWN),
  279. MAKE_SCODE_ENTRY(STG_E_WRITEFAULT),
  280. MAKE_SCODE_ENTRY(TYPE_E_AMBIGUOUSNAME),
  281. MAKE_SCODE_ENTRY(TYPE_E_BADMODULEKIND),
  282. MAKE_SCODE_ENTRY(TYPE_E_BUFFERTOOSMALL),
  283. MAKE_SCODE_ENTRY(TYPE_E_CANTCREATETMPFILE),
  284. MAKE_SCODE_ENTRY(TYPE_E_CANTLOADLIBRARY),
  285. MAKE_SCODE_ENTRY(TYPE_E_CIRCULARTYPE),
  286. MAKE_SCODE_ENTRY(TYPE_E_DLLFUNCTIONNOTFOUND),
  287. MAKE_SCODE_ENTRY(TYPE_E_DUPLICATEID),
  288. MAKE_SCODE_ENTRY(TYPE_E_ELEMENTNOTFOUND),
  289. MAKE_SCODE_ENTRY(TYPE_E_INCONSISTENTPROPFUNCS),
  290. MAKE_SCODE_ENTRY(TYPE_E_INVALIDSTATE),
  291. MAKE_SCODE_ENTRY(TYPE_E_INVDATAREAD),
  292. MAKE_SCODE_ENTRY(TYPE_E_IOERROR),
  293. MAKE_SCODE_ENTRY(TYPE_E_LIBNOTREGISTERED),
  294. MAKE_SCODE_ENTRY(TYPE_E_NAMECONFLICT),
  295. MAKE_SCODE_ENTRY(TYPE_E_OUTOFBOUNDS),
  296. MAKE_SCODE_ENTRY(TYPE_E_QUALIFIEDNAMEDISALLOWED),
  297. MAKE_SCODE_ENTRY(TYPE_E_REGISTRYACCESS),
  298. MAKE_SCODE_ENTRY(TYPE_E_SIZETOOBIG),
  299. MAKE_SCODE_ENTRY(TYPE_E_TYPEMISMATCH),
  300. MAKE_SCODE_ENTRY(TYPE_E_UNDEFINEDTYPE),
  301. MAKE_SCODE_ENTRY(TYPE_E_UNKNOWNLCID),
  302. MAKE_SCODE_ENTRY(TYPE_E_UNSUPFORMAT),
  303. MAKE_SCODE_ENTRY(TYPE_E_WRONGTYPEKIND),
  304. MAKE_SCODE_ENTRY(VIEW_E_DRAW),
  305. };
  306. #undef MAKE_SCODE_ENTRY
  307. // look for it in the table
  308. for (int i = 0; i < _countof(scNameTable); i++)
  309. {
  310. if (sc == scNameTable[i].sc)
  311. return scNameTable[i].lpszName;
  312. }
  313. static TCHAR szError[32] ;
  314. wsprintf( szError, _T("%#08.8x"), sc ) ;
  315. return szError ;    // not found
  316. }
  317. LPCTSTR AFXAPI _GetScodeRangeString(SCODE sc)
  318. {
  319. struct RANGE_ENTRY
  320. {
  321. SCODE scFirst;
  322. SCODE scLast;
  323. LPCTSTR lpszName;
  324. };
  325. #define MAKE_RANGE_ENTRY(scRange) 
  326. { scRange##_FIRST, scRange##_LAST, 
  327. _T(#scRange) _T("_FIRST...") _T(#scRange) _T("_LAST") }
  328. static const RANGE_ENTRY scRangeTable[] =
  329. {
  330. MAKE_RANGE_ENTRY(CACHE_E),
  331. MAKE_RANGE_ENTRY(CACHE_S),
  332. MAKE_RANGE_ENTRY(CLASSFACTORY_E),
  333. MAKE_RANGE_ENTRY(CLASSFACTORY_S),
  334. MAKE_RANGE_ENTRY(CLIENTSITE_E),
  335. MAKE_RANGE_ENTRY(CLIENTSITE_S),
  336. MAKE_RANGE_ENTRY(CLIPBRD_E),
  337. MAKE_RANGE_ENTRY(CLIPBRD_S),
  338. MAKE_RANGE_ENTRY(CONVERT10_E),
  339. MAKE_RANGE_ENTRY(CONVERT10_S),
  340. MAKE_RANGE_ENTRY(CO_E),
  341. MAKE_RANGE_ENTRY(CO_S),
  342. MAKE_RANGE_ENTRY(DATA_E),
  343. MAKE_RANGE_ENTRY(DATA_S),
  344. MAKE_RANGE_ENTRY(DRAGDROP_E),
  345. MAKE_RANGE_ENTRY(DRAGDROP_S),
  346. MAKE_RANGE_ENTRY(ENUM_E),
  347. MAKE_RANGE_ENTRY(ENUM_S),
  348. MAKE_RANGE_ENTRY(INPLACE_E),
  349. MAKE_RANGE_ENTRY(INPLACE_S),
  350. MAKE_RANGE_ENTRY(MARSHAL_E),
  351. MAKE_RANGE_ENTRY(MARSHAL_S),
  352. MAKE_RANGE_ENTRY(MK_E),
  353. MAKE_RANGE_ENTRY(MK_S),
  354. MAKE_RANGE_ENTRY(OLEOBJ_E),
  355. MAKE_RANGE_ENTRY(OLEOBJ_S),
  356. MAKE_RANGE_ENTRY(OLE_E),
  357. MAKE_RANGE_ENTRY(OLE_S),
  358. MAKE_RANGE_ENTRY(REGDB_E),
  359. MAKE_RANGE_ENTRY(REGDB_S),
  360. MAKE_RANGE_ENTRY(VIEW_E),
  361. MAKE_RANGE_ENTRY(VIEW_S),
  362. };
  363. #undef MAKE_RANGE_ENTRY
  364. // look for it in the table
  365. for (int i = 0; i < _countof(scRangeTable); i++)
  366. {
  367. if (sc >= scRangeTable[i].scFirst && sc <= scRangeTable[i].scLast)
  368. return scRangeTable[i].lpszName;
  369. }
  370. return NULL;    // not found
  371. }
  372. LPCTSTR AFXAPI _GetSeverityString(SCODE sc)
  373. {
  374. static const TCHAR* rgszSEVERITY[] =
  375. {
  376. _T("SEVERITY_SUCCESS"),
  377. _T("SEVERITY_ERROR"),
  378. };
  379. return rgszSEVERITY[SCODE_SEVERITY(sc)];
  380. }
  381. LPCTSTR AFXAPI _GetFacilityString(SCODE sc)
  382. {
  383. static const TCHAR* rgszFACILITY[] =
  384. {
  385. _T("FACILITY_NULL"),
  386. _T("FACILITY_RPC"),
  387. _T("FACILITY_DISPATCH"),
  388. _T("FACILITY_STORAGE"),
  389. _T("FACILITY_ITF"),
  390. _T("FACILITY_0x05"),
  391. _T("FACILITY_0x06"),
  392. _T("FACILITY_WIN32"),
  393. _T("FACILITY_WINDOWS"),
  394. };
  395. if (SCODE_FACILITY(sc) >= _countof(rgszFACILITY))
  396. return _T("<Unknown Facility>");
  397. return rgszFACILITY[SCODE_FACILITY(sc)];
  398. }
  399. LPCTSTR AFXAPI _GetFullScodeString(SCODE sc)
  400. {
  401. static TCHAR szBuf[128];
  402. LPCTSTR lpsz;
  403. if ((lpsz = _GetScodeString(sc)) != NULL)
  404. {
  405. // found exact match
  406. wsprintf(szBuf, _T("%s ($%08lX)"), lpsz, sc);
  407. }
  408. else if ((lpsz = _GetScodeRangeString(sc)) != NULL)
  409. {
  410. // found suitable range
  411. wsprintf(szBuf, _T("range: %s ($%08lX)"), lpsz, sc);
  412. }
  413. else
  414. {
  415. // not found at all -- split it up into its parts
  416. wsprintf(szBuf, _T("severity: %s, facility: %s ($%08lX)"),
  417. _GetSeverityString(sc), _GetFacilityString(sc), sc);
  418. }
  419. return szBuf;
  420. }
  421. LPTSTR HRtoString( HRESULT hr )
  422. {
  423. SCODE   sc ;
  424. sc = GetScode( hr ) ;
  425. #ifdef FORMATMSG
  426. LPVOID lpMessageBuffer ;
  427. if (FormatMessage(
  428.   FORMAT_MESSAGE_ALLOCATE_BUFFER |
  429.   FORMAT_MESSAGE_FROM_SYSTEM,
  430.   NULL,
  431.   sc,
  432.   MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  433.   (LPTSTR) &lpMessageBuffer,
  434.   0,
  435.   NULL ))
  436. {
  437. wsprintf( sz, _T( "%s (0x%lx)" ), (LPTSTR)lpMessageBuffer, sc);
  438. LocalFree(lpMessageBuffer) ;
  439. }
  440. else
  441. {
  442. wsprintf( sz, _T( "Unknown Scode (0x%lx)" ), sc);
  443. }
  444. #endif // !FORMATMSG
  445. return (LPTSTR)_GetFullScodeString(sc) ;
  446. }
  447. #include <ctype.h>
  448. // Very useful API that parses a LONG out of a string, updating
  449. // the string pointer before it returns.
  450. //
  451. LONG WINAPI ParseOffNumber( LPTSTR FAR *lplp, LPINT lpConv )
  452. {
  453. LPTSTR lp = *lplp;
  454. LONG  lValue=0;
  455. int   sign=1;
  456. while( isspace(*lp) )
  457. lp++;
  458. if( *lp=='-' )
  459. {
  460. sign = -1;
  461. lp++;
  462. }
  463. if (*lp=='')
  464. {
  465. *lpConv = FALSE;
  466. return 0L;
  467. }
  468. // Is it a decimal number
  469. #ifdef UNICODE
  470. if( _wcsnicmp( lp, (LPTSTR)_T("0x"),2) )
  471. #else
  472. #ifdef WIN32
  473. if( strnicmp( lp, (LPTSTR)_T("0x"),2) )
  474. #else
  475. if( _fstrnicmp( lp, (LPTSTR)_T("0x"),2) )
  476. #endif
  477. #endif
  478. {
  479. while( isdigit(*lp) )
  480. {
  481. lValue *= 10;
  482. lValue += (*lp - (TCHAR)'0');
  483. lp++;
  484. }
  485. }
  486. else
  487. {
  488. lp+=2;
  489. while( isxdigit(*lp) )
  490. {
  491. lValue *= 16;
  492. if( isdigit(*lp) )
  493. lValue += (*lp - (TCHAR)'0');
  494. else
  495. lValue += (toupper(*lp) - (TCHAR)'A' + 10);
  496. lp++;
  497. }
  498. }
  499. while( isspace(*lp) )
  500. lp++;
  501. lValue *= (long)sign;
  502. if (*lp==(TCHAR)',')
  503. {
  504. lp++;
  505. while( isspace(*lp) )
  506. lp++;
  507. *lplp = lp;
  508. if (lpConv)
  509. *lpConv = (int)TRUE;
  510. }
  511. else
  512. {
  513. *lplp = lp;
  514. if (lpConv)
  515. *lpConv = (int)(*lp=='');
  516. }
  517. return lValue;
  518. } //*** ParseOffNumber
  519. static TCHAR* BASED_CODE g_rgszVT[] =
  520. {
  521. _T("void"),             //VT_EMPTY           = 0,   /* [V]   [P]  nothing                     */
  522. _T("null"),             //VT_NULL            = 1,   /* [V]        SQL style Null              */
  523. _T("short"),            //VT_I2              = 2,   /* [V][T][P]  2 byte signed int           */
  524. _T("long"),             //VT_I4              = 3,   /* [V][T][P]  4 byte signed int           */
  525. _T("single"),           //VT_R4              = 4,   /* [V][T][P]  4 byte real                 */
  526. _T("double"),           //VT_R8              = 5,   /* [V][T][P]  8 byte real                 */
  527. _T("CURRENCY"),         //VT_CY              = 6,   /* [V][T][P]  currency                    */
  528. _T("DATE"),             //VT_DATE            = 7,   /* [V][T][P]  date                        */
  529. _T("BSTR"),             //VT_BSTR            = 8,   /* [V][T][P]  binary string               */
  530. _T("IDispatch*"),       //VT_DISPATCH        = 9,   /* [V][T]     IDispatch FAR*              */
  531. _T("SCODE"),            //VT_ERROR           = 10,  /* [V][T]     SCODE                       */
  532. _T("boolean"),          //VT_BOOL            = 11,  /* [V][T][P]  True=-1, False=0            */
  533. _T("VARIANT"),          //VT_VARIANT         = 12,  /* [V][T][P]  VARIANT FAR*                */
  534. _T("IUnknown*"),        //VT_UNKNOWN         = 13,  /* [V][T]     IUnknown FAR*               */
  535. _T("wchar_t"),          //VT_WBSTR           = 14,  /* [V][T]     wide binary string          */
  536. _T(""),                 //                   = 15,
  537. _T("char"),             //VT_I1              = 16,  /*    [T]     signed char                 */
  538. _T("unsigned char"),             //VT_UI1             = 17,  /*    [T]     unsigned char               */
  539. _T("unsigned short"),           //VT_UI2             = 18,  /*    [T]     unsigned short              */
  540. _T("unsigned long"),            //VT_UI4             = 19,  /*    [T]     unsigned short              */
  541. _T("int64"),            //VT_I8              = 20,  /*    [T][P]  signed 64-bit int           */
  542. _T("uint64"),           //VT_UI8             = 21,  /*    [T]     unsigned 64-bit int         */
  543. _T("int"),              //VT_INT             = 22,  /*    [T]     signed machine int          */
  544. _T("unsigned int"),             //VT_UINT            = 23,  /*    [T]     unsigned machine int        */
  545. _T("void"),             //VT_VOID            = 24,  /*    [T]     C style void                */
  546. _T("HRESULT"),          //VT_HRESULT         = 25,  /*    [T]                                 */
  547. _T("PTR"),              //VT_PTR             = 26,  /*    [T]     pointer type                */
  548. _T("SAFEARRAY"),        //VT_SAFEARRAY       = 27,  /*    [T]     (use VT_ARRAY in VARIANT)   */
  549. _T("CARRAY"),           //VT_CARRAY          = 28,  /*    [T]     C style array               */
  550. _T("USERDEFINED"),      //VT_USERDEFINED     = 29,  /*    [T]     user defined type         */
  551. _T("LPSTR"),            //VT_LPSTR           = 30,  /*    [T][P]  null terminated string      */
  552. _T("LPWSTR"),           //VT_LPWSTR          = 31,  /*    [T][P]  wide null terminated string */
  553. _T(""),                 //                   = 32,
  554. _T(""),                 //                   = 33,
  555. _T(""),                 //                   = 34,
  556. _T(""),                 //                   = 35,
  557. _T(""),                 //                   = 36,
  558. _T(""),                 //                   = 37,
  559. _T(""),                 //                   = 38,
  560. _T(""),                 //                   = 39,
  561. _T(""),                 //                   = 40,
  562. _T(""),                 //                   = 41,
  563. _T(""),                 //                   = 42,
  564. _T(""),                 //                   = 43,
  565. _T(""),                 //                   = 44,
  566. _T(""),                 //                   = 45,
  567. _T(""),                 //                   = 46,
  568. _T(""),                 //                   = 47,
  569. _T(""),                 //                   = 48,
  570. _T(""),                 //                   = 49,
  571. _T(""),                 //                   = 50,
  572. _T(""),                 //                   = 51,
  573. _T(""),                 //                   = 52,
  574. _T(""),                 //                   = 53,
  575. _T(""),                 //                   = 54,
  576. _T(""),                 //                   = 55,
  577. _T(""),                 //                   = 56,
  578. _T(""),                 //                   = 57,
  579. _T(""),                 //                   = 58,
  580. _T(""),                 //                   = 59,
  581. _T(""),                 //                   = 60,
  582. _T(""),                 //                   = 61,
  583. _T(""),                 //                   = 62,
  584. _T(""),                 //                   = 63,
  585. _T("FILETIME"),         //VT_FILETIME        = 64,  /*       [P]  FILETIME                    */
  586. _T("BLOB"),             //VT_BLOB            = 65,  /*       [P]  Length prefixed bytes       */
  587. _T("STREAM"),           //VT_STREAM          = 66,  /*       [P]  Name of the stream follows  */
  588. _T("STORAGE"),          //VT_STORAGE         = 67,  /*       [P]  Name of the storage follows */
  589. _T("STREAMED_OBJECT"),  //VT_STREAMED_OBJECT = 68,  /*       [P]  Stream contains an object   */
  590. _T("STORED_OBJECT"),    //VT_STORED_OBJECT   = 69,  /*       [P]  Storage contains an object  */
  591. _T("BLOB_OBJECT"),      //VT_BLOB_OBJECT     = 70,  /*       [P]  Blob contains an object     */
  592. _T("CF"),               //VT_CF              = 71,  /*       [P]  Clipboard format            */
  593. _T("CLSID"),            //VT_CLSID           = 72   /*       [P]  A Class ID                  */
  594. };
  595. CString VTtoString( VARTYPE vt )
  596. {
  597. CString str ;
  598. vt &= ~0xF000 ;
  599. if (vt <= VT_CLSID)
  600.    str = g_rgszVT[vt]  ;
  601. else
  602.    str = "BAD VARTYPE" ;
  603. return str ;
  604. }
  605. CString TYPEDESCtoString( ITypeInfo* pti, TYPEDESC* ptdesc )
  606. {
  607. CString str ;
  608. if (ptdesc->vt == VT_PTR)
  609. {
  610. // ptdesc->lptdesc points to a TYPEDESC that specifies the thing pointed to
  611. str = TYPEDESCtoString( pti, ptdesc->lptdesc ) ;
  612. str += "*" ;
  613. return str.AllocSysString() ;
  614. }
  615. if ((ptdesc->vt & 0x0FFF) == VT_CARRAY)
  616. {
  617. // ptdesc->lpadesc points to an ARRAYDESC
  618. str = TYPEDESCtoString( pti, &ptdesc->lpadesc->tdescElem ) ;
  619. // Allocate cDims * lstrlen("[123456]")
  620. CString strTemp ;
  621. for (USHORT n = 0 ; n < ptdesc->lpadesc->cDims ; n++)
  622. {
  623. strTemp.Format( _T("[%d]"), ptdesc->lpadesc->rgbounds[n].cElements ) ;
  624. str += strTemp ;
  625. }
  626. return str ;
  627. }
  628. if ((ptdesc->vt & 0x0FFF) == VT_SAFEARRAY)
  629. {
  630. str = "SAFEARRAY(" + TYPEDESCtoString( pti, ptdesc->lptdesc ) + ")" ;
  631. return str ;
  632. }
  633. if (ptdesc->vt == VT_USERDEFINED)
  634. {
  635. // Use ptdesc->hreftype and pti->GetRefTypeInfo
  636. //
  637. ASSERT(pti) ;
  638. ITypeInfo* ptiRefType = NULL ;
  639. HRESULT hr = pti->GetRefTypeInfo( ptdesc->hreftype, &ptiRefType ) ;
  640. if (SUCCEEDED(hr))
  641. {
  642. BSTR            bstrName = NULL ;
  643. BSTR            bstrDoc = NULL ;
  644. BSTR            bstrHelp = NULL ;
  645. DWORD           dwHelpID ;
  646. hr = ptiRefType->GetDocumentation( MEMBERID_NIL, &bstrName, &bstrDoc, &dwHelpID, &bstrHelp ) ;
  647. if (FAILED(hr))
  648. {
  649. #ifdef _DEBUG
  650. ErrorMessage(_T("ITypeInfo::GetDocumentation() failed"), hr ) ;
  651. #endif
  652. return _T("ITypeInfo::GetDocumentation() failed in TYPEDESCToString") ;
  653. }
  654. str = bstrName ;
  655. SysFreeString(bstrName) ;
  656. SysFreeString( bstrDoc ) ;
  657. SysFreeString( bstrHelp ) ;
  658. ptiRefType->Release() ;
  659. }
  660. else
  661. {
  662. #ifdef _DEBUG
  663. ErrorMessage( _T("GetRefTypeInfo failed in TYPEDESCToString"), hr ) ;
  664. #endif
  665. return _T("<GetRefTypeInfo failed>") ;
  666. }
  667. return str ;
  668. }
  669. return VTtoString( ptdesc->vt );
  670. }