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.

378 lines
12 KiB

  1. //=================================================================
  2. //
  3. // WmiApi.cpp
  4. //
  5. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. #include <cominit.h>
  10. #include <assertbreak.h>
  11. #include "WmiApi.h"
  12. #include "DllWrapperCreatorReg.h"
  13. // {DD3B4892-CD0F-11d2-911E-0060081A46FD}
  14. static const GUID g_guidWmiApi =
  15. {0xdd3b4892, 0xcd0f, 0x11d2, {0x91, 0x1e, 0x0, 0x60, 0x8, 0x1a, 0x46, 0xfd}};
  16. static const TCHAR g_tstrWmi[] = _T("WMI.DLL");
  17. /******************************************************************************
  18. * Register this class with the CResourceManager.
  19. *****************************************************************************/
  20. CDllApiWraprCreatrReg<CWmiApi, &g_guidWmiApi, g_tstrWmi> MyRegisteredWmiWrapper;
  21. /******************************************************************************
  22. * Constructor
  23. ******************************************************************************/
  24. CWmiApi::CWmiApi(LPCTSTR a_tstrWrappedDllName)
  25. : CDllWrapperBase(a_tstrWrappedDllName),
  26. m_pfnWmiQueryAllData(NULL),
  27. m_pfnWmiOpenBlock(NULL),
  28. m_pfnWmiCloseBlock(NULL),
  29. m_pfnWmiQuerySingleInstance(NULL),
  30. m_pfnWmiSetSingleItem(NULL),
  31. m_pfnWmiSetSingleInstance(NULL),
  32. m_pfnWmiExecuteMethod(NULL),
  33. m_pfnWmiNotificationRegistraton(NULL),
  34. m_pfnWmiFreeBuffer(NULL),
  35. m_pfnWmiEnumerateGuids(NULL),
  36. m_pfnWmiMofEnumerateResources(NULL),
  37. m_pfnWmiFileHandleToInstanceName(NULL),
  38. m_pfnWmiDevInstToInstanceName(NULL),
  39. m_pfnWmiQueryGuidInformation(NULL)
  40. {
  41. }
  42. /******************************************************************************
  43. * Destructor
  44. ******************************************************************************/
  45. CWmiApi::~CWmiApi()
  46. {
  47. }
  48. /******************************************************************************
  49. * Initialization function to check that we obtained function addresses.
  50. ******************************************************************************/
  51. bool CWmiApi::Init()
  52. {
  53. bool fRet = LoadLibrary();
  54. if(fRet)
  55. {
  56. #ifdef NTONLY
  57. m_pfnWmiQueryAllData = (PFN_WMI_QUERY_ALL_DATA)
  58. GetProcAddress("WmiQueryAllDataW");
  59. m_pfnWmiQuerySingleInstance = (PFN_WMI_QUERY_SINGLE_INSTANCE)
  60. GetProcAddress("WmiQuerySingleInstanceW");
  61. m_pfnWmiSetSingleItem = (PFN_WMI_SET_SINGLE_ITEM)
  62. GetProcAddress("WmiSetSingleItemW");
  63. m_pfnWmiSetSingleInstance = (PFN_WMI_SET_SINGLE_INSTANCE)
  64. GetProcAddress("WmiSetSingleInstanceW");
  65. m_pfnWmiExecuteMethod = (PFN_WMI_EXECUTE_METHOD)
  66. GetProcAddress("WmiExecuteMethodW");
  67. m_pfnWmiNotificationRegistraton = (PFN_WMI_NOTIFICATION_REGRISTRATION)
  68. GetProcAddress("WmiNotificationRegistrationW");
  69. m_pfnWmiMofEnumerateResources = (PFN_WMI_MOF_ENUMERATE_RESOURCES)
  70. GetProcAddress("WmiMofEnumerateResourcesW");
  71. m_pfnWmiFileHandleToInstanceName = (PFN_WMI_FILE_HANDLE_TO_INSTANCE_NAME)
  72. GetProcAddress("WmiFileHandleToInstanceNameW");
  73. m_pfnWmiDevInstToInstanceName = (PFN_WMI_DEV_INST_TO_INSTANCE_NAME)
  74. GetProcAddress("WmiDevInstToInstanceNameW");
  75. m_pfnWmiQueryGuidInformation = (PFN_WMI_QUERY_GUID_INFORMATION)
  76. GetProcAddress("WmiQueryGuidInformation");
  77. fRet = (m_pfnWmiQueryAllData != NULL) &&
  78. (m_pfnWmiQuerySingleInstance != NULL) &&
  79. (m_pfnWmiSetSingleItem != NULL) &&
  80. (m_pfnWmiSetSingleInstance != NULL) &&
  81. (m_pfnWmiExecuteMethod != NULL) &&
  82. (m_pfnWmiNotificationRegistraton != NULL) &&
  83. (m_pfnWmiMofEnumerateResources != NULL) &&
  84. (m_pfnWmiFileHandleToInstanceName != NULL) &&
  85. (m_pfnWmiDevInstToInstanceName != NULL) &&
  86. (m_pfnWmiQueryGuidInformation != NULL);
  87. #endif
  88. #ifdef WIN9XONLY
  89. m_pfnWmiQueryAllData = (PFN_WMI_QUERY_ALL_DATA)
  90. GetProcAddress("WmiQueryAllDataA");
  91. m_pfnWmiQuerySingleInstance = (PFN_WMI_QUERY_SINGLE_INSTANCE)
  92. GetProcAddress("WmiQuerySingleInstanceA");
  93. m_pfnWmiSetSingleItem = (PFN_WMI_SET_SINGLE_ITEM)
  94. GetProcAddress("WmiSetSingleItemA");
  95. m_pfnWmiSetSingleInstance = (PFN_WMI_SET_SINGLE_INSTANCE)
  96. GetProcAddress("WmiSetSingleInstanceA");
  97. m_pfnWmiExecuteMethod = (PFN_WMI_EXECUTE_METHOD)
  98. GetProcAddress("WmiExecuteMethodA");
  99. m_pfnWmiNotificationRegistraton = (PFN_WMI_NOTIFICATION_REGRISTRATION)
  100. GetProcAddress("WmiNotificationRegistrationA");
  101. m_pfnWmiMofEnumerateResources = (PFN_WMI_MOF_ENUMERATE_RESOURCES)
  102. GetProcAddress("WmiMofEnumerateResourcesA");
  103. m_pfnWmiFileHandleToInstanceName = (PFN_WMI_FILE_HANDLE_TO_INSTANCE_NAME)
  104. GetProcAddress("WmiFileHandleToInstanceNameA");
  105. fRet = (m_pfnWmiQueryAllData != NULL) &&
  106. (m_pfnWmiQuerySingleInstance != NULL) &&
  107. (m_pfnWmiSetSingleItem != NULL) &&
  108. (m_pfnWmiSetSingleInstance != NULL) &&
  109. (m_pfnWmiExecuteMethod != NULL) &&
  110. (m_pfnWmiNotificationRegistraton != NULL) &&
  111. (m_pfnWmiMofEnumerateResources != NULL) &&
  112. (m_pfnWmiFileHandleToInstanceName != NULL);
  113. #endif
  114. m_pfnWmiOpenBlock = (PFN_WMI_OPEN_BLOCK)
  115. GetProcAddress("WmiOpenBlock");
  116. m_pfnWmiCloseBlock = (PFN_WMI_CLOSE_BLOCK)
  117. GetProcAddress("WmiCloseBlock");
  118. m_pfnWmiFreeBuffer = (PNF_WMI_FREE_BUFFER)
  119. GetProcAddress("WmiFreeBuffer");
  120. m_pfnWmiEnumerateGuids = (PFN_WMI_ENUMERATE_GUIDS)
  121. GetProcAddress("WmiEnumerateGuids");
  122. fRet = fRet &&
  123. (m_pfnWmiOpenBlock != NULL) &&
  124. (m_pfnWmiCloseBlock != NULL) &&
  125. (m_pfnWmiFreeBuffer != NULL) &&
  126. (m_pfnWmiEnumerateGuids != NULL);
  127. if (!fRet)
  128. {
  129. LogErrorMessage(L"Failed find entrypoint in wmiapi");
  130. }
  131. }
  132. return fRet;
  133. }
  134. /******************************************************************************
  135. * Member functions wrapping Wmi api functions. Add new functions here
  136. * as required.
  137. ******************************************************************************/
  138. ULONG CWmiApi::WmiQueryAllData
  139. (
  140. IN WMIHANDLE a_h,
  141. IN OUT ULONG* a_ul,
  142. OUT PVOID a_pv
  143. )
  144. {
  145. return m_pfnWmiQueryAllData(a_h, a_ul, a_pv);
  146. }
  147. ULONG CWmiApi::WmiOpenBlock
  148. (
  149. IN GUID* a_pguid,
  150. IN ULONG a_ul,
  151. OUT WMIHANDLE a_wmih
  152. )
  153. {
  154. return m_pfnWmiOpenBlock(a_pguid, a_ul, a_wmih);
  155. }
  156. ULONG CWmiApi::WmiCloseBlock
  157. (
  158. IN WMIHANDLE a_wmih
  159. )
  160. {
  161. return m_pfnWmiCloseBlock(a_wmih);
  162. }
  163. ULONG CWmiApi::WmiQuerySingleInstance
  164. (
  165. IN WMIHANDLE a_wmihDataBlockHandle,
  166. IN LPCTSTR a_tstrInstanceName,
  167. IN OUT ULONG* a_ulBufferSize,
  168. OUT PVOID a_pvBuffer
  169. )
  170. {
  171. return m_pfnWmiQuerySingleInstance(a_wmihDataBlockHandle,
  172. a_tstrInstanceName,
  173. a_ulBufferSize,
  174. a_pvBuffer);
  175. }
  176. ULONG CWmiApi::WmiSetSingleItem
  177. (
  178. IN WMIHANDLE a_wmihDataBlockHandle,
  179. IN LPCTSTR a_tstrInstanceName,
  180. IN ULONG a_ulDataItemId,
  181. IN ULONG a_ulReserved,
  182. IN ULONG a_ulValueBufferSize,
  183. IN PVOID a_pvValueBuffer
  184. )
  185. {
  186. return m_pfnWmiSetSingleItem(a_wmihDataBlockHandle,
  187. a_tstrInstanceName,
  188. a_ulDataItemId,
  189. a_ulReserved,
  190. a_ulValueBufferSize,
  191. a_pvValueBuffer);
  192. }
  193. ULONG CWmiApi::WmiSetSingleInstance
  194. (
  195. IN WMIHANDLE a_wmihDataBlockHandle,
  196. IN LPCTSTR a_tstrInstanceName,
  197. IN ULONG a_ulReserved,
  198. IN ULONG a_ulValueBufferSize,
  199. IN PVOID a_pvValueBuffer
  200. )
  201. {
  202. return m_pfnWmiSetSingleInstance(a_wmihDataBlockHandle,
  203. a_tstrInstanceName,
  204. a_ulReserved,
  205. a_ulValueBufferSize,
  206. a_pvValueBuffer);
  207. }
  208. ULONG CWmiApi::WmiExecuteMethod
  209. (
  210. IN WMIHANDLE a_wmihMethodDataBlockHandle,
  211. IN LPCTSTR a_tstrMethodInstanceName,
  212. IN ULONG a_ulMethodId,
  213. IN ULONG a_ulInputValueBufferSize,
  214. IN PVOID a_pvInputValueBuffer,
  215. IN OUT ULONG* a_ulOutputBufferSize,
  216. OUT PVOID a_pvOutputBuffer
  217. )
  218. {
  219. return m_pfnWmiExecuteMethod(a_wmihMethodDataBlockHandle,
  220. a_tstrMethodInstanceName,
  221. a_ulMethodId,
  222. a_ulInputValueBufferSize,
  223. a_pvInputValueBuffer,
  224. a_ulOutputBufferSize,
  225. a_pvOutputBuffer);
  226. }
  227. ULONG CWmiApi::WmiNotificationRegistration
  228. (
  229. IN LPGUID a_pguidGuid,
  230. IN BOOLEAN a_blnEnable,
  231. IN PVOID a_pvDeliveryInfo,
  232. IN ULONG_PTR a_pulDeliveryContext,
  233. IN ULONG a_ulFlags
  234. )
  235. {
  236. return m_pfnWmiNotificationRegistraton(a_pguidGuid,
  237. a_blnEnable,
  238. a_pvDeliveryInfo,
  239. a_pulDeliveryContext,
  240. a_ulFlags);
  241. }
  242. ULONG CWmiApi::WmiMofEnumerateResources
  243. (
  244. IN MOFHANDLE a_MofResourceHandle,
  245. OUT ULONG* a_pulMofResourceCount,
  246. OUT PMOFRESOURCEINFO* a_MofResourceInfo
  247. )
  248. {
  249. return m_pfnWmiMofEnumerateResources(a_MofResourceHandle,
  250. a_pulMofResourceCount,
  251. a_MofResourceInfo);
  252. }
  253. ULONG CWmiApi::WmiFileHandleToInstanceName
  254. (
  255. IN WMIHANDLE a_wmihDataBlockHandle,
  256. IN HANDLE a_hFileHandle,
  257. IN OUT ULONG* a_pulNumberCharacters,
  258. OUT TCHAR* a_tcInstanceNames
  259. )
  260. {
  261. return m_pfnWmiFileHandleToInstanceName(a_wmihDataBlockHandle,
  262. a_hFileHandle,
  263. a_pulNumberCharacters,
  264. a_tcInstanceNames);
  265. }
  266. ULONG CWmiApi::WmiDevInstToInstanceName
  267. (
  268. OUT TCHAR* a_ptcInstanceName,
  269. IN ULONG a_ulInstanceNameLength,
  270. IN TCHAR* a_tcDevInst,
  271. IN ULONG a_ulInstanceIndex
  272. )
  273. {
  274. ASSERT_BREAK(m_pfnWmiDevInstToInstanceName);
  275. if (m_pfnWmiDevInstToInstanceName)
  276. return m_pfnWmiDevInstToInstanceName(a_ptcInstanceName,
  277. a_ulInstanceNameLength,
  278. a_tcDevInst,
  279. a_ulInstanceIndex);
  280. return 0xffffffff;
  281. }
  282. void CWmiApi::WmiFreeBuffer
  283. (
  284. IN PVOID a_pvBuffer
  285. )
  286. {
  287. m_pfnWmiFreeBuffer(a_pvBuffer);
  288. }
  289. ULONG CWmiApi::WmiEnumerateGuids
  290. (
  291. OUT LPGUID a_lpguidGuidList,
  292. IN OUT ULONG* a_pulGuidCount
  293. )
  294. {
  295. return m_pfnWmiEnumerateGuids(a_lpguidGuidList,
  296. a_pulGuidCount);
  297. }
  298. ULONG CWmiApi::WmiQueryGuidInformation
  299. (
  300. IN WMIHANDLE a_wmihGuidHandle,
  301. OUT PWMIGUIDINFORMATION a_GuidInfo
  302. )
  303. {
  304. ASSERT_BREAK(m_pfnWmiQueryGuidInformation);
  305. if (m_pfnWmiQueryGuidInformation)
  306. return m_pfnWmiQueryGuidInformation(a_wmihGuidHandle,
  307. a_GuidInfo);
  308. return 0xffffffff;
  309. }