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.

338 lines
9.7 KiB

  1. //============================================================
  2. //
  3. // WBEMPSAPI.cpp - implementation of PSAPI.DLL access class
  4. //
  5. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  6. //
  7. // 01/21/97 a-jmoon created
  8. //
  9. //============================================================
  10. #include "precomp.h"
  11. #include <winerror.h>
  12. #include "WBEMPSAPI.h"
  13. #include <strsafe.h>
  14. #pragma warning(disable : 4995)
  15. #ifdef NTONLY
  16. //
  17. // resource management failures
  18. //
  19. extern BOOL bAddInstanceCreatorFailure ;
  20. /**********************************************************************************************************
  21. * Register this class with the CResourceManager.
  22. **********************************************************************************************************/
  23. // {A8CFDD23-C2D2-11d2-B352-00105A1F8569}
  24. const GUID guidPSAPI =
  25. { 0xa8cfdd23, 0xc2d2, 0x11d2, { 0xb3, 0x52, 0x0, 0x10, 0x5a, 0x1f, 0x85, 0x69 } };
  26. class CPSAPICreatorRegistration
  27. {
  28. public:
  29. CPSAPICreatorRegistration ()
  30. {
  31. try
  32. {
  33. BOOL bNonFailure =
  34. CResourceManager::sm_TheResourceManager.AddInstanceCreator ( guidPSAPI, CPSAPICreator ) ;
  35. if ( FALSE == bNonFailure )
  36. {
  37. bAddInstanceCreatorFailure = TRUE ;
  38. }
  39. }
  40. catch ( CHeap_Exception& e_HE )
  41. {
  42. bAddInstanceCreatorFailure = TRUE ;
  43. }
  44. }
  45. ~CPSAPICreatorRegistration ()
  46. {}
  47. static CResource * CPSAPICreator ( PVOID pData )
  48. {
  49. CPSAPI *t_pPsapi = new CPSAPI ;
  50. if ( !t_pPsapi )
  51. {
  52. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  53. }
  54. return t_pPsapi ;
  55. }
  56. };
  57. CPSAPICreatorRegistration MyCPSAPICreatorRegistration ;
  58. /**********************************************************************************************************/
  59. /*****************************************************************************
  60. *
  61. * FUNCTION : CPSAPI::CPSAPI
  62. *
  63. * DESCRIPTION : Constructor
  64. *
  65. * INPUTS : none
  66. *
  67. * OUTPUTS : none
  68. *
  69. * RETURNS : nothing
  70. *
  71. * COMMENTS :
  72. *
  73. *****************************************************************************/
  74. CPSAPI::CPSAPI() : CTimedDllResource () {
  75. hLibHandle = NULL ;
  76. Init () ;
  77. }
  78. /*****************************************************************************
  79. *
  80. * FUNCTION : CPSAPI::~CPSAPI
  81. *
  82. * DESCRIPTION : Destructor
  83. *
  84. * INPUTS : none
  85. *
  86. * OUTPUTS : none
  87. *
  88. * RETURNS : nothing
  89. *
  90. * COMMENTS :
  91. *
  92. *****************************************************************************/
  93. CPSAPI::~CPSAPI() {
  94. if(hLibHandle != NULL) {
  95. FreeLibrary(hLibHandle) ;
  96. }
  97. }
  98. /*****************************************************************************
  99. *
  100. * FUNCTION : CPSAPI::Init
  101. *
  102. * DESCRIPTION : Loads CSAPI.DLL, locates entry points
  103. *
  104. * INPUTS : none
  105. *
  106. * OUTPUTS : none
  107. *
  108. * RETURNS : ERROR_SUCCESS or windows error code
  109. *
  110. * COMMENTS :
  111. *
  112. *****************************************************************************/
  113. LONG CPSAPI::Init() {
  114. // Try to load CSAPI.DLL
  115. //======================
  116. if(hLibHandle == NULL) {
  117. hLibHandle = LoadLibrary(_T("PSAPI.DLL")) ;
  118. if(hLibHandle == NULL) {
  119. // this is possible to be neccessary in the future !!!
  120. // resource manager may start to care about error from load library
  121. //
  122. // let resource manager know load failed
  123. //
  124. m_bValid = FALSE;
  125. m_dwCreationError = ::GetLastError ();
  126. LogErrorMessage(L"Failed to load library psapi.dll");
  127. }
  128. else {
  129. // Find the entry points
  130. //======================
  131. pEnumProcesses = (PSAPI_ENUM_PROCESSES) GetProcAddress(hLibHandle, "EnumProcesses") ;
  132. pEnumDeviceDrivers = (PSAPI_ENUM_DRIVERS) GetProcAddress(hLibHandle, "EnumDeviceDrivers") ;
  133. pEnumProcessModules = (PSAPI_ENUM_MODULES) GetProcAddress(hLibHandle, "EnumProcessModules") ;
  134. pGetProcessMemoryInfo = (PSAPI_GET_MEMORY_INFO) GetProcAddress(hLibHandle, "GetProcessMemoryInfo") ;
  135. #ifdef UNICODE
  136. pGetDeviceDriverBaseName = (PSAPI_GET_DRIVER_NAME) GetProcAddress(hLibHandle, "GetDeviceDriverBaseNameW") ;
  137. pGetModuleBaseName = (PSAPI_GET_MODULE_NAME) GetProcAddress(hLibHandle, "GetModuleBaseNameW") ;
  138. pGetDeviceDriverFileName = (PSAPI_GET_DRIVER_EXE) GetProcAddress(hLibHandle, "GetDeviceDriverFileNameW") ;
  139. pGetModuleFileNameEx = (PSAPI_GET_MODULE_EXE) GetProcAddress(hLibHandle, "GetModuleFileNameExW") ;
  140. #else
  141. pGetDeviceDriverBaseName = (PSAPI_GET_DRIVER_NAME) GetProcAddress(hLibHandle, "GetDeviceDriverBaseNameA") ;
  142. pGetModuleBaseName = (PSAPI_GET_MODULE_NAME) GetProcAddress(hLibHandle, "GetModuleBaseNameA") ;
  143. pGetDeviceDriverFileName = (PSAPI_GET_DRIVER_EXE) GetProcAddress(hLibHandle, "GetDeviceDriverFileNameA") ;
  144. pGetModuleFileNameEx = (PSAPI_GET_MODULE_EXE) GetProcAddress(hLibHandle, "GetModuleFileNameExA") ;
  145. #endif
  146. if(pEnumProcesses == NULL ||
  147. pEnumDeviceDrivers == NULL ||
  148. pEnumProcessModules == NULL ||
  149. pGetDeviceDriverBaseName == NULL ||
  150. pGetModuleBaseName == NULL ||
  151. pGetDeviceDriverFileName == NULL ||
  152. pGetModuleFileNameEx == NULL ||
  153. pGetProcessMemoryInfo == NULL) {
  154. // Couldn't get one or more entry points
  155. //======================================
  156. FreeLibrary(hLibHandle) ;
  157. hLibHandle = NULL ;
  158. // this is possible to be neccessary in the future !!!
  159. // resource manager may start to care about error from load library
  160. //
  161. // let resource manager know load failed
  162. //
  163. m_bValid = FALSE;
  164. m_dwCreationError = ERROR_PROC_NOT_FOUND;
  165. ::SetLastError (ERROR_PROC_NOT_FOUND);
  166. LogErrorMessage(L"Failed find entrypoint in wbempsapi");
  167. }
  168. }
  169. }
  170. return m_dwCreationError ;
  171. }
  172. /*****************************************************************************
  173. *
  174. * FUNCTION : CPSAPI::EnumProcesses
  175. * CPSAPI::EnumDeviceDrivers
  176. * CPSAPI::EnumProcessModules
  177. * CPSAPI::GetDeviceDriverBaseName
  178. * CPSAPI::GetModuleBaseName
  179. * CPSAPI::GetDeviceDriverFileName
  180. * CPSAPI::GetModuleFileNameEx
  181. * CPSAPI::GetProcessMemoryInfo
  182. *
  183. * DESCRIPTION : CSAPI function wrappers
  184. *
  185. * INPUTS : none
  186. *
  187. * OUTPUTS : none
  188. *
  189. * RETURNS : CSAPI return codes
  190. *
  191. * COMMENTS :
  192. *
  193. *****************************************************************************/
  194. BOOL CPSAPI::EnumProcesses(DWORD *pdwPIDList, DWORD dwListSize, DWORD *pdwByteCount) {
  195. if(hLibHandle == NULL) {
  196. return FALSE ;
  197. }
  198. return pEnumProcesses(pdwPIDList, dwListSize, pdwByteCount) ;
  199. }
  200. BOOL CPSAPI::EnumDeviceDrivers(LPVOID pImageBaseList, DWORD dwListSize, DWORD *pdwByteCount) {
  201. if(hLibHandle == NULL) {
  202. return FALSE ;
  203. }
  204. return pEnumDeviceDrivers(pImageBaseList, dwListSize, pdwByteCount) ;
  205. }
  206. BOOL CPSAPI::EnumProcessModules(HANDLE hProcess, HMODULE *ModuleList,
  207. DWORD dwListSize, DWORD *pdwByteCount) {
  208. if(hLibHandle == NULL) {
  209. return FALSE ;
  210. }
  211. return pEnumProcessModules(hProcess, ModuleList, dwListSize, pdwByteCount) ;
  212. }
  213. DWORD CPSAPI::GetDeviceDriverBaseName(LPVOID pImageBase, LPTSTR pszName, DWORD dwNameSize) {
  214. if(hLibHandle == NULL) {
  215. return 0 ;
  216. }
  217. return pGetDeviceDriverBaseName(pImageBase, pszName, dwNameSize) ;
  218. }
  219. DWORD CPSAPI::GetModuleBaseName(HANDLE hProcess, HMODULE hModule,
  220. LPTSTR pszName, DWORD dwNameSize) {
  221. if(hLibHandle == NULL) {
  222. return 0 ;
  223. }
  224. return pGetModuleBaseName(hProcess, hModule, pszName, dwNameSize) ;
  225. }
  226. DWORD CPSAPI::GetDeviceDriverFileName(LPVOID pImageBase, LPTSTR pszName, DWORD dwNameSize) {
  227. if(hLibHandle == NULL) {
  228. return 0 ;
  229. }
  230. return pGetDeviceDriverFileName(pImageBase, pszName, dwNameSize) ;
  231. }
  232. DWORD CPSAPI::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule,
  233. LPTSTR pszName, DWORD dwNameSize)
  234. {
  235. if (hLibHandle == NULL)
  236. return 0;
  237. DWORD dwRet = pGetModuleFileNameEx(hProcess, hModule, pszName, dwNameSize);
  238. if (dwRet)
  239. {
  240. // GetModuleFileNameEx sometimes returns some funky things like:
  241. // \\??\\C:\\blah\\...
  242. // \\SystemRoot\\system32\\blah\\..
  243. CHString strFilename = pszName;
  244. // If it starts with "\\??\\" get rid of it.
  245. if (strFilename.Find(_T("\\??\\")) == 0)
  246. lstrcpy(pszName, strFilename.Mid(sizeof(_T("\\??\\"))/sizeof(TCHAR) - 1));
  247. else if (strFilename.Find(_T("\\SystemRoot\\")) == 0)
  248. {
  249. if(GetWindowsDirectory(pszName, dwNameSize)){
  250. // Leave off that last '\\' so we seperate c:\\winnt from the
  251. // rest of the path.
  252. StringCchCat(pszName, dwNameSize, strFilename.Mid(sizeof(_T("\\SystemRoot"))/sizeof(TCHAR) - 1));
  253. }
  254. }
  255. }
  256. return dwRet;
  257. }
  258. BOOL CPSAPI::GetProcessMemoryInfo(HANDLE hProcess,
  259. PROCESS_MEMORY_COUNTERS *pMemCtrs,
  260. DWORD dwByteCount) {
  261. if(hLibHandle == NULL) {
  262. return 0 ;
  263. }
  264. return pGetProcessMemoryInfo(hProcess, pMemCtrs, dwByteCount) ;
  265. }
  266. #endif