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.

470 lines
14 KiB

  1. //=================================================================
  2. //
  3. // Kernel32API.cpp
  4. //
  5. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. #include <cominit.h>
  10. #include "Kernel32Api.h"
  11. #include "DllWrapperCreatorReg.h"
  12. // {DDEA7E32-CCE8-11d2-911E-0060081A46FD}
  13. static const GUID g_guidKernel32Api =
  14. {0xddea7e32, 0xcce8, 0x11d2, {0x91, 0x1e, 0x0, 0x60, 0x8, 0x1a, 0x46, 0xfd}};
  15. static const TCHAR g_tstrKernel32[] = _T("KERNEL32.DLL");
  16. /******************************************************************************
  17. * Register this class with the CResourceManager.
  18. *****************************************************************************/
  19. CDllApiWraprCreatrReg<CKernel32Api, &g_guidKernel32Api, g_tstrKernel32> MyRegisteredKernel32Wrapper;
  20. /******************************************************************************
  21. * Constructor
  22. ******************************************************************************/
  23. CKernel32Api::CKernel32Api(LPCTSTR a_tstrWrappedDllName)
  24. : CDllWrapperBase(a_tstrWrappedDllName),
  25. m_pfnGetDiskFreeSpaceEx(NULL),
  26. m_pfnCreateToolhelp32Snapshot(NULL),
  27. m_pfnThread32First(NULL),
  28. m_pfnThread32Next(NULL),
  29. m_pfnProcess32First(NULL),
  30. m_pfnProcess32Next(NULL),
  31. m_pfnModule32First(NULL),
  32. m_pfnModule32Next(NULL),
  33. m_pfnHeap32ListFirst(NULL),
  34. m_pfnGlobalMemoryStatusEx(NULL),
  35. m_pfnGetSystemDefaultUILanguage(NULL)
  36. {
  37. }
  38. /******************************************************************************
  39. * Destructor
  40. ******************************************************************************/
  41. CKernel32Api::~CKernel32Api()
  42. {
  43. }
  44. /******************************************************************************
  45. * Initialization function to check that we obtained function addresses.
  46. * Init should fail only if the minimum set of functions was not available;
  47. * functions added in later versions may or may not be present - it is the
  48. * client's responsibility in such cases to check, in their code, for the
  49. * version of the dll before trying to call such functions. Not doing so
  50. * when the function is not present will result in an AV.
  51. *
  52. * The Init function is called by the WrapperCreatorRegistation class.
  53. ******************************************************************************/
  54. bool CKernel32Api::Init()
  55. {
  56. bool fRet = LoadLibrary();
  57. if(fRet)
  58. {
  59. #ifdef NTONLY
  60. m_pfnGetDiskFreeSpaceEx = (PFN_KERNEL32_GET_DISK_FREE_SPACE_EX)
  61. GetProcAddress("GetDiskFreeSpaceExW");
  62. m_pfnGetVolumePathName = (PFN_KERNEL32_GET_VOLUME_PATH_NAME)
  63. GetProcAddress("GetVolumePathNameW");
  64. #endif
  65. #ifdef WIN9XONLY
  66. m_pfnGetDiskFreeSpaceEx = (PFN_KERNEL32_GET_DISK_FREE_SPACE_EX)
  67. GetProcAddress("GetDiskFreeSpaceExA");
  68. m_pfnGetVolumePathName = (PFN_KERNEL32_GET_VOLUME_PATH_NAME)
  69. GetProcAddress("GetVolumePathNameA");
  70. #endif
  71. // NT5 ONLY FUNCTIONS
  72. m_pfnCreateToolhelp32Snapshot = (PFN_KERNEL32_CREATE_TOOLHELP32_SNAPSHOT)
  73. GetProcAddress("CreateToolhelp32Snapshot");
  74. m_pfnThread32First = (PFN_KERNEL32_THREAD32_FIRST)
  75. GetProcAddress("Thread32First");
  76. m_pfnThread32Next = (PFN_KERNEL32_THREAD32_NEXT)
  77. GetProcAddress("Thread32Next");
  78. m_pfnProcess32First = (PFN_KERNEL32_PROCESS32_FIRST)
  79. GetProcAddress("Process32First");
  80. m_pfnProcess32Next = (PFN_KERNEL32_PROCESS32_NEXT)
  81. GetProcAddress("Process32Next");
  82. m_pfnModule32First = (PFN_KERNEL32_MODULE32_FIRST)
  83. GetProcAddress("Module32First");
  84. m_pfnModule32Next = (PFN_KERNEL32_MODULE32_NEXT)
  85. GetProcAddress("Module32Next");
  86. m_pfnHeap32ListFirst = (PFN_KERNEL32_HEAP32_LIST_FIRST)
  87. GetProcAddress("Heap32ListFirst");
  88. m_pfnGlobalMemoryStatusEx = (PFN_KERNEL32_GLOBAL_MEMORY_STATUS_EX)
  89. GetProcAddress("GlobalMemoryStatusEx");
  90. m_pfnGetSystemDefaultUILanguage = (PFN_KERNEL32_GET_SYSTEM_DEFAULT_U_I_LANGUAGE)
  91. GetProcAddress("GetSystemDefaultUILanguage");
  92. // Check that we have function pointers to functions that should be
  93. // present in all versions of this dll...
  94. // ( in this case, ALL these are functions that may or may not be
  95. // present, so don't bother)
  96. }
  97. return fRet;
  98. }
  99. /******************************************************************************
  100. * Member functions wrapping Kernel32 api functions. Add new functions here
  101. * as required.
  102. ******************************************************************************/
  103. // This member function's wrapped pointer has not been validated as it may
  104. // not exist on all versions of the dll. Hence the wrapped function's normal
  105. // return value is returned via the last parameter, while the result of the
  106. // function indicates whether the function existed or not in the wrapped dll.
  107. bool CKernel32Api::GetDiskFreeSpaceEx
  108. (
  109. LPCTSTR a_lpDirectoryName,
  110. PULARGE_INTEGER a_lpFreeBytesAvailableToCaller,
  111. PULARGE_INTEGER a_lpTotalNumberOfBytes,
  112. PULARGE_INTEGER a_lpTotalNumberOfFreeBytes,
  113. BOOL *a_pfRetval
  114. )
  115. {
  116. BOOL t_fExists = FALSE;
  117. BOOL t_fTemp = FALSE;
  118. if(m_pfnGetDiskFreeSpaceEx != NULL)
  119. {
  120. t_fTemp = m_pfnGetDiskFreeSpaceEx(a_lpDirectoryName,
  121. a_lpFreeBytesAvailableToCaller,
  122. a_lpTotalNumberOfBytes,
  123. a_lpTotalNumberOfFreeBytes);
  124. t_fExists = TRUE;
  125. if(a_pfRetval != NULL)
  126. {
  127. *a_pfRetval = t_fTemp;
  128. }
  129. }
  130. return t_fExists;
  131. }
  132. // This member function's wrapped pointer has not been validated as it may
  133. // not exist on all versions of the dll. Hence the wrapped function's normal
  134. // return value is returned via the last parameter, while the result of the
  135. // function indicates whether the function existed or not in the wrapped dll.
  136. bool CKernel32Api::CreateToolhelp32Snapshot
  137. (
  138. DWORD a_dwFlags,
  139. DWORD a_th32ProcessID,
  140. HANDLE *a_phRetval
  141. )
  142. {
  143. bool t_fExists = false;
  144. HANDLE t_hTemp;
  145. if(m_pfnCreateToolhelp32Snapshot != NULL)
  146. {
  147. t_hTemp = m_pfnCreateToolhelp32Snapshot(a_dwFlags, a_th32ProcessID);
  148. t_fExists = true;
  149. if(a_phRetval != NULL)
  150. {
  151. *a_phRetval = t_hTemp;
  152. }
  153. }
  154. return t_fExists;
  155. }
  156. // This member function's wrapped pointer has not been validated as it may
  157. // not exist on all versions of the dll. Hence the wrapped function's normal
  158. // return value is returned via the last parameter, while the result of the
  159. // function indicates whether the function existed or not in the wrapped dll.
  160. bool CKernel32Api::Thread32First
  161. (
  162. HANDLE a_hSnapshot,
  163. LPTHREADENTRY32 a_lpte,
  164. BOOL *a_pfRetval
  165. )
  166. {
  167. bool t_fExists = false;
  168. BOOL t_fTemp = FALSE;
  169. if(m_pfnThread32First != NULL)
  170. {
  171. t_fTemp = m_pfnThread32First(a_hSnapshot, a_lpte);
  172. t_fExists = true;
  173. if(a_pfRetval != NULL)
  174. {
  175. *a_pfRetval = t_fTemp;
  176. }
  177. }
  178. return t_fExists;
  179. }
  180. // This member function's wrapped pointer has not been validated as it may
  181. // not exist on all versions of the dll. Hence the wrapped function's normal
  182. // return value is returned via the last parameter, while the result of the
  183. // function indicates whether the function existed or not in the wrapped dll.
  184. bool CKernel32Api::Thread32Next
  185. (
  186. HANDLE a_hSnapshot,
  187. LPTHREADENTRY32 a_lpte,
  188. BOOL *a_pfRetval
  189. )
  190. {
  191. bool t_fExists = false;
  192. BOOL t_fTemp = FALSE;
  193. if(m_pfnThread32Next != NULL)
  194. {
  195. t_fTemp = m_pfnThread32Next(a_hSnapshot, a_lpte);
  196. t_fExists = true;
  197. if(a_pfRetval != NULL)
  198. {
  199. *a_pfRetval = t_fTemp;
  200. }
  201. }
  202. return t_fExists;
  203. }
  204. // This member function's wrapped pointer has not been validated as it may
  205. // not exist on all versions of the dll. Hence the wrapped function's normal
  206. // return value is returned via the last parameter, while the result of the
  207. // function indicates whether the function existed or not in the wrapped dll.
  208. bool CKernel32Api::Process32First
  209. (
  210. HANDLE a_hSnapshot,
  211. LPPROCESSENTRY32 a_lppe,
  212. BOOL *a_pfRetval
  213. )
  214. {
  215. bool t_fExists = false;
  216. BOOL t_fTemp = FALSE;
  217. if(m_pfnProcess32First != NULL)
  218. {
  219. t_fTemp = m_pfnProcess32First(a_hSnapshot, a_lppe);
  220. t_fExists = true;
  221. if(a_pfRetval != NULL)
  222. {
  223. *a_pfRetval = t_fTemp;
  224. }
  225. }
  226. return t_fExists;
  227. }
  228. // This member function's wrapped pointer has not been validated as it may
  229. // not exist on all versions of the dll. Hence the wrapped function's normal
  230. // return value is returned via the last parameter, while the result of the
  231. // function indicates whether the function existed or not in the wrapped dll.
  232. bool CKernel32Api::Process32Next
  233. (
  234. HANDLE a_hSnapshot,
  235. LPPROCESSENTRY32 a_lppe,
  236. BOOL *a_pfRetval
  237. )
  238. {
  239. bool t_fExists = false;
  240. BOOL t_fTemp = FALSE;
  241. if(m_pfnProcess32Next != NULL)
  242. {
  243. t_fTemp = m_pfnProcess32Next(a_hSnapshot, a_lppe);
  244. t_fExists = true;
  245. if(a_pfRetval != NULL)
  246. {
  247. *a_pfRetval = t_fTemp;
  248. }
  249. }
  250. return t_fExists;
  251. }
  252. // This member function's wrapped pointer has not been validated as it may
  253. // not exist on all versions of the dll. Hence the wrapped function's normal
  254. // return value is returned via the last parameter, while the result of the
  255. // function indicates whether the function existed or not in the wrapped dll.
  256. bool CKernel32Api::Module32First
  257. (
  258. HANDLE a_hSnapshot,
  259. LPMODULEENTRY32 a_lpme,
  260. BOOL *a_pfRetval
  261. )
  262. {
  263. bool t_fExists = false;
  264. BOOL t_fTemp = FALSE;
  265. if(m_pfnModule32First != NULL)
  266. {
  267. t_fTemp = m_pfnModule32First(a_hSnapshot, a_lpme);
  268. t_fExists = true;
  269. if(a_pfRetval != NULL)
  270. {
  271. *a_pfRetval = t_fTemp;
  272. }
  273. }
  274. return t_fExists;
  275. }
  276. // This member function's wrapped pointer has not been validated as it may
  277. // not exist on all versions of the dll. Hence the wrapped function's normal
  278. // return value is returned via the last parameter, while the result of the
  279. // function indicates whether the function existed or not in the wrapped dll.
  280. bool CKernel32Api::Module32Next
  281. (
  282. HANDLE a_hSnapshot,
  283. LPMODULEENTRY32 a_lpme,
  284. BOOL *a_pfRetval
  285. )
  286. {
  287. bool t_fExists = false;
  288. BOOL t_fTemp = FALSE;
  289. if(m_pfnModule32Next != NULL)
  290. {
  291. t_fTemp = m_pfnModule32Next(a_hSnapshot, a_lpme);
  292. t_fExists = true;
  293. if(a_pfRetval != NULL)
  294. {
  295. *a_pfRetval = t_fTemp;
  296. }
  297. }
  298. return t_fExists;
  299. }
  300. // This member function's wrapped pointer has not been validated as it may
  301. // not exist on all versions of the dll. Hence the wrapped function's normal
  302. // return value is returned via the last parameter, while the result of the
  303. // function indicates whether the function existed or not in the wrapped dll.
  304. bool CKernel32Api::Heap32ListFirst
  305. (
  306. HANDLE a_hSnapshot,
  307. LPHEAPLIST32 a_lphl,
  308. BOOL *a_pfRetval
  309. )
  310. {
  311. bool t_fExists = false;
  312. BOOL t_fTemp = FALSE;
  313. if(m_pfnHeap32ListFirst != NULL)
  314. {
  315. t_fTemp = m_pfnHeap32ListFirst(a_hSnapshot, a_lphl);
  316. t_fExists = true;
  317. if(a_pfRetval != NULL)
  318. {
  319. *a_pfRetval = t_fTemp;
  320. }
  321. }
  322. return t_fExists;
  323. }
  324. // This member function's wrapped pointer has not been validated as it may
  325. // not exist on all versions of the dll. Hence the wrapped function's normal
  326. // return value is returned via the last parameter, while the result of the
  327. // function indicates whether the function existed or not in the wrapped dll.
  328. bool CKernel32Api::GlobalMemoryStatusEx
  329. (
  330. IN OUT LPMEMORYSTATUSEX a_lpBuffer,
  331. BOOL *a_pfRetval
  332. )
  333. {
  334. BOOL t_fExists = FALSE;
  335. BOOL t_fTemp = FALSE;
  336. if(m_pfnGlobalMemoryStatusEx != NULL && a_pfRetval != NULL)
  337. {
  338. t_fTemp = m_pfnGlobalMemoryStatusEx(a_lpBuffer);
  339. t_fExists = TRUE;
  340. if(a_pfRetval != NULL)
  341. {
  342. *a_pfRetval = t_fTemp;
  343. }
  344. }
  345. return t_fExists;
  346. }
  347. // This member function's wrapped pointer has not been validated as it may
  348. // not exist on all versions of the dll. Hence the wrapped function's normal
  349. // return value is returned via the last parameter, while the result of the
  350. // function indicates whether the function existed or not in the wrapped dll.
  351. bool CKernel32Api::GetSystemDefaultUILanguage
  352. (
  353. LANGID *a_plidRetval
  354. )
  355. {
  356. BOOL t_fExists = FALSE;
  357. LANGID t_lidTemp;
  358. if(m_pfnGetSystemDefaultUILanguage != NULL && a_plidRetval != NULL)
  359. {
  360. t_lidTemp = m_pfnGetSystemDefaultUILanguage();
  361. t_fExists = TRUE;
  362. if(a_plidRetval != NULL)
  363. {
  364. *a_plidRetval = t_lidTemp;
  365. }
  366. }
  367. return t_fExists;
  368. }
  369. // This member function's wrapped pointer has not been validated as it may
  370. // not exist on all versions of the dll. Hence the wrapped function's normal
  371. // return value is returned via the last parameter, while the result of the
  372. // function indicates whether the function existed or not in the wrapped dll.
  373. bool CKernel32Api::GetVolumePathName(
  374. LPCTSTR lpszFileName,
  375. LPTSTR lpszVolumePathName,
  376. DWORD dwBufferLength,
  377. BOOL *pfRetval)
  378. {
  379. bool fExists = false;
  380. BOOL fTemp = FALSE;
  381. if(m_pfnGetVolumePathName != NULL &&
  382. pfRetval != NULL)
  383. {
  384. fTemp = m_pfnGetVolumePathName(
  385. lpszFileName,
  386. lpszVolumePathName,
  387. dwBufferLength);
  388. fExists = true;
  389. if(pfRetval != NULL)
  390. {
  391. *pfRetval = fTemp;
  392. }
  393. }
  394. return fExists;
  395. }