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.

327 lines
7.0 KiB

  1. // File: NetworkAdapterConfig.h
  2. //
  3. // Synopsis: Defines a NetworkAdapterConfig
  4. // This object has the knowledge for installing
  5. // using WMI to retrieve network adapter information
  6. //
  7. // History: 02/16/2001 JeffJon Created
  8. #include "pch.h"
  9. #include "NetworkAdapterConfig.h"
  10. #include "NetworkInterface.h"
  11. #define CYS_WBEM_NETWORK_ADAPTER_CLASS L"Win32_NetworkAdapterConfiguration"
  12. #define CYS_WBEM_ADAPTER_QUERY \
  13. L"Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
  14. NetworkAdapterConfig::NetworkAdapterConfig() :
  15. initialized(false),
  16. nicCount(0)
  17. {
  18. LOG_CTOR(NetworkAdapterConfig);
  19. }
  20. NetworkAdapterConfig::~NetworkAdapterConfig()
  21. {
  22. LOG_DTOR(NetworkAdapterConfig);
  23. // Free all the NIC info from the vector and reset the count
  24. networkInterfaceVector.erase(
  25. networkInterfaceVector.begin(),
  26. networkInterfaceVector.end());
  27. nicCount = 0;
  28. }
  29. HRESULT
  30. NetworkAdapterConfig::Initialize()
  31. {
  32. LOG_FUNCTION(NetworkAdapterConfig::Initialize);
  33. HRESULT hr = S_OK;
  34. do
  35. {
  36. // Execute the query to retrieve the Adapter information
  37. SmartInterface<IEnumWbemClassObject> adapterEnum;
  38. hr = ExecQuery(
  39. CYS_WBEM_NETWORK_ADAPTER_CLASS,
  40. adapterEnum);
  41. if (FAILED(hr))
  42. {
  43. LOG(String::format(
  44. L"Failed to run query: hr = %1!x!",
  45. hr));
  46. break;
  47. }
  48. // Loop through all the adapters retrieving data
  49. ULONG itemsReturned = 0;
  50. do
  51. {
  52. itemsReturned = 0;
  53. // Get the next adapter from the query
  54. IWbemClassObject* tempAdapterObject = 0;
  55. SmartInterface<IWbemClassObject> adapterObject;
  56. hr = adapterEnum->Next(
  57. WBEM_INFINITE,
  58. 1,
  59. &tempAdapterObject,
  60. &itemsReturned);
  61. if (FAILED(hr))
  62. {
  63. LOG(String::format(
  64. L"Failed to get the next adapter: hr = %1!x!",
  65. hr));
  66. break;
  67. }
  68. adapterObject.Acquire(tempAdapterObject);
  69. if (hr == S_FALSE ||
  70. itemsReturned == 0)
  71. {
  72. // No more items
  73. break;
  74. }
  75. // Create a new network interface based on the query results
  76. NetworkInterface* newInterface = new NetworkInterface();
  77. if (!newInterface)
  78. {
  79. LOG(
  80. L"Failed to create new interface object from WMI adapter object");
  81. continue;
  82. }
  83. hr = newInterface->Initialize(adapterObject);
  84. if (FAILED(hr))
  85. {
  86. LOG(String::format(
  87. L"Failed to initialize network interface: hr = 0x%1!x!",
  88. hr));
  89. delete newInterface;
  90. continue;
  91. }
  92. // Add the new interface to the embedded container
  93. AddInterface(newInterface);
  94. } while (itemsReturned > 0);
  95. } while (false);
  96. if (SUCCEEDED(hr))
  97. {
  98. initialized = true;
  99. }
  100. LOG_HRESULT(hr);
  101. return hr;
  102. }
  103. void
  104. NetworkAdapterConfig::AddInterface(NetworkInterface* newInterface)
  105. {
  106. LOG_FUNCTION(NetworkAdapterConfig::AddInterface);
  107. do
  108. {
  109. // verify parameters
  110. if (!newInterface)
  111. {
  112. ASSERT(newInterface);
  113. break;
  114. }
  115. // Add the new NIC to the vector and increment the count
  116. networkInterfaceVector.push_back(newInterface);
  117. ++nicCount;
  118. } while (false);
  119. }
  120. unsigned int
  121. NetworkAdapterConfig::GetNICCount() const
  122. {
  123. LOG_FUNCTION(NetworkAdapterConfig::GetNICCount);
  124. ASSERT(IsInitialized());
  125. LOG(String::format(
  126. L"nicCount = %1!d!",
  127. nicCount));
  128. return nicCount;
  129. }
  130. NetworkInterface
  131. NetworkAdapterConfig::GetNIC(unsigned int nicIndex)
  132. {
  133. LOG_FUNCTION2(
  134. NetworkAdapterConfig::GetNIC,
  135. String::format(
  136. L"%1!d!",
  137. nicIndex));
  138. ASSERT(IsInitialized());
  139. ASSERT(nicIndex < GetNICCount());
  140. NetworkInterface* nic = networkInterfaceVector[nicIndex];
  141. ASSERT(nic);
  142. return *nic;
  143. }
  144. HRESULT
  145. NetworkAdapterConfig::ExecQuery(
  146. const String& classType,
  147. SmartInterface<IEnumWbemClassObject>& enumClassObject)
  148. {
  149. LOG_FUNCTION2(
  150. NetworkAdapterConfig::ExecQuery,
  151. classType);
  152. HRESULT hr = S_OK;
  153. do
  154. {
  155. // Run the query and get the enumerator
  156. SmartInterface<IWbemServices> spWbemServices;
  157. hr = GetWbemServices(spWbemServices);
  158. if (FAILED(hr))
  159. {
  160. LOG(String::format(
  161. L"Failed to get WMI COM object: hr = 0x%1!x!",
  162. hr));
  163. break;
  164. }
  165. IEnumWbemClassObject* tempEnumClassObject = 0;
  166. hr = spWbemServices->ExecQuery(
  167. AutoBstr(L"WQL"),
  168. AutoBstr(CYS_WBEM_ADAPTER_QUERY),
  169. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  170. 0,
  171. &tempEnumClassObject);
  172. if (FAILED(hr))
  173. {
  174. LOG(String::format(
  175. L"Failed to run query: hr = %1!x!",
  176. hr));
  177. break;
  178. }
  179. enumClassObject.Acquire(tempEnumClassObject);
  180. } while(false);
  181. LOG_HRESULT(hr);
  182. return hr;
  183. }
  184. HRESULT
  185. NetworkAdapterConfig::GetWbemServices(SmartInterface<IWbemServices>& spWbemServices)
  186. {
  187. LOG_FUNCTION(NetworkAdapterConfig::GetWbemServices);
  188. HRESULT hr = S_OK;
  189. if (!wbemService)
  190. {
  191. do
  192. {
  193. // First we have to initialize the security (this may be
  194. // a bug in WMI but we will do it anyway just to be safe)
  195. CoInitializeSecurity(
  196. 0,
  197. -1,
  198. 0,
  199. 0,
  200. RPC_C_AUTHN_LEVEL_DEFAULT,
  201. RPC_C_IMP_LEVEL_IMPERSONATE,
  202. 0,
  203. 0,
  204. 0);
  205. SmartInterface<IWbemLocator> wbemLocator;
  206. hr = wbemLocator.AcquireViaCreateInstance(
  207. CLSID_WbemLocator,
  208. 0,
  209. CLSCTX_INPROC_SERVER);
  210. if (FAILED(hr))
  211. {
  212. ASSERT(SUCCEEDED(hr));
  213. LOG(String::format(
  214. L"Failed to CoCreate the Wbem Locator: hr = %1!x!",
  215. hr));
  216. break;
  217. }
  218. IWbemServices* tempWbemServices = 0;
  219. hr = wbemLocator->ConnectServer(
  220. L"root\\CIMV2",
  221. 0, // user name
  222. 0, // password
  223. 0, // locale
  224. 0, // security flags, must be zero
  225. 0, // authority
  226. 0, // wbem context
  227. &tempWbemServices);
  228. if (FAILED(hr))
  229. {
  230. ASSERT(SUCCEEDED(hr));
  231. LOG(String::format(
  232. L"Failed to connect to server to get wbemService: hr = %1!x!",
  233. hr));
  234. break;
  235. }
  236. wbemService.Acquire(tempWbemServices);
  237. ASSERT(wbemService);
  238. } while(false);
  239. }
  240. spWbemServices = wbemService;
  241. LOG_HRESULT(hr);
  242. return hr;
  243. }