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.

380 lines
25 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998
  5. //
  6. // File: DynLoad.hxx
  7. //
  8. // Contents: Macro for Dynamically loading/unloading entry points
  9. //
  10. // History: 25-Jun-98 KyleP Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #pragma once
  14. //
  15. // Sample use:
  16. //
  17. // DeclDynLoad( Kernel32,
  18. // GetLongPathName,
  19. // DWORD,
  20. // WINAPI,
  21. // (LPCWSTR lpszShortPath, LPWSTR lpszLongPath, DWORD cchBuffer) );
  22. //
  23. #define DeclDynLoad( DLL, Name, ReturnType, CallingConvention, Args, ArgsSansTypes ) \
  24. \
  25. typedef ReturnType (CallingConvention * PFN##Name)##Args; \
  26. \
  27. class CDynLoad##DLL \
  28. { \
  29. public: \
  30. CDynLoad##DLL() \
  31. { \
  32. _hmod = LoadLibraryA( #DLL ".dll" ); \
  33. \
  34. if ( 0 == _hmod ) \
  35. THROW( CException() ); \
  36. \
  37. _pfn = (PFN##Name)GetProcAddress( _hmod, #Name ); \
  38. \
  39. if ( 0 == _pfn ) \
  40. { \
  41. FreeLibrary( _hmod ); \
  42. THROW( CException() ); \
  43. } \
  44. } \
  45. \
  46. ~CDynLoad##DLL() \
  47. { \
  48. FreeLibrary( _hmod ); \
  49. } \
  50. \
  51. ReturnType Name##Args \
  52. { \
  53. return _pfn##ArgsSansTypes; \
  54. } \
  55. \
  56. private: \
  57. HMODULE _hmod; \
  58. PFN##Name _pfn; \
  59. }
  60. #define DeclDynLoad2( DLL, Name, ReturnType, CallingConvention, Args, ArgsSansTypes, \
  61. Name2, ReturnType2, CallingConvention2, Args2, ArgsSansTypes2 ) \
  62. \
  63. typedef ReturnType (CallingConvention * PFN##Name)##Args; \
  64. typedef ReturnType2 (CallingConvention2 * PFN##Name2)##Args2; \
  65. \
  66. class CDynLoad##DLL \
  67. { \
  68. public: \
  69. CDynLoad##DLL() \
  70. { \
  71. _hmod = LoadLibraryA( #DLL ".dll" ); \
  72. \
  73. if ( 0 == _hmod ) \
  74. THROW( CException() ); \
  75. \
  76. _pfn = (PFN##Name)GetProcAddress( _hmod, #Name ); \
  77. \
  78. if ( 0 == _pfn ) \
  79. { \
  80. FreeLibrary( _hmod ); \
  81. THROW( CException() ); \
  82. } \
  83. \
  84. _pfn2 = (PFN##Name2)GetProcAddress( _hmod, #Name2 ); \
  85. \
  86. if ( 0 == _pfn2 ) \
  87. { \
  88. FreeLibrary( _hmod ); \
  89. THROW( CException() ); \
  90. } \
  91. } \
  92. \
  93. ~CDynLoad##DLL() \
  94. { \
  95. FreeLibrary( _hmod ); \
  96. } \
  97. \
  98. ReturnType Name##Args \
  99. { \
  100. return _pfn##ArgsSansTypes; \
  101. } \
  102. \
  103. ReturnType2 Name2##Args2 \
  104. { \
  105. return _pfn2##ArgsSansTypes2; \
  106. } \
  107. \
  108. private: \
  109. HMODULE _hmod; \
  110. PFN##Name _pfn; \
  111. PFN##Name2 _pfn2; \
  112. }
  113. #define DeclDynLoad3( DLL, Name, ReturnType, CallingConvention, Args, ArgsSansTypes, \
  114. Name2, ReturnType2, CallingConvention2, Args2, ArgsSansTypes2, \
  115. Name3, ReturnType3, CallingConvention3, Args3, ArgsSansTypes3 ) \
  116. \
  117. typedef ReturnType (CallingConvention * PFN##Name)##Args; \
  118. typedef ReturnType2 (CallingConvention2 * PFN##Name2)##Args2; \
  119. typedef ReturnType3 (CallingConvention3 * PFN##Name3)##Args3; \
  120. \
  121. class CDynLoad##DLL \
  122. { \
  123. public: \
  124. CDynLoad##DLL() \
  125. { \
  126. _hmod = LoadLibraryA( #DLL ".dll" ); \
  127. \
  128. if ( 0 == _hmod ) \
  129. THROW( CException() ); \
  130. \
  131. _pfn = (PFN##Name)GetProcAddress( _hmod, #Name ); \
  132. \
  133. if ( 0 == _pfn ) \
  134. { \
  135. FreeLibrary( _hmod ); \
  136. THROW( CException() ); \
  137. } \
  138. \
  139. _pfn2 = (PFN##Name2)GetProcAddress( _hmod, #Name2 ); \
  140. \
  141. if ( 0 == _pfn2 ) \
  142. { \
  143. FreeLibrary( _hmod ); \
  144. THROW( CException() ); \
  145. } \
  146. \
  147. _pfn3 = (PFN##Name3)GetProcAddress( _hmod, #Name3 ); \
  148. \
  149. if ( 0 == _pfn3 ) \
  150. { \
  151. FreeLibrary( _hmod ); \
  152. THROW( CException() ); \
  153. } \
  154. } \
  155. \
  156. ~CDynLoad##DLL() \
  157. { \
  158. FreeLibrary( _hmod ); \
  159. } \
  160. \
  161. ReturnType Name##Args \
  162. { \
  163. return _pfn##ArgsSansTypes; \
  164. } \
  165. \
  166. ReturnType2 Name2##Args2 \
  167. { \
  168. return _pfn2##ArgsSansTypes2; \
  169. } \
  170. \
  171. ReturnType3 Name3##Args3 \
  172. { \
  173. return _pfn3##ArgsSansTypes3; \
  174. } \
  175. \
  176. private: \
  177. HMODULE _hmod; \
  178. PFN##Name _pfn; \
  179. PFN##Name2 _pfn2; \
  180. PFN##Name3 _pfn3; \
  181. }
  182. #define DeclDynLoad4( DLL, Name, ReturnType, CallingConvention, Args, ArgsSansTypes, \
  183. Name2, ReturnType2, CallingConvention2, Args2, ArgsSansTypes2, \
  184. Name3, ReturnType3, CallingConvention3, Args3, ArgsSansTypes3, \
  185. Name4, ReturnType4, CallingConvention4, Args4, ArgsSansTypes4 ) \
  186. \
  187. typedef ReturnType (CallingConvention * PFN##Name)##Args; \
  188. typedef ReturnType2 (CallingConvention2 * PFN##Name2)##Args2; \
  189. typedef ReturnType3 (CallingConvention3 * PFN##Name3)##Args3; \
  190. typedef ReturnType4 (CallingConvention4 * PFN##Name4)##Args4; \
  191. \
  192. class CDynLoad##DLL \
  193. { \
  194. public: \
  195. CDynLoad##DLL() \
  196. { \
  197. _hmod = LoadLibraryA( #DLL ".dll" ); \
  198. \
  199. if ( 0 == _hmod ) \
  200. THROW( CException() ); \
  201. \
  202. _pfn = (PFN##Name)GetProcAddress( _hmod, #Name ); \
  203. \
  204. if ( 0 == _pfn ) \
  205. { \
  206. FreeLibrary( _hmod ); \
  207. THROW( CException() ); \
  208. } \
  209. \
  210. _pfn2 = (PFN##Name2)GetProcAddress( _hmod, #Name2 ); \
  211. \
  212. if ( 0 == _pfn2 ) \
  213. { \
  214. FreeLibrary( _hmod ); \
  215. THROW( CException() ); \
  216. } \
  217. \
  218. _pfn3 = (PFN##Name3)GetProcAddress( _hmod, #Name3 ); \
  219. \
  220. if ( 0 == _pfn3 ) \
  221. { \
  222. FreeLibrary( _hmod ); \
  223. THROW( CException() ); \
  224. } \
  225. \
  226. _pfn4 = (PFN##Name4)GetProcAddress( _hmod, #Name4 ); \
  227. \
  228. if ( 0 == _pfn4 ) \
  229. { \
  230. FreeLibrary( _hmod ); \
  231. THROW( CException() ); \
  232. } \
  233. } \
  234. \
  235. ~CDynLoad##DLL() \
  236. { \
  237. FreeLibrary( _hmod ); \
  238. } \
  239. \
  240. ReturnType Name##Args \
  241. { \
  242. return _pfn##ArgsSansTypes; \
  243. } \
  244. \
  245. ReturnType2 Name2##Args2 \
  246. { \
  247. return _pfn2##ArgsSansTypes2; \
  248. } \
  249. \
  250. ReturnType3 Name3##Args3 \
  251. { \
  252. return _pfn3##ArgsSansTypes3; \
  253. } \
  254. \
  255. ReturnType4 Name4##Args4 \
  256. { \
  257. return _pfn4##ArgsSansTypes4; \
  258. } \
  259. \
  260. private: \
  261. HMODULE _hmod; \
  262. PFN##Name _pfn; \
  263. PFN##Name2 _pfn2; \
  264. PFN##Name3 _pfn3; \
  265. PFN##Name4 _pfn4; \
  266. }
  267. #define DeclDynLoad5( DLL, Name, ReturnType, CallingConvention, Args, ArgsSansTypes, \
  268. Name2, ReturnType2, CallingConvention2, Args2, ArgsSansTypes2, \
  269. Name3, ReturnType3, CallingConvention3, Args3, ArgsSansTypes3, \
  270. Name4, ReturnType4, CallingConvention4, Args4, ArgsSansTypes4, \
  271. Name5, ReturnType5, CallingConvention5, Args5, ArgsSansTypes5 ) \
  272. \
  273. typedef ReturnType (CallingConvention * PFN##Name)##Args; \
  274. typedef ReturnType2 (CallingConvention2 * PFN##Name2)##Args2; \
  275. typedef ReturnType3 (CallingConvention3 * PFN##Name3)##Args3; \
  276. typedef ReturnType4 (CallingConvention4 * PFN##Name4)##Args4; \
  277. typedef ReturnType5 (CallingConvention5 * PFN##Name5)##Args5; \
  278. \
  279. class CDynLoad##DLL \
  280. { \
  281. public: \
  282. CDynLoad##DLL() \
  283. { \
  284. _hmod = LoadLibraryA( #DLL ".dll" ); \
  285. \
  286. if ( 0 == _hmod ) \
  287. THROW( CException() ); \
  288. \
  289. _pfn = (PFN##Name)GetProcAddress( _hmod, #Name ); \
  290. \
  291. if ( 0 == _pfn ) \
  292. { \
  293. FreeLibrary( _hmod ); \
  294. THROW( CException() ); \
  295. } \
  296. \
  297. _pfn2 = (PFN##Name2)GetProcAddress( _hmod, #Name2 ); \
  298. \
  299. if ( 0 == _pfn2 ) \
  300. { \
  301. FreeLibrary( _hmod ); \
  302. THROW( CException() ); \
  303. } \
  304. \
  305. _pfn3 = (PFN##Name3)GetProcAddress( _hmod, #Name3 ); \
  306. \
  307. if ( 0 == _pfn3 ) \
  308. { \
  309. FreeLibrary( _hmod ); \
  310. THROW( CException() ); \
  311. } \
  312. \
  313. _pfn4 = (PFN##Name4)GetProcAddress( _hmod, #Name4 ); \
  314. \
  315. if ( 0 == _pfn4 ) \
  316. { \
  317. FreeLibrary( _hmod ); \
  318. THROW( CException() ); \
  319. } \
  320. \
  321. _pfn5 = (PFN##Name5)GetProcAddress( _hmod, #Name5 ); \
  322. \
  323. if ( 0 == _pfn5 ) \
  324. { \
  325. FreeLibrary( _hmod ); \
  326. THROW( CException() ); \
  327. } \
  328. } \
  329. \
  330. ~CDynLoad##DLL() \
  331. { \
  332. FreeLibrary( _hmod ); \
  333. } \
  334. \
  335. ReturnType Name##Args \
  336. { \
  337. return _pfn##ArgsSansTypes; \
  338. } \
  339. \
  340. ReturnType2 Name2##Args2 \
  341. { \
  342. return _pfn2##ArgsSansTypes2; \
  343. } \
  344. \
  345. ReturnType3 Name3##Args3 \
  346. { \
  347. return _pfn3##ArgsSansTypes3; \
  348. } \
  349. \
  350. ReturnType4 Name4##Args4 \
  351. { \
  352. return _pfn4##ArgsSansTypes4; \
  353. } \
  354. \
  355. ReturnType5 Name5##Args5 \
  356. { \
  357. return _pfn5##ArgsSansTypes5; \
  358. } \
  359. \
  360. private: \
  361. HMODULE _hmod; \
  362. PFN##Name _pfn; \
  363. PFN##Name2 _pfn2; \
  364. PFN##Name3 _pfn3; \
  365. PFN##Name4 _pfn4; \
  366. PFN##Name5 _pfn5; \
  367. }