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.

500 lines
11 KiB

  1. ;; Copyright Microsoft Corporation
  2. ;;
  3. ;; Module Name:
  4. ;;
  5. ;; winthrow.tpl
  6. ;;
  7. ;; Abstract:
  8. ;;
  9. ;; Author:
  10. ;;
  11. ;; July 2001 JayKrell
  12. ;;
  13. ;; Revision History:
  14. ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16. [Code]
  17. TemplateName=winthrow_cpp
  18. Begin=
  19. @NoFormat(
  20. //
  21. // Microsoft Visual C++ 7.0 or higher required.
  22. //
  23. #if (defined(_MSC_VER) && _MSC_VER >= 1300 && defined(__cplusplus))
  24. @If(@MicrosoftInternal)(
  25. #include "winthrow_private.h"
  26. )@Else(
  27. #include "winthrow.h"
  28. )
  29. template <typename T>
  30. bool
  31. @PrivateNamespace_IsErrorAcceptable(
  32. T Error,
  33. SIZE_T NumberOfAcceptableErrors,
  34. va_list VaListOfAcceptableErrors
  35. )
  36. {
  37. while (NumberOfAcceptableErrors-- != 0)
  38. {
  39. const T AcceptableError = va_arg(VaListOfAcceptableErrors, DWORD);
  40. if (Error == AcceptableError)
  41. {
  42. /* not consuming the whole va_list is not portable, but very common. */
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. static bool
  49. @PrivateNamespace_IsWin32ErrorAcceptable(
  50. DWORD Win32Error,
  51. SIZE_T NumberOfAcceptableErrors,
  52. va_list VaListOfAcceptableErrors
  53. )
  54. {
  55. return ::@PrivateNamespace_IsErrorAcceptable<DWORD>(Win32Error, NumberOfAcceptableErrors, VaListOfAcceptableErrors);
  56. }
  57. static bool
  58. @PrivateNamespace_IsHResultAcceptable(
  59. HRESULT HResult,
  60. SIZE_T NumberOfAcceptableErrors,
  61. va_list VaListOfAcceptableErrors
  62. )
  63. {
  64. return ::@PrivateNamespace_IsErrorAcceptable<HRESULT>(HResult, NumberOfAcceptableErrors, VaListOfAcceptableErrors);
  65. }
  66. @If(@MicrosoftInternal)(
  67. static bool
  68. @PrivateNamespace_IsNtStatusAcceptable(
  69. NTSTATUS NtStatus,
  70. SIZE_T NumberOfAcceptableErrors,
  71. va_list VaListOfAcceptableErrors
  72. )
  73. {
  74. return ::@PrivateNamespace_IsErrorAcceptable<NTSTATUS>(NtStatus, NumberOfAcceptableErrors, VaListOfAcceptableErrors);
  75. }
  76. )
  77. )
  78. @Template(ImplementWinThrowIFunc)
  79. @NL
  80. @NL#endif // (defined(_MSC_VER) && _MSC_VER >= 1300 && defined(__cplusplus))
  81. @NL
  82. End=
  83. [Code]
  84. TemplateName=winthrow_h
  85. Begin=
  86. @NoFormat(
  87. //
  88. // Microsoft Visual C++ 7.0 or higher required.
  89. //
  90. #if (defined(_MSC_VER) && _MSC_VER >= 1300 && defined(__cplusplus))
  91. #ifndef @HEADERjGUARD
  92. #define @HEADERjGUARD
  93. #pragma once
  94. #include <stddef.h>
  95. #include <stdarg.h>
  96. @If(@MicrosoftInternal)(
  97. #include "nt.h"
  98. #include "ntrtl.h"
  99. #include "nturtl.h"
  100. )
  101. #include "windows.h"
  102. #include "ole2.h"
  103. #include "commctrl.h"
  104. #include "imagehlp.h"
  105. #include "setupapi.h"
  106. #include "wincrypt.h"
  107. @If(@MicrosoftInternal)(
  108. #if !defined(NT_INCLUDED)
  109. typedef long NTSTATUS;
  110. #endif
  111. )
  112. #if !defined(EXCEPTION_TYPE)
  113. #define EXCEPTION_TYPE EXCEPTION_TYPE
  114. class EXCEPTION_TYPE
  115. {
  116. typedef enum EType { etSuccess, etWin32Error, etHResult@If(@MicrosoftInternal)(, etNtStatus) } EType;
  117. EType m_Type;
  118. union
  119. {
  120. DWORD m_Win32Error;
  121. HRESULT m_HResult;
  122. @If(@MicrosoftInternal)(NTSTATUS m_NtStatus;)
  123. };
  124. //
  125. // strawman, will be generated by genthunk
  126. //
  127. typedef enum ERichType { ertNone, ertCreateFile, ertReadFile } ERichType;
  128. ERichType m_RichType;
  129. union
  130. {
  131. struct
  132. {
  133. WCHAR FileName[MAX_PATH];
  134. } CreateFile;
  135. struct
  136. {
  137. HANDLE Handle;
  138. } ReadFile;
  139. };
  140. public:
  141. EXCEPTION_TYPE() : m_Type(etSuccess), m_RichType(ertNone) { }
  142. ~EXCEPTION_TYPE() { }
  143. DWORD GetWin32Error() const
  144. {
  145. //
  146. // GetLastError sometimes returns HRESULTs, so we do too.
  147. //
  148. switch (m_Type)
  149. {
  150. case etSuccess:
  151. return NO_ERROR;
  152. case etWin32Error:
  153. return m_Win32Error;
  154. case etHResult:
  155. if (HRESULT_FACILITY(m_HResult) == FACILITY_WIN32)
  156. return HRESULT_CODE(m_HResult);
  157. return m_HResult;
  158. @If(@MicrosoftInternal)(
  159. case etNtStatus:
  160. return m_NtStatus;
  161. )
  162. }
  163. }
  164. DWORD GetHResult() const
  165. {
  166. switch (m_Type)
  167. {
  168. case etSuccess:
  169. return NOERROR;
  170. case etWin32Error:
  171. return HRESULT_FROM_WIN32(m_Win32Error);
  172. case etHResult:
  173. return m_HResult;
  174. @If(@MicrosoftInternal)(
  175. case etNtStatus:
  176. return HRESULT_FROM_NT(m_NtStatus);
  177. )
  178. }
  179. }
  180. @If(@MicrosoftInternal)(
  181. NTSTATUS GetNtStatus() const
  182. {
  183. switch (m_Type)
  184. {
  185. case etSuccess:
  186. return STATUS_SUCCESS;
  187. case etWin32Error:
  188. return HRESULT_FROM_WIN32(m_Win32Error);
  189. case etHResult:
  190. if (m_HResult & FACILITY_NT_BIT)
  191. return (m_HResult & ~FACILITY_NT_BIT);
  192. return m_HResult;
  193. @If(@MicrosoftInternal)(
  194. case etNtStatus:
  195. return m_NtStatus;
  196. )
  197. }
  198. })
  199. //
  200. // strawman, will be generated by genthunk
  201. //
  202. void InitCreateFileW(
  203. IN DWORD Win32Error,
  204. IN LPCWSTR lpFileName,
  205. IN DWORD dwDesiredAccess,
  206. IN DWORD dwShareMode,
  207. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  208. IN DWORD dwCreationDisposition,
  209. IN DWORD dwFlagsAndAttributes,
  210. IN HANDLE hTemplateFile
  211. )
  212. {
  213. this->InitWin32Error(Win32Error);
  214. SIZE_T Length = ::lstrlenW(lpFileName);
  215. if (Length >= MAX_PATH)
  216. Length = MAX_PATH - 1;
  217. CopyMemory(this->CreateFile.FileName, lpFileName, Length * sizeof(WCHAR));
  218. this->CreateFile.FileName[Length] = 0;
  219. this->m_RichType = ertCreateFile;
  220. }
  221. //
  222. // strawman, will be generated by genthunk
  223. //
  224. void InitReadFile(
  225. IN DWORD Win32Error,
  226. IN HANDLE hFile,
  227. OUT LPVOID lpBuffer,
  228. IN DWORD nNumberOfBytesToRead,
  229. OUT LPDWORD lpNumberOfBytesRead,
  230. IN LPOVERLAPPED lpOverlapped
  231. )
  232. {
  233. this->InitWin32Error(Win32Error);
  234. this->ReadFile.Handle = hFile;
  235. this->m_RichType = ertReadFile;
  236. }
  237. void InitWin32Error(DWORD Win32Error)
  238. {
  239. this->m_Type = etWin32Error;
  240. this->m_Win32Error = Win32Error;
  241. }
  242. void InitHResult(HRESULT HResult)
  243. {
  244. this->m_Type = etHResult;
  245. this->m_HResult = HResult;
  246. }
  247. @If(@MicrosoftInternal)(
  248. void InitNtStatus(NTSTATUS NtStatus)
  249. {
  250. this->m_Type = etNtStatus;
  251. this->m_NtStatus = NtStatus;
  252. })
  253. void Throw() const
  254. {
  255. throw *this;
  256. }
  257. };
  258. #endif // EXCEPTION_TYPE
  259. )
  260. @Template(DeclareWinThrowIFunc)
  261. @NL#endif // @HEADERjGUARD
  262. @NL#endif // (defined(_MSC_VER) && _MSC_VER >= 1300 && defined(__cplusplus))
  263. @NL
  264. End=
  265. [Macros]
  266. TemplateName=ImplementWinThrowMacro
  267. Begin=
  268. @DeclareUnlessVa()@NL
  269. @ImplementUnlessVa@NL@NL
  270. @DeclareSameSignature()@NL
  271. @ImplementSameSignature@NL@NL
  272. @DeclareUnlessDots()@NL
  273. @ImplementUnlessDots@NL@NL
  274. End=
  275. [IFunc]
  276. TemplateName=ImplementWinThrowIFunc
  277. Begin=
  278. @If(@And(@Not(@DoesApiReturnVoid), @IsApiDeclared))(
  279. @ImplementWinThrowMacro
  280. )@Else(
  281. /* @ApiName skipped */@NL@NL
  282. )
  283. End=
  284. [Macros]
  285. TemplateName=DeclareWinThrowMacro
  286. Begin=
  287. @DeclareUnlessVa(;)@NL@NL
  288. @DeclareSameSignature(;)@NL@NL
  289. @DeclareUnlessDots(;)@NL@NL
  290. End=
  291. [IFunc]
  292. TemplateName=DeclareWinThrowIFunc
  293. Begin=
  294. @If(@And(@Not(@DoesApiReturnVoid), @IsApiDeclared))(
  295. @DeclareWinThrowMacro
  296. )@Else(
  297. /* @ApiName skipped */@NL@NL
  298. )
  299. End=
  300. [Macros]
  301. TemplateName=ArgsOut
  302. Begin=
  303. @ArgList(@ArgName@ArgMore(,))
  304. End=
  305. TemplateName=ArgsOutNL
  306. Begin=
  307. @ArgList(@ArgName@ArgMore(,@NL))
  308. End=
  309. TemplateName=ArgsIn
  310. Begin=
  311. @ArgList(@ArgMod @ArgType @ArgName@ArgMore(,))
  312. End=
  313. TemplateName=ArgsInNL
  314. Begin=
  315. @ArgList(@ArgMod @ArgType @ArgName@ArgMore(,@NL))
  316. End=
  317. TemplateName=DeclareSameSignature
  318. NumArgs=1
  319. Begin=
  320. @NewReturnTypeMacro @ApiFnMod @SameSignatureName(@Indent(@NL
  321. @ArgsInNL))@MArg(1)
  322. End=
  323. [Macros]
  324. TemplateName=ImplementSameSignature
  325. Begin=
  326. {@Indent(@NL
  327. @DeclareLocalErrorOut
  328. @IFNewReturnTypeNotVoidMacro(return) ::@UnlessVaName(@Indent(@NL@IfArgs(@ArgsOutNL,@NL)
  329. @PassErrorOut
  330. 0,@NL
  331. 0));@NL
  332. )}@NL
  333. End=
  334. TemplateName=DeclareUnlessVa
  335. NumArgs=1
  336. Begin=
  337. @NewReturnTypeMacro @ApiFnMod @UnlessVaName(@Indent(@NL
  338. @IfArgs(@ArgsInNL,@NL)
  339. @DeclareErrorOut
  340. SIZE_T NumberOfAcceptableErrors,@NL
  341. va_list VaListOfAcceptableErrors))@MArg(1)
  342. End=
  343. TemplateName=ImplementUnlessVa
  344. Begin=
  345. {@Indent(@NL
  346. @ClearErrorOut
  347. @ApiFnRet const RetVal = ::@ApiName(@IfArgs(@NL@Indent(@ArgsOutNL)));@NL
  348. @NL
  349. if (@Failed)@NL
  350. {@Indent(@NL
  351. @SetErrorOut
  352. if (!@IsErrorAcceptable)@NL
  353. {@Indent(@NL
  354. @ThrowError
  355. )}@NL
  356. )}@NL
  357. @IFNewReturnTypeNotVoidMacro(return RetVal;@NL)
  358. )}@NL
  359. End=
  360. TemplateName=DeclareUnlessDots
  361. NumArgs=1
  362. Begin=
  363. @NewReturnTypeMacro __cdecl @UnlessDotsName(@Indent(@NL@IfArgs(@ArgsInNL,@NL)
  364. @DeclareErrorOut
  365. SIZE_T NumberOfAcceptableErrors,@NL
  366. ...))@MArg(1)
  367. End=
  368. [Macros]
  369. TemplateName=ImplementUnlessDots
  370. Begin=
  371. {@Indent(@NL
  372. va_list VaListOfAcceptableErrors;@NL
  373. @NL
  374. @ClearErrorOut
  375. va_start(VaListOfAcceptableErrors, NumberOfAcceptableErrors);@NL
  376. @NL
  377. @IFNewReturnTypeNotVoidMacro(@ApiFnRet const RetVal =)::@UnlessVaName(@Indent(@NL@IfArgs(@ArgsOutNL,@NL)
  378. @PassErrorOut
  379. NumberOfAcceptableErrors,@NL
  380. VaListOfAcceptableErrors));@NL
  381. @NL
  382. va_end(VaListOfAcceptableErrors);@NL
  383. @NL
  384. @IFNewReturnTypeNotVoidMacro(return RetVal;@NL)
  385. )}@NL
  386. End=
  387. [Macros]
  388. TemplateName=DeclareErrorOut
  389. Begin=
  390. @IfApiCode(DeclareErrorOut)@Else(@RetType(DeclareErrorOut))
  391. End=
  392. TemplateName=ClearErrorOut
  393. Begin=
  394. @IfApiCode(ClearErrorOut)@Else(@RetType(ClearErrorOut))
  395. End=
  396. TemplateName=ThrowError
  397. Begin=
  398. @IfApiCode(ThrowError)@Else(@RetType(ThrowError))
  399. End=
  400. TemplateName=IsErrorAcceptable
  401. Begin=
  402. @IfApiCode(IsErrorAcceptable)@Else(@RetType(IsErrorAcceptable))
  403. End=
  404. TemplateName=SetErrorOut
  405. Begin=
  406. @IfApiCode(SetErrorOut)@Else(@RetType(SetErrorOut))
  407. End=
  408. TemplateName=Failed
  409. Begin=
  410. @IfApiCode(Failed)@Else(@RetType(Failed))
  411. End=
  412. ;;
  413. ;; This is a temporary for when SameSignature calls Unless(0);
  414. ;;
  415. [Macros]
  416. TemplateName=DeclareLocalErrorOut
  417. Begin=
  418. @IfApiCode(DeclareLocalErrorOut)@Else(@RetType(DeclareLocalErrorOut))
  419. End=
  420. [Macros]
  421. TemplateName=PassErrorOut
  422. Begin=
  423. @IfApiCode(PassErrorOut)@Else(@RetType(PassErrorOut))
  424. End=
  425. ;;
  426. ;; This code would change BOOL to void.
  427. ;;
  428. ;;TemplateName=NewReturnTypeMacro
  429. ;;Begin=
  430. ;;@IfRetType(BOOL,void)@Else(@ApiFnRet)
  431. ;;End=
  432. ;;
  433. ;;TemplateName=IFNewReturnTypeNotVoidMacro
  434. ;;NumArgs=1
  435. ;;Begin=
  436. ;;@IfRetType(BOOL,)@Else(@MArg(1))
  437. ;;End=
  438. ;;
  439. ;; This code just passed BOOL through unmodified.
  440. ;;
  441. TemplateName=NewReturnTypeMacro
  442. Begin=
  443. @ApiFnRet
  444. End=
  445. TemplateName=IFNewReturnTypeNotVoidMacro
  446. NumArgs=1
  447. Begin=
  448. @MArg(1)
  449. End=