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.

166 lines
4.9 KiB

  1. // NicIterator.cpp: implementation of the CNicIterator class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "NicIterator.h"
  6. #include "Tracing.h"
  7. //
  8. // Constant data
  9. //
  10. WCHAR REGKEY_NETWORK[] = L"System\\CurrentControlSet\\Control\\Network";
  11. //
  12. // Private data structures
  13. //
  14. struct RegData {
  15. union {
  16. WCHAR wstrValue[1024];
  17. DWORD dwValue;
  18. }Contents;
  19. };
  20. //
  21. // Private non-member functions
  22. //
  23. static bool FindNICAdaptersRegKey(wstring& wszNicAdaptersRegKey);
  24. void CNicIterator::LoadNicInfo()
  25. {
  26. USES_CONVERSION;
  27. m_vNicInfo.clear();
  28. HKEY hkNicAdapters;
  29. wstring wstrNicAdaptersRegKey;
  30. if ( FindNICAdaptersRegKey( wstrNicAdaptersRegKey ))
  31. {
  32. if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,
  33. wstrNicAdaptersRegKey.c_str(), &hkNicAdapters ))
  34. {
  35. WCHAR wszName[1024];
  36. DWORD dwNicAdapterIndex = 0;
  37. while ( ERROR_SUCCESS == RegEnumKey( hkNicAdapters, dwNicAdapterIndex, wszName, sizeof(wszName)))
  38. {
  39. SATraceString(W2A(wszName));
  40. HKEY hkNics;
  41. DWORD dwNicIndex = 0;
  42. wstring wstrNics(wstrNicAdaptersRegKey);
  43. wstrNics.append(L"\\");
  44. wstrNics.append(wszName);
  45. wstrNics.append(L"\\Connection");
  46. SATraceString( W2A(wstrNics.c_str()) );
  47. if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,
  48. wstrNics.c_str(), &hkNics ))
  49. {
  50. DWORD dwRegType;
  51. RegData regData;
  52. DWORD dwSizeOfRegType = sizeof(regData);
  53. DWORD dwSizeOfName = sizeof(wszName);
  54. SATraceString("Enumerating values");
  55. CNicInfo nicInfo;
  56. nicInfo.wstrRegKey = wstrNics;
  57. DWORD dwNicAttributes = 0;
  58. while ( ERROR_SUCCESS == RegEnumValue( hkNics,
  59. dwNicIndex,
  60. wszName,
  61. &dwSizeOfName,
  62. 0,
  63. &dwRegType,
  64. (BYTE*)&regData,
  65. &dwSizeOfRegType))
  66. {
  67. if ( dwRegType == REG_SZ )
  68. {
  69. if ( lstrcmpi(L"PnpInstanceID", wszName) == 0 )
  70. {
  71. nicInfo.wstrPNPInstanceID = regData.Contents.wstrValue;
  72. dwNicAttributes++;
  73. }
  74. else if ( lstrcmpi(L"Name", wszName) == 0 )
  75. {
  76. nicInfo.wstrName = regData.Contents.wstrValue;
  77. dwNicAttributes++;
  78. }
  79. }
  80. dwNicIndex++;
  81. }
  82. if ( dwNicAttributes >= 2 )
  83. {
  84. m_vNicInfo.push_back(nicInfo);
  85. }
  86. RegCloseKey( hkNics );
  87. }
  88. dwNicAdapterIndex++;
  89. } // while RegEnumKey ( hkNicAdapters..)
  90. RegCloseKey(hkNicAdapters);
  91. }
  92. }
  93. return;
  94. }
  95. static bool FindNICAdaptersRegKey(wstring& wszNicAdaptersRegKey)
  96. {
  97. HKEY hk;
  98. bool bRc = false;
  99. if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE, REGKEY_NETWORK, &hk ))
  100. {
  101. DWORD dwIndex = 0;
  102. WCHAR wszName[1024];
  103. while ( ERROR_SUCCESS == RegEnumKey( hk, dwIndex, wszName, sizeof(wszName)))
  104. {
  105. WCHAR wszValue[1024];
  106. LONG lSizeOfValue = sizeof(wszValue);
  107. //
  108. // Check the value of this key
  109. //
  110. if ( ERROR_SUCCESS == RegQueryValue( hk, wszName, wszValue, &lSizeOfValue)
  111. && lstrcmpi(L"Network Adapters", wszValue) == 0 )
  112. {
  113. //
  114. // Found the Network Adapters reg key
  115. //
  116. wstring wstrNicAdapters(REGKEY_NETWORK);
  117. wstrNicAdapters.append(L"\\");
  118. wstrNicAdapters.append(wszName);
  119. wszNicAdaptersRegKey = wstrNicAdapters;
  120. bRc = true;
  121. }
  122. //
  123. // Next enumeration element
  124. dwIndex++;
  125. }
  126. RegCloseKey(hk);
  127. }
  128. return bRc;
  129. }