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.

537 lines
21 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft OLE
  3. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  4. //
  5. // File: testhelp.hxx
  6. //
  7. // Contents: Declaration & macros for testhelp library functions.
  8. //
  9. // History: 28-Nov-94 DeanE Created
  10. //--------------------------------------------------------------------------
  11. #ifndef __TESTHELP_HXX__
  12. #define __TESTHELP_HXX__
  13. // Include headers for stuff in the testhelp library. Need to include
  14. // this header after <windows.h>, <ole2.h>, and <stdio.h>
  15. //
  16. #include <ctmem.hxx> // For memory functions
  17. #include <log.hxx> // For test logs
  18. #include <creghelp.hxx> // Registry helper class
  19. #include <cdbghelp.hxx> // Debug helper class
  20. #include <stddef.h> //for 'offsetof' macro
  21. // Used to make testhelp (TH) error codes
  22. #define MAKE_TH_ERROR_CODE(code) \
  23. MAKE_SCODE(SEVERITY_ERROR, FACILITY_NULL, code)
  24. // Function declarations
  25. HRESULT CmdlineToArgs(LPSTR paszCmdline, PINT pargc, CHAR ***pargv);
  26. // extern Global variable, whose THREAD_VALIDATE_FLAG_ON bit is used at present
  27. // to do or skip thread validation DH_VDATETHREAD macro. Other bits may be
  28. // used in future.
  29. extern ULONG g_fThreadValidate ;
  30. // DEBUG object declaration as extern
  31. DH_DECLARE;
  32. //+----------------------------------------------------------------------------
  33. //
  34. // Macro:
  35. // GETPPARENT
  36. //
  37. // Synopsis:
  38. // Given a pointer to something contained by a struct (or
  39. // class,) the type name of the containing struct (or class),
  40. // and the name of the member being pointed to, return a pointer
  41. // to the container.
  42. //
  43. // Arguments:
  44. // [pmemb] -- pointer to member of struct (or class.)
  45. // [struc] -- type name of containing struct (or class.)
  46. // [membname] - name of member within the struct (or class.)
  47. //
  48. // Returns:
  49. // pointer to containing struct (or class)
  50. //
  51. // Notes:
  52. // Assumes all pointers are FAR.
  53. //
  54. // History:
  55. // 11/10/93 - ChrisWe - created
  56. // 30-Dec-94 - AlexE - Swiped from OLE project
  57. //
  58. //-----------------------------------------------------------------------------
  59. #define GETPPARENT(pmemb, struc, membname) (\
  60. (struc FAR *)(((char FAR *)(pmemb))-offsetof(struc, membname)))
  61. //+----------------------------------------------------------------------------
  62. //
  63. // Macro:
  64. // DH_VDATEPTRIN (ptr, type)
  65. // DH_VDATEPTRIN0(ptr, type)
  66. //
  67. // Synopsis:
  68. // Validates a pointer for reading and asserts if it is not.
  69. // Second version validates NULL pointers as well.
  70. //
  71. // Arguments:
  72. // [ptr] - The pointer to test
  73. //
  74. // [type] - the type of the pointer.
  75. //
  76. //
  77. // Returns:
  78. // E_INVALIDARG if there is a problem, continues executing
  79. // if not.
  80. //
  81. // Notes:
  82. //
  83. // History:
  84. // 26-Jan-95 AlexE - Created
  85. //
  86. //-----------------------------------------------------------------------------
  87. #define DH_VDATEPTRIN( ptr, type ) \
  88. { \
  89. if ( (NULL == ptr) || (FALSE != IsBadReadPtr(ptr, sizeof(type))) ) \
  90. { \
  91. DH_TRACE( (DH_LVL_ALWAYS, \
  92. TEXT("IN pointer is invalid: %8.8lx"), ptr) ) ; \
  93. \
  94. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  95. \
  96. return E_INVALIDARG ; \
  97. } \
  98. }
  99. #define DH_VDATEPTRIN0( ptr, type ) \
  100. { \
  101. if(NULL != ptr) \
  102. DH_VDATEPTRIN( ptr, type ); \
  103. }
  104. //+----------------------------------------------------------------------------
  105. //
  106. // Macro:
  107. // DH_VDATEPTROUT (ptr, type)
  108. // DH_VDATEPTROUT0(ptr, type)
  109. //
  110. // Synopsis:
  111. // Validates a pointer for writing and asserts if it is not.
  112. // Second version validates NULL pointers as well.
  113. //
  114. // Arguments:
  115. // [ptr] - The pointer to test
  116. //
  117. // [type] - the type of the pointer.
  118. //
  119. //
  120. // Returns:
  121. // E_INVALIDARG if there is a problem, continues executing
  122. // if not.
  123. //
  124. // Notes:
  125. //
  126. // History:
  127. // 26-Jan-95 AlexE - Created
  128. //
  129. //-----------------------------------------------------------------------------
  130. #define DH_VDATEPTROUT( ptr, type ) \
  131. { \
  132. if ( (NULL == ptr) || (FALSE != IsBadWritePtr(ptr, sizeof(type))) ) \
  133. { \
  134. DH_TRACE( (DH_LVL_ALWAYS, \
  135. TEXT("OUT pointer is invalid: %8.8lx"), ptr) ) ; \
  136. \
  137. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  138. \
  139. return E_INVALIDARG ; \
  140. } \
  141. }
  142. #define DH_VDATEPTROUT0( ptr, type ) \
  143. { \
  144. if(NULL != ptr) \
  145. DH_VDATEPTROUT( ptr, type ); \
  146. }
  147. //+----------------------------------------------------------------------------
  148. //
  149. // Macro:
  150. // DH_VDATECODEPTR (ptr)
  151. // DH_VDATECODEPTR0(ptr)
  152. //
  153. // Synopsis:
  154. // Validates a pointer for writing and asserts if it is not.
  155. // Second version validates NULL pointers as well.
  156. //
  157. // Arguments:
  158. // [ptr] - The pointer to the code
  159. //
  160. // Returns:
  161. // E_INVALIDARG if there is a problem, continues executing
  162. // if not.
  163. //
  164. // Notes:
  165. //
  166. // History:
  167. // 26-Jan-95 AlexE - Created
  168. //
  169. //-----------------------------------------------------------------------------
  170. #define DH_VDATECODEPTR( ptr ) \
  171. { \
  172. if ( (NULL == ptr) || (FALSE != IsBadCodePtr( (FARPROC) ptr))) \
  173. { \
  174. DH_TRACE( (DH_LVL_ALWAYS, \
  175. TEXT("CODE pointer is invalid: %8.8lx"), ptr) ) ; \
  176. \
  177. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  178. \
  179. return E_INVALIDARG ; \
  180. } \
  181. }
  182. #define DH_VDATECODEPTR0( ptr ) \
  183. { \
  184. if(NULL != ptr) \
  185. DH_VDATECODEPTR( ptr ) \
  186. }
  187. //+----------------------------------------------------------------------------
  188. //
  189. // Macro:
  190. // DH_VDATESTRINGPTR (ptr)
  191. // DH_VDATESTRINGPTR0(ptr)
  192. //
  193. // Synopsis:
  194. // Validates a string pointer for reading asserts if it is not.
  195. // Second version validates NULL pointers as well.
  196. //
  197. // Arguments:
  198. // [ptr] - The string pointer
  199. //
  200. // Returns:
  201. // E_INVALIDARG if there is a problem, continues executing
  202. // if not.
  203. //
  204. // Notes:
  205. //
  206. // History:
  207. // 11-Sep-95 Kennethm - Created
  208. //
  209. //-----------------------------------------------------------------------------
  210. #define DH_VDATESTRINGPTR( ptr ) \
  211. { \
  212. if ( (NULL == ptr) || (FALSE != IsBadStringPtr( ptr, (WORD)-1))) \
  213. { \
  214. DH_TRACE( (DH_LVL_ALWAYS, \
  215. TEXT("String pointer is invalid: %8.8lx"), ptr) ) ; \
  216. \
  217. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  218. \
  219. return E_INVALIDARG ; \
  220. } \
  221. }
  222. #define DH_VDATESTRINGPTR0( ptr ) \
  223. { \
  224. if(NULL != ptr) \
  225. DH_VDATESTRINGPTR( ptr); \
  226. }
  227. #define DH_VDATESTRINGPTRW( ptr ) \
  228. { \
  229. if ( (NULL == ptr) || (FALSE != IsBadStringPtrW( ptr, (WORD)-1))) \
  230. { \
  231. DH_TRACE( (DH_LVL_ALWAYS, \
  232. TEXT("String pointer is invalid: %8.8lx"), ptr) ); \
  233. \
  234. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")); \
  235. \
  236. return E_INVALIDARG; \
  237. } \
  238. }
  239. #define DH_VDATESTRINGPTRW0( ptr ) \
  240. { \
  241. if(NULL != ptr) \
  242. DH_VDATESTRINGPTRW( ptr); \
  243. }
  244. //+----------------------------------------------------------------------------
  245. //
  246. // Macro:
  247. // DH_VDATEWRITEBUFFER (ptr,size)
  248. // DH_VDATEWRITEBUFFER0(ptr,size)
  249. //
  250. // Synopsis:
  251. // Validates a buffer for writing asserts if it is not valid.
  252. // Second version validates NULL pointers as well.
  253. //
  254. // Arguments:
  255. // [ptr] - The buffer pointer
  256. // [size] - The buffer size
  257. //
  258. // Returns:
  259. // E_INVALIDARG if there is a problem, continues executing
  260. // if not.
  261. //
  262. // Notes:
  263. //
  264. // History:
  265. // 5-Feb-98 BogdanT - Created
  266. //
  267. //-----------------------------------------------------------------------------
  268. #define DH_VDATEWRITEBUFFER( ptr, size ) \
  269. { \
  270. if ( (NULL == ptr) || (FALSE != IsBadWritePtr(ptr, size) ) ) \
  271. { \
  272. DH_TRACE( (DH_LVL_ALWAYS, \
  273. TEXT("Write buffer is invalid: %8.8lx"), ptr) ) ; \
  274. \
  275. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  276. \
  277. return E_INVALIDARG ; \
  278. } \
  279. }
  280. #define DH_VDATEWRITEBUFFER0( ptr, size ) \
  281. { \
  282. if(NULL != ptr) \
  283. DH_VDATEWRITEBUFFER( ptr, size); \
  284. }
  285. //+----------------------------------------------------------------------------
  286. //
  287. // Macro:
  288. // DH_VDATEREADBUFFER (ptr,size)
  289. // DH_VDATEREADBUFFER0(ptr,size)
  290. //
  291. // Synopsis:
  292. // Validates a buffer for reading asserts if it is not valid.
  293. // Second version validates NULL pointers as well.
  294. //
  295. // Arguments:
  296. // [ptr] - The buffer pointer
  297. // [size] - The buffer size
  298. //
  299. // Returns:
  300. // E_INVALIDARG if there is a problem, continues executing
  301. // if not.
  302. //
  303. // Notes:
  304. //
  305. // History:
  306. // 5-Feb-98 BogdanT - Created
  307. //
  308. //-----------------------------------------------------------------------------
  309. #define DH_VDATEREADBUFFER( ptr, size ) \
  310. { \
  311. if ( (NULL == ptr) || (FALSE != IsBadReadPtr(ptr, size) ) ) \
  312. { \
  313. DH_TRACE( (DH_LVL_ALWAYS, \
  314. TEXT("Read buffer is invalid: %8.8lx"), ptr) ) ; \
  315. \
  316. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  317. \
  318. return E_INVALIDARG ; \
  319. } \
  320. }
  321. #define DH_VDATEREADBUFFER0( ptr, size ) \
  322. { \
  323. if(NULL != ptr) \
  324. DH_VDATEREADBUFFER( ptr, size); \
  325. }
  326. //--------------------------------------------------------------------
  327. // Macros for aborting normal flow on error
  328. // Use to print error message and jump to cleanup code
  329. // at the end of the current function, if error occured.
  330. //
  331. // Sample Usage:
  332. // HRESULT MyFunction(...)
  333. // {
  334. // HRESULT hr=S_OK;
  335. // .....
  336. // hr=Func1(...)
  337. // DH_HRCHECK_ABORT(hr,TEXT("Func1"));
  338. // .....
  339. // pBuff = new BYTE[100];
  340. // DH_ABORTIF(NULL==pBuff,E_OUTOFMEMORY,TEXT("pBuff==NULL"));
  341. // ....
  342. // ErrReturn:
  343. // return hr;
  344. // };
  345. //
  346. // Note:
  347. // -- on error DH_HRCHECK_ABORT and DH_ABORTIF jump to ErrReturn
  348. // -- you must have hr and ErrReturn label defined locally
  349. // -- never use DH_HRCHECK_ABORT or DH_ABORTIF in cleanup code
  350. // -- group cleanup code at the end of each function
  351. // -- when reading the normal flow just ignore these macros
  352. //
  353. //--------------------------------------------------------------------
  354. #define DH_HRCHECK_ABORT(hresult,message) \
  355. { \
  356. DH_HRCHECK(hresult,message); \
  357. if (S_OK != hresult) \
  358. { \
  359. hr=hresult; \
  360. goto ErrReturn; \
  361. } \
  362. }
  363. #define DH_ABORTIF(condition,err_code,msg) \
  364. { \
  365. if ((condition)) \
  366. { \
  367. hr=err_code; \
  368. DH_HRCHECK(hr, msg); \
  369. goto ErrReturn; \
  370. } \
  371. }
  372. //+----------------------------------------------------------------------------
  373. //
  374. // Macro: DH_THREAD_VALIDATION_OFF
  375. //
  376. // Synopsis: Turns the thread validation macro DH_VDATETHREAD off by
  377. // clearing the THREAD_VALIDATE_FLAG_ON bit of the global
  378. // variable g_fThreadValidate.
  379. //
  380. // Arguments: None
  381. //
  382. // Returns: None
  383. //
  384. // History: 30-Jan-1996 NarindK - Created
  385. //
  386. //-----------------------------------------------------------------------------
  387. #define DH_THREAD_VALIDATION_OFF \
  388. { \
  389. g_fThreadValidate &= ~THREAD_VALIDATE_FLAG_ON ; \
  390. }
  391. //+----------------------------------------------------------------------------
  392. //
  393. // Macro: DH_THREAD_VALIDATION_ON
  394. //
  395. // Synopsis: Turns the thread validation macro DH_VDATETHREAD on by
  396. // setting the THREAD_VALIDATE_FLAG_ON bit of the global
  397. // variable g_fThreadValidate.
  398. //
  399. // Arguments: None
  400. //
  401. // Returns: None
  402. //
  403. // History: 30-Jan-1996 NarindK - Created
  404. //
  405. //-----------------------------------------------------------------------------
  406. #define DH_THREAD_VALIDATION_ON \
  407. { \
  408. g_fThreadValidate |= THREAD_VALIDATE_FLAG_ON ; \
  409. }
  410. //-----------------------------------------------------------------------------
  411. //
  412. // Macro: DH_IS_THREAD_VALIDATION_ON
  413. //
  414. // Synopsis: Checks THREAD_VALIDATE_FLAG_ON bit of global variable used
  415. // to turn on/off DH_VDATETHREAD macro. If the bit is set,
  416. // then the macro computes to 1, else 0.
  417. //
  418. // Arguments: None
  419. //
  420. // Returns: None
  421. //
  422. // History: 30-Jan-1996 NarindK - Created
  423. //
  424. //-----------------------------------------------------------------------------
  425. #define DH_IS_THREAD_VALIDATION_ON \
  426. (g_fThreadValidate & THREAD_VALIDATE_FLAG_ON)
  427. //+----------------------------------------------------------------------------
  428. //
  429. // Macro:
  430. // DH_VDATETHREAD
  431. //
  432. // Synopsis:
  433. // Validates verifies that the current thread ID is the same as
  434. // the one passed in as the 'tid" argument. This means that when
  435. // an object is created, it needs to store its thread ID away
  436. // somewhere.
  437. //
  438. //
  439. // Arguments: None.
  440. //
  441. // Returns: Nothing. It breaks into the debugger if it is determined
  442. // that the current code is executing on the wrong
  443. // thread.
  444. //
  445. // Notes: Your base class must derive from CThreadCheck if it uses this
  446. // macro. By default, g_fThreadValidate's THREAD_VALIDATE_FLAG_ON
  447. // bit is set.
  448. //
  449. // History:
  450. // 02-Feb-95 AlexE - Created
  451. // 28-Feb-95 AlexE - Converted to use CThreadCheck class
  452. // 26-Jan-96 NarindK - Added g_fThreadValidate global flag.
  453. //
  454. //-----------------------------------------------------------------------------
  455. #define DH_VDATETHREAD \
  456. { \
  457. if( DH_IS_THREAD_VALIDATION_ON && (FALSE == CheckThread()) ) \
  458. { \
  459. DH_TRACE((DH_LVL_ALWAYS, TEXT("Code Executing on wrong thread!"))) ; \
  460. \
  461. gdhDebugHelper.LabAssertEx(TEXT(__FILE__), __LINE__, TEXT("")) ; \
  462. \
  463. DebugBreak() ; \
  464. } \
  465. }
  466. //
  467. // Platform specifiers and accompanying command line function. These
  468. // exist because sometimes the tests need to have different behavior
  469. // for different OLE builds under the same OS.
  470. //
  471. #define OS_NT 1
  472. #define OS_WIN95 2
  473. #define OS_WIN95_DCOM 3
  474. #define OS_STRING_NT (OLESTR("NT"))
  475. #define OS_STRING_WIN95 (OLESTR("WIN95"))
  476. #define OS_STRING_WIN95DCOM (OLESTR("WIN95DCOM"))
  477. extern DWORD g_dwOperatingSystem;
  478. DWORD GetOSFromCmdline(CBaseCmdlineObj *pCmdLine);
  479. #endif // __TESTHELP_HXX__