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.

428 lines
11 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 1999-2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // SAQueryNetInfo.cpp
  7. //
  8. // Description:
  9. // implement the class CSAQueryNetInfo
  10. //
  11. // History:
  12. // 1. lustar.li (Guogang Li), creation date in 7-DEC-2000
  13. //
  14. // Notes:
  15. //
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <debug.h>
  21. #include <wbemidl.h>
  22. #include <SAEventcomm.h>
  23. #include <oahelp.inl>
  24. #include "SAQueryNetInfo.h"
  25. //////////////////////////////////////////////////////////////////////////////
  26. //++
  27. //
  28. // method:
  29. // CSAQueryNetInfo::CSAQueryNetInfo
  30. //
  31. // Description:
  32. // Constructor
  33. //
  34. // Arguments:
  35. // [in] IWbemServices * - pointer to IWbemServices
  36. // [in] UINT - the interval of generate event
  37. //
  38. // Returns:
  39. // NONE
  40. //
  41. // History: lustar.li Created 12/7/2000
  42. //
  43. //--
  44. //////////////////////////////////////////////////////////////////////////////
  45. CSAQueryNetInfo::CSAQueryNetInfo(
  46. /*[in]*/ IWbemServices * pNS,
  47. /*[in]*/ UINT uiInterval
  48. )
  49. {
  50. m_uiQueryInterval = uiInterval;
  51. m_bLinkCable = TRUE;
  52. m_bFirstQuery = TRUE;
  53. m_nPacketsSent = 0;
  54. m_nPacketsCurrentSent = 0;
  55. m_nPacketsReceived = 0;
  56. m_nPacketsCurrentReceived = 0;
  57. m_pNs = pNS;
  58. m_pWmiNs = NULL;
  59. }
  60. //////////////////////////////////////////////////////////////////////////////
  61. //++
  62. //
  63. // method:
  64. // CSAQueryNetInfo::~CSAQueryNetInfo
  65. //
  66. // Description:
  67. // Destructor
  68. //
  69. // Arguments:
  70. // NONE
  71. //
  72. // Returns:
  73. // NONE
  74. //
  75. // History: lustar.li Created 12/7/2000
  76. //
  77. //--
  78. //////////////////////////////////////////////////////////////////////////////
  79. CSAQueryNetInfo::~CSAQueryNetInfo()
  80. {
  81. if(m_pWmiNs)
  82. m_pWmiNs->Release();
  83. }
  84. //////////////////////////////////////////////////////////////////////////////
  85. //++
  86. //
  87. // method:
  88. // CSAQueryNetInfo::GetDisplayInformation
  89. //
  90. // Description:
  91. // return the display information ID
  92. //
  93. // Arguments:
  94. // NONE
  95. //
  96. // Returns:
  97. // UINT
  98. //
  99. // History: lustar.li Created 12/7/2000
  100. //
  101. //--
  102. //////////////////////////////////////////////////////////////////////////////
  103. UINT
  104. CSAQueryNetInfo::GetDisplayInformation()
  105. {
  106. //
  107. // Generate an event one second
  108. //
  109. Sleep(m_uiQueryInterval);
  110. //
  111. // Get network information
  112. //
  113. if(!GetNetConnection()||!GetNetInfo())
  114. return SA_NET_DISPLAY_IDLE;
  115. //
  116. // first query
  117. //
  118. if(m_bFirstQuery)
  119. {
  120. m_bFirstQuery=!m_bFirstQuery;
  121. return SA_NET_DISPLAY_IDLE;
  122. }
  123. if(!m_bLinkCable)
  124. {
  125. return SA_NET_DISPLAY_NO_CABLE;
  126. }
  127. else if(m_nPacketsCurrentReceived||m_nPacketsCurrentSent)
  128. {
  129. return SA_NET_DISPLAY_TRANSMITING;
  130. }
  131. else
  132. return SA_NET_DISPLAY_IDLE;
  133. }
  134. //////////////////////////////////////////////////////////////////////////////
  135. //++
  136. //
  137. // method:
  138. // CSAQueryNetInfo::GetNetInfo
  139. //
  140. // Description:
  141. // get net info from wmi
  142. //
  143. // Arguments:
  144. // NONE
  145. //
  146. // Returns:
  147. // BOOL
  148. //
  149. // History: lustar.li Created 12/7/2000
  150. //
  151. //--
  152. //////////////////////////////////////////////////////////////////////////////
  153. BOOL
  154. CSAQueryNetInfo::GetNetInfo()
  155. {
  156. HRESULT hr;
  157. VARIANT vVal;
  158. ULONG uReturned;
  159. UINT uiPacketsSent = 0;
  160. UINT uiPacketsReceived = 0;
  161. IEnumWbemClassObject *pEnum = NULL;
  162. IWbemClassObject *pPerfInst = NULL;
  163. CBSTR bstrClassName = CBSTR(SANETCLASSNAME);
  164. CBSTR bstrPropName1 = CBSTR(SANETRECEIVEPACKET);
  165. CBSTR bstrPropName2 = CBSTR(SANETSENDPACKET);
  166. if ( ((BSTR)bstrClassName == NULL) ||
  167. ((BSTR)bstrPropName1 == NULL) ||
  168. ((BSTR)bstrPropName2 == NULL) )
  169. {
  170. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetInfo failed on memory allocation ");
  171. return FALSE;
  172. }
  173. //
  174. // Create the object enumerator to net transfer
  175. //
  176. hr = m_pNs->CreateInstanceEnum( bstrClassName,
  177. WBEM_FLAG_SHALLOW,
  178. NULL,
  179. &pEnum );
  180. if(hr == WBEM_NO_ERROR)
  181. {
  182. while ( pEnum->Next( INFINITE,
  183. 1,
  184. &pPerfInst,
  185. &uReturned ) == WBEM_NO_ERROR )
  186. {
  187. //
  188. // Get the property of "PacketsReceivedUnicastPersec"
  189. //
  190. if ( ( pPerfInst->Get( bstrPropName1,
  191. 0L,
  192. &vVal,
  193. NULL, NULL ) ) != WBEM_NO_ERROR )
  194. {
  195. pPerfInst->Release( );
  196. pEnum->Release();
  197. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetInfo failed \
  198. <Get PacketsReceivedUnicastPersec>");
  199. return FALSE;
  200. }
  201. uiPacketsReceived+=vVal.uintVal;
  202. //
  203. // Get the property of "PacketsSentUnicastPersec"
  204. //
  205. VariantInit(&vVal);
  206. if ( ( pPerfInst->Get( bstrPropName2,
  207. 0L,
  208. &vVal,
  209. NULL, NULL ) ) != WBEM_NO_ERROR )
  210. {
  211. pPerfInst->Release( );
  212. pEnum->Release();
  213. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetInfo failed \
  214. <Get PacketsSentUnicastPersec>");
  215. return FALSE;
  216. }
  217. uiPacketsSent+=vVal.uintVal;
  218. pPerfInst->Release( );
  219. }
  220. }
  221. else
  222. {
  223. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetInfo failed \
  224. <Create the object enumerator>");
  225. return FALSE;
  226. }
  227. pEnum->Release();
  228. //
  229. // update the data in this class
  230. //
  231. m_nPacketsCurrentReceived = uiPacketsReceived-m_nPacketsReceived;
  232. m_nPacketsReceived = uiPacketsReceived;
  233. m_nPacketsCurrentSent = uiPacketsSent-m_nPacketsSent;
  234. m_nPacketsSent = uiPacketsSent;
  235. return TRUE;
  236. }
  237. //////////////////////////////////////////////////////////////////////////////
  238. //++
  239. //
  240. // method:
  241. // CSAQueryNetInfo::Initialize()
  242. //
  243. // Description:
  244. // complete the initialize of the class
  245. //
  246. // Arguments:
  247. // NONE
  248. //
  249. // Returns:
  250. // BOOL
  251. //
  252. // History: lustar.li Created 12/8/2000
  253. //
  254. //--
  255. //////////////////////////////////////////////////////////////////////////////
  256. BOOL
  257. CSAQueryNetInfo::Initialize()
  258. {
  259. HRESULT hr;
  260. IWbemLocator *pIWbemLocator = NULL;
  261. IWbemServices *pIWbemServices = NULL;
  262. if(!m_pNs)
  263. {
  264. TRACE(" SANetworkMonitor: CSAQueryNetInfo::Initialize failed \
  265. <Namespace is NULL>");
  266. return FALSE;
  267. }
  268. CBSTR bstrNameSpace = CBSTR(SAWMINAMESPACE);
  269. if ((BSTR)bstrNameSpace == NULL)
  270. {
  271. TRACE(" SANetworkMonitor: CSAQueryNetInfo::Initialize failed on memory allocation ");
  272. return FALSE;
  273. }
  274. if ( CoCreateInstance( CLSID_WbemAdministrativeLocator,
  275. NULL,
  276. CLSCTX_INPROC_SERVER,
  277. IID_IWbemLocator,
  278. (LPVOID *) &pIWbemLocator ) == S_OK )
  279. {
  280. hr = pIWbemLocator->ConnectServer( bstrNameSpace,
  281. NULL,
  282. NULL,
  283. 0L,
  284. 0L,
  285. NULL,
  286. NULL,
  287. &m_pWmiNs );
  288. if(hr!=WBEM_S_NO_ERROR)
  289. {
  290. pIWbemLocator->Release();
  291. TRACE(" SANetworkMonitor: CSAQueryNetInfo::Initialize failed \
  292. <cannot connect server>");
  293. return FALSE;
  294. }
  295. }
  296. else
  297. {
  298. TRACE(" SANetworkMonitor: CSAQueryNetInfo::Initialize failed \
  299. <CoCreateInstance fail>");
  300. return FALSE;
  301. }
  302. pIWbemLocator->Release();
  303. return TRUE;
  304. }
  305. //////////////////////////////////////////////////////////////////////////////
  306. //++
  307. //
  308. // method:
  309. // CSAQueryNetInfo::GetNetConnection()
  310. //
  311. // Description:
  312. // get the net connection status
  313. //
  314. // Arguments:
  315. // NONE
  316. //
  317. // Returns:
  318. // BOOL
  319. //
  320. // History: lustar.li Created 12/8/2000
  321. //
  322. //--
  323. //////////////////////////////////////////////////////////////////////////////
  324. BOOL
  325. CSAQueryNetInfo::GetNetConnection()
  326. {
  327. HRESULT hr;
  328. VARIANT vVal;
  329. ULONG uReturned;
  330. IEnumWbemClassObject *pEnum = NULL;
  331. IWbemClassObject *pPerfInst = NULL;
  332. m_bLinkCable=TRUE;
  333. CBSTR bstrClassName = CBSTR(SAMEDIACLASSNAME);
  334. CBSTR bstrPropName = CBSTR(SAMEDIACONNECTSTATUS);
  335. if ( ((BSTR)bstrClassName == NULL) || ((BSTR)bstrPropName == NULL) )
  336. {
  337. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetConnection failed on memory allocation ");
  338. return FALSE;
  339. }
  340. //
  341. // Query the status of network connection
  342. //
  343. hr = m_pWmiNs->CreateInstanceEnum( bstrClassName,
  344. WBEM_FLAG_SHALLOW,
  345. NULL,
  346. &pEnum );
  347. if(hr == WBEM_NO_ERROR)
  348. {
  349. while ( pEnum->Next( INFINITE,
  350. 1,
  351. &pPerfInst,
  352. &uReturned ) == WBEM_NO_ERROR )
  353. {
  354. //
  355. // Get the property of "NdisMediaConnectStatus"
  356. //
  357. if ( ( pPerfInst->Get( bstrPropName,
  358. 0L,
  359. &vVal,
  360. NULL, NULL ) ) != WBEM_NO_ERROR )
  361. {
  362. pPerfInst->Release( );
  363. pEnum->Release();
  364. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetConnection \
  365. failed <Get NdisMediaConnectStatus>");
  366. return FALSE;
  367. }
  368. pPerfInst->Release( );
  369. if(vVal.uintVal)
  370. {
  371. m_bLinkCable = FALSE;
  372. break;
  373. }
  374. else
  375. {
  376. continue;
  377. }
  378. }
  379. }
  380. else
  381. {
  382. TRACE(" SANetworkMonitor: CSAQueryNetInfo::GetNetConnection failed \
  383. <CreateInstanceEnum>");
  384. return FALSE;
  385. }
  386. pEnum->Release();
  387. return TRUE;
  388. }