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.

387 lines
8.8 KiB

  1. // NicName.cpp : Implementation of CNicName
  2. #include "stdafx.h"
  3. #include "MSSANic.h"
  4. #include "NicName.h"
  5. #include "Tracing.h"
  6. //
  7. // Constant data
  8. //
  9. const WCHAR REGKEY_NETWORK[] = L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
  10. const DWORD MAX_REGKEY_VALUE = 1024;
  11. //
  12. // Private data structures
  13. //
  14. struct RegData {
  15. union {
  16. WCHAR wstrValue[MAX_REGKEY_VALUE];
  17. DWORD dwValue;
  18. }Contents;
  19. };
  20. //
  21. // Private non-member functions
  22. //
  23. static bool FindNICAdaptersRegKey(wstring& wszNicAdaptersRegKey);
  24. //+-----------------------------------------------------------------------
  25. //
  26. // Method: Constructor
  27. //
  28. // Synopsis: Construct the CNicName object
  29. //
  30. // History: JKountz 08/19/2000 Created
  31. //+-----------------------------------------------------------------------
  32. CNicName::CNicName()
  33. {
  34. //
  35. // Load the user friendly Nic information from
  36. // the registry.
  37. //
  38. LoadNicInfo();
  39. }
  40. //+-----------------------------------------------------------------------
  41. //
  42. // Method: Get
  43. //
  44. // Synopsis: Get the user friendly name for the specified Nic card.
  45. //
  46. // Arguments: IN bstrPnpDeviceID Plug and Play Device ID for the Nic
  47. // card we are lookup up.
  48. //
  49. // OUT bstrName Receives the user friendly name for
  50. // the specified Nic.
  51. //
  52. // History: JKountz 08/19/2000 Created
  53. //+-----------------------------------------------------------------------
  54. STDMETHODIMP CNicName::Get(BSTR bstrPnpDeviceID, BSTR *pbstrName)
  55. {
  56. try
  57. {
  58. bool bFound = false;
  59. vector<CNicInfo>::iterator it;
  60. wstring wstrPNPDeviceID(bstrPnpDeviceID);
  61. //
  62. // Search the list of NIC
  63. //
  64. for(it = m_vNicInfo.begin(); it != m_vNicInfo.end(); it++)
  65. {
  66. //
  67. // Does the PnP Device ID match?
  68. //
  69. if ( 0 == lstrcmpi( wstrPNPDeviceID.c_str(),
  70. (*it).m_wstrPNPDeviceID.c_str()))
  71. {
  72. *pbstrName = ::SysAllocString((*it).m_wstrName.c_str());
  73. bFound = true;
  74. }
  75. }
  76. //
  77. // Provide a reasonable alternative if not match was found
  78. //
  79. if ( !bFound )
  80. {
  81. //
  82. // BUGBUG: Probably should localize this
  83. //
  84. *pbstrName = ::SysAllocString(L"Local Network Connection");
  85. }
  86. }
  87. catch(...)
  88. {
  89. }
  90. return S_OK;
  91. }
  92. //+-----------------------------------------------------------------------
  93. //
  94. // Method: Set
  95. //
  96. // Synopsis: Set the user friendly name for the specified Nic card.
  97. //
  98. // Arguments: IN bstrPnpDeviceID Plug and Play Device ID for the Nic
  99. // card we are lookup up.
  100. //
  101. // IN bstrName The user friendly name for the
  102. // specified Nic.
  103. //
  104. // History: JKountz 08/19/2000 Created
  105. //+-----------------------------------------------------------------------
  106. STDMETHODIMP CNicName::Set(BSTR bstrPnpDeviceID, BSTR bstrName)
  107. {
  108. //
  109. // Default return code is invalid PNP Device ID
  110. //
  111. HRESULT hr = E_INVALIDARG;
  112. try
  113. {
  114. vector<CNicInfo>::iterator it;
  115. wstring wstrPNPDeviceID(bstrPnpDeviceID);
  116. //
  117. // Search the list of NIC
  118. //
  119. for(it = m_vNicInfo.begin(); it != m_vNicInfo.end(); it++)
  120. {
  121. //
  122. // Does the PnP Device ID match?
  123. //
  124. if ( 0 == lstrcmpi( wstrPNPDeviceID.c_str(),
  125. (*it).m_wstrPNPDeviceID.c_str()))
  126. {
  127. (*it).m_wstrName = bstrName;
  128. if ( ERROR_SUCCESS == Store(*it))
  129. {
  130. hr = S_OK;
  131. }
  132. }
  133. }
  134. }
  135. catch(...)
  136. {
  137. }
  138. return hr;
  139. }
  140. //+-----------------------------------------------------------------------
  141. //
  142. // Method: LoadNicInfo
  143. //
  144. // Synopsis: Preload the Nic information. We enumerate through the
  145. // registry looking for all Nic's. For each Nic we create
  146. // an instance of CNicInfo and store it in a vector
  147. // class variable. See CNicInfo for more information.
  148. //
  149. // History: JKountz 08/19/2000 Created
  150. //+-----------------------------------------------------------------------
  151. void CNicName::LoadNicInfo()
  152. {
  153. //
  154. // Clear the list of Nic's
  155. //
  156. m_vNicInfo.clear();
  157. //
  158. // Locate the Network Adapters REG key. All the Nic's
  159. // are listed under this key.
  160. //
  161. HKEY hkNicAdapters;
  162. wstring wstrNicAdaptersRegKey(REGKEY_NETWORK);
  163. //
  164. // Open the Network Adapters REG key
  165. //
  166. if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,
  167. wstrNicAdaptersRegKey.c_str(), &hkNicAdapters ))
  168. {
  169. //
  170. // Enumerate all the Nic's
  171. //
  172. WCHAR wszName[1024];
  173. DWORD dwNicAdapterIndex = 0;
  174. while ( ERROR_SUCCESS == RegEnumKey( hkNicAdapters, dwNicAdapterIndex, wszName, (sizeof wszName)/(sizeof wszName[0])))
  175. {
  176. HKEY hkNics;
  177. DWORD dwNicIndex = 0;
  178. wstring wstrNics(wstrNicAdaptersRegKey);
  179. wstrNics.append(L"\\");
  180. wstrNics.append(wszName);
  181. wstrNics.append(L"\\Connection");
  182. //
  183. // Open the Connection sub key. This is where the Pnp Device ID
  184. // and user friendly name are stored.
  185. //
  186. if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,
  187. wstrNics.c_str(), &hkNics ))
  188. {
  189. DWORD dwRegType;
  190. RegData regData;
  191. DWORD dwSizeOfRegType = sizeof(regData);
  192. DWORD dwSizeOfName = (sizeof wszName)/(sizeof wszName[0]);
  193. CNicInfo nicInfo;
  194. nicInfo.m_wstrRegKey = wstrNics;
  195. DWORD dwNicAttributes = 0;
  196. //
  197. // Enumerate all the values under Connection.
  198. // We are looking for PNPDeviceID and Name which
  199. // are both REG_SZ types.
  200. //
  201. while ( ERROR_SUCCESS == RegEnumValue( hkNics,
  202. dwNicIndex,
  203. wszName,
  204. &dwSizeOfName,
  205. 0,
  206. &dwRegType,
  207. (BYTE*)&regData,
  208. &dwSizeOfRegType))
  209. {
  210. if ( dwRegType == REG_SZ )
  211. {
  212. //
  213. // Found the PNP Device ID
  214. //
  215. if ( lstrcmpi(L"PnpInstanceID", wszName) == 0 )
  216. {
  217. nicInfo.m_wstrPNPDeviceID = regData.Contents.wstrValue;
  218. dwNicAttributes++;
  219. }
  220. //
  221. // Found the user friendly name
  222. //
  223. else if ( lstrcmpi(L"Name", wszName) == 0 )
  224. {
  225. nicInfo.m_wstrName = regData.Contents.wstrValue;
  226. dwNicAttributes++;
  227. }
  228. }
  229. dwNicIndex++;
  230. dwSizeOfRegType = sizeof(regData);
  231. }
  232. //
  233. // Did we find both the Pnp Device ID and user friendly name?
  234. //
  235. if ( dwNicAttributes >= 2 )
  236. {
  237. // Save them
  238. m_vNicInfo.push_back(nicInfo);
  239. }
  240. RegCloseKey( hkNics );
  241. }
  242. dwNicAdapterIndex++;
  243. } // while RegEnumKey ( hkNicAdapters..)
  244. RegCloseKey(hkNicAdapters);
  245. }
  246. return;
  247. }
  248. //+-----------------------------------------------------------------------
  249. //
  250. // Method: FindNICAdaptersRegKey
  251. //
  252. // Synopsis: Locate the Network Adapters REG key. All the Nic info
  253. // we need is stored under this key. It is located
  254. // below System\CurrentControlSet\Control\Network
  255. //
  256. // History: JKountz 08/19/2000 Created
  257. //+-----------------------------------------------------------------------
  258. static bool FindNICAdaptersRegKey(wstring& wszNicAdaptersRegKey)
  259. {
  260. HKEY hk;
  261. bool bRc = false;
  262. if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE, REGKEY_NETWORK, &hk ))
  263. {
  264. DWORD dwIndex = 0;
  265. WCHAR wszName[1024];
  266. while ( ERROR_SUCCESS == RegEnumKey( hk, dwIndex, wszName, (sizeof wszName)/(sizeof wszName[0])))
  267. {
  268. WCHAR wszValue[1024];
  269. LONG lSizeOfValue = sizeof(wszValue);
  270. //
  271. // Check the value of this key, we need a value of: Network Adapters
  272. //
  273. if ( ERROR_SUCCESS == RegQueryValue( hk, wszName, wszValue, &lSizeOfValue)
  274. && lstrcmpi(L"Network Adapters", wszValue) == 0 )
  275. {
  276. //
  277. // Found it
  278. //
  279. wstring wstrNicAdapters(REGKEY_NETWORK);
  280. wstrNicAdapters.append(L"\\");
  281. wstrNicAdapters.append(wszName);
  282. wszNicAdaptersRegKey = wstrNicAdapters;
  283. bRc = true;
  284. }
  285. //
  286. // Next enumeration element
  287. dwIndex++;
  288. }
  289. RegCloseKey(hk);
  290. }
  291. return bRc;
  292. }
  293. //+-----------------------------------------------------------------------
  294. //
  295. // Method: Store
  296. //
  297. // Synopsis: Store changes to the user friendly Nic name.
  298. //
  299. // Arguments: IN CNicInfo which contains the changed state
  300. // that needs to be stored. We use the m_wstrRegKey
  301. // member of CNicInfo to locate the Nic card that
  302. // needs to be updated.
  303. //
  304. // History: JKountz 08/19/2000 Created
  305. //+-----------------------------------------------------------------------
  306. DWORD CNicName::Store(CNicInfo &rNicInfo)
  307. {
  308. DWORD dwRc;
  309. HKEY hkNic;
  310. dwRc = RegOpenKey( HKEY_LOCAL_MACHINE,
  311. rNicInfo.m_wstrRegKey.c_str(), &hkNic );
  312. if ( ERROR_SUCCESS == dwRc)
  313. {
  314. DWORD dwNameLen;
  315. dwNameLen = sizeof(WCHAR)*(lstrlen(rNicInfo.m_wstrName.c_str()) + 1);
  316. dwRc = RegSetValueEx(hkNic,
  317. L"Name",
  318. 0,
  319. REG_SZ,
  320. (BYTE*)(rNicInfo.m_wstrName.c_str()),
  321. dwNameLen);
  322. RegCloseKey(hkNic);
  323. }
  324. if ( ERROR_SUCCESS != dwRc )
  325. {
  326. SATraceFailure( "CNicName::Store", dwRc );
  327. }
  328. return dwRc;
  329. }