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.

903 lines
21 KiB

  1. // Copyright (c) Microsoft. All rights reserved.
  2. //
  3. // This is unpublished source code of Microsoft.
  4. // The copyright notice above does not evidence any
  5. // actual or intended publication of such source code.
  6. // OneLiner : Implementation of NICCard
  7. // DevUnit : wlbstest
  8. // Author : Murtaza Hakim
  9. // History:
  10. // --------
  11. //
  12. // Revised by : mhakim
  13. // Date : 02-16-01
  14. // Reason : Added code to find friendly name of nic.
  15. // include files
  16. #include "NICCard.h"
  17. #include <iostream>
  18. #include <devguid.h>
  19. #include <cfg.h>
  20. using namespace std;
  21. // constructor
  22. NICCard::NICCard( IdentifierType type,
  23. wstring id )
  24. :
  25. pnc( NULL ),
  26. nameType( type ),
  27. nicName( id ),
  28. status( NICCard_SUCCESS )
  29. {
  30. HRESULT hr;
  31. // initialize com.
  32. hr = CoInitializeEx(NULL,
  33. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  34. if( !SUCCEEDED( hr ) )
  35. {
  36. // failure to initialize com
  37. cout << "CoInitializeEx failure " << endl;
  38. // status = COM_FAILURE;
  39. // return;
  40. }
  41. // get INetCfg com interface.
  42. hr = CoCreateInstance( CLSID_CNetCfg,
  43. NULL,
  44. CLSCTX_SERVER,
  45. IID_INetCfg,
  46. (void **) &pnc );
  47. if( !SUCCEEDED( hr ) )
  48. {
  49. // failure to create instance.
  50. cout << "CoCreateInstance failure" << endl;
  51. status = COM_FAILURE;
  52. return;
  53. }
  54. }
  55. // destructor
  56. NICCard::~NICCard()
  57. {
  58. // release resources.
  59. HRESULT hr;
  60. if( pnc )
  61. {
  62. hr = pnc->Release();
  63. pnc = 0;
  64. }
  65. // uninitialize.
  66. CoUninitialize();
  67. }
  68. //+----------------------------------------------------------------------------
  69. //
  70. // Function: NICCard::isBoundTo
  71. //
  72. // Description: whether a component is bound to the adapter
  73. //
  74. // Arguments: wstring component - component id, e.g. "ms_wlbs"
  75. //
  76. // Returns: NICCard::NICCard_Error -
  77. //
  78. // History: Murtaza intitial code
  79. // fengsun Created Header 12/21/00
  80. //
  81. //+----------------------------------------------------------------------------
  82. NICCard::NICCard_Error
  83. NICCard::isBoundTo( wstring component )
  84. {
  85. HRESULT hr;
  86. NICCard_Error err;
  87. INetCfgComponent *pnccNic = NULL;
  88. INetCfgComponent *pnccComponent = NULL;
  89. INetCfgComponentBindings *pnccb = NULL;
  90. // check creation status.
  91. // if not success something went wrong during construction
  92. // and this object cannot be used.
  93. //
  94. if( status != NICCard_SUCCESS )
  95. {
  96. err = status;
  97. goto cleanup;
  98. }
  99. // Initializes network configuration by loading into
  100. // memory all basic networking information
  101. //
  102. hr = pnc->Initialize( NULL );
  103. if( !SUCCEEDED( hr ) )
  104. {
  105. // failure to Initialize
  106. cout << "INetCfg::Initialize failure " << endl;
  107. err = COM_FAILURE;
  108. goto cleanup;
  109. }
  110. // check if physical nic object of nameType, and nicName exists.
  111. //
  112. err = findNIC( nameType,
  113. nicName,
  114. &pnccNic );
  115. if( err != NICCard_SUCCESS )
  116. {
  117. // nic specified does not exist.
  118. wprintf(L"nic specified as %s does not exist\n", nicName.c_str() );
  119. // err value set by findNic
  120. goto cleanup;
  121. }
  122. if (FAILED(hr = pnc->FindComponent(component.c_str(),
  123. &pnccComponent)))
  124. {
  125. // not protocol, service or client. No such component exists.
  126. wprintf(L"component specified as %s does not exist\n", component.c_str() );
  127. err = NO_SUCH_COMPONENT;
  128. goto cleanup;
  129. }
  130. // check if binding exists.
  131. //
  132. hr = pnccComponent->QueryInterface( IID_INetCfgComponentBindings, (void **) &pnccb );
  133. if( !SUCCEEDED( hr ) )
  134. {
  135. cout << "INetCfgComponent::QueryInterface failed " << endl;
  136. err = COM_FAILURE;
  137. goto cleanup;
  138. }
  139. hr = pnccb->IsBoundTo( pnccNic );
  140. if( hr == S_OK )
  141. {
  142. err = BOUND;
  143. }
  144. else if( hr == S_FALSE )
  145. {
  146. err = UNBOUND;
  147. }
  148. else
  149. {
  150. err = COM_FAILURE;
  151. }
  152. goto cleanup;
  153. // free up com resources.
  154. cleanup:
  155. if( pnccb )
  156. {
  157. pnccb->Release();
  158. pnccb = 0;
  159. }
  160. if( pnccComponent )
  161. {
  162. pnccComponent->Release();
  163. pnccComponent = 0;
  164. }
  165. if( pnccNic )
  166. {
  167. pnccNic->Release();
  168. pnccNic = 0;
  169. }
  170. pnc->Uninitialize();
  171. return err;
  172. }
  173. //+----------------------------------------------------------------------------
  174. //
  175. // Function: NICCard::bind
  176. //
  177. // Description: bind a component to the adapter
  178. //
  179. // Arguments: wstring component - component id, e.g. "ms_wlbs"
  180. //
  181. // Returns: NICCard::NICCard_Error -
  182. //
  183. // History: Murtaza intitial code
  184. // fengsun Created Header 12/21/00
  185. //
  186. //+----------------------------------------------------------------------------
  187. NICCard::NICCard_Error
  188. NICCard::bind( wstring component )
  189. {
  190. NICCard_Error err;
  191. err = isBoundTo( component );
  192. if( err == UNBOUND )
  193. {
  194. return toggleState( component );
  195. }
  196. else if( err == BOUND )
  197. {
  198. return NICCard_SUCCESS;
  199. }
  200. else
  201. {
  202. return err;
  203. }
  204. }
  205. //+----------------------------------------------------------------------------
  206. //
  207. // Function: NICCard::unbind
  208. //
  209. // Description: unbind a component to the adapter
  210. //
  211. // Arguments: wstring component - component id, e.g. "ms_wlbs"
  212. //
  213. // Returns: NICCard::NICCard_Error -
  214. //
  215. // History: Murtaza intitial code
  216. // fengsun Created Header 12/21/00
  217. //
  218. //+----------------------------------------------------------------------------
  219. NICCard::NICCard_Error
  220. NICCard::unbind( wstring component )
  221. {
  222. NICCard_Error err;
  223. err = isBoundTo( component );
  224. if( err == BOUND )
  225. {
  226. return toggleState( component );
  227. }
  228. else if( err == UNBOUND )
  229. {
  230. return NICCard_SUCCESS;
  231. }
  232. else
  233. {
  234. return err;
  235. }
  236. }
  237. // private
  238. // returns true if found, else false.
  239. NICCard::NICCard_Error
  240. NICCard::findNIC( IdentifierType type,
  241. wstring nicName,
  242. INetCfgComponent** ppnccNic )
  243. {
  244. HRESULT hr;
  245. IEnumNetCfgComponent* pencc;
  246. INetCfgComponent* pncc;
  247. ULONG countToFetch = 1;
  248. ULONG countFetched;
  249. DWORD characteristics;
  250. wstring name;
  251. LPWSTR pName;
  252. hr = pnc->EnumComponents( &GUID_DEVCLASS_NET,
  253. &pencc );
  254. if( !SUCCEEDED( hr ) )
  255. {
  256. // failure to Enumerate net components
  257. cout << "INetCfg::EnumComponents failure " << endl;
  258. return COM_FAILURE;
  259. }
  260. while( ( hr = pencc->Next( countToFetch, &pncc, &countFetched ) )== S_OK )
  261. {
  262. // here we have been given guid.
  263. if( type == guid )
  264. {
  265. hr = pncc->GetBindName( &pName );
  266. }
  267. else
  268. {
  269. hr = pncc->GetDisplayName( &pName );
  270. }
  271. name = pName;
  272. CoTaskMemFree( pName );
  273. if( name == nicName )
  274. {
  275. *ppnccNic = pncc;
  276. if( pencc )
  277. {
  278. pencc->Release();
  279. pencc = 0;
  280. }
  281. return NICCard_SUCCESS;
  282. }
  283. pncc->Release();
  284. }
  285. if( pencc )
  286. {
  287. pencc->Release();
  288. pencc = 0;
  289. }
  290. *ppnccNic = NULL;
  291. return NO_SUCH_NIC;
  292. }
  293. //+----------------------------------------------------------------------------
  294. //
  295. // Function: NICCard::toggleState
  296. //
  297. // Description: toggle a component binding state to the adapter
  298. //
  299. // Arguments: wstring component - component id, e.g. "ms_wlbs"
  300. //
  301. // Returns: NICCard::NICCard_Error -
  302. //
  303. // History: Murtaza intitial code
  304. // fengsun Created Header 12/21/00
  305. //
  306. //+----------------------------------------------------------------------------
  307. NICCard::NICCard_Error
  308. NICCard::toggleState( wstring component )
  309. {
  310. HRESULT hr;
  311. NICCard_Error err;
  312. INetCfgComponent *pnccNic = NULL;
  313. INetCfgComponent *pnccComponent = NULL;
  314. INetCfgComponentBindings *pnccb = NULL;
  315. INetCfgLock *pncl = NULL;
  316. LPWSTR presentLockHolder = new wchar_t[1000];
  317. // check creation status.
  318. // if not success something went wrong during construction
  319. // and this object cannot be used.
  320. //
  321. if( status != NICCard_SUCCESS )
  322. {
  323. err = status;
  324. goto cleanup;
  325. }
  326. // as this operation can make modifications we require a lock.
  327. // thus get lock.
  328. //
  329. hr = pnc->QueryInterface( IID_INetCfgLock, ( void **) &pncl );
  330. if( !SUCCEEDED( hr ) )
  331. {
  332. cout << "INetCfg QueryInterface for IID_INetCfgLock failed " << endl;
  333. err = COM_FAILURE;
  334. goto cleanup;
  335. }
  336. hr = pncl->AcquireWriteLock( TIME_TO_WAIT,
  337. L"NLBManager",
  338. &presentLockHolder );
  339. if( hr != S_OK )
  340. {
  341. cout << "INetCfgLock::AcquireWriteLock failure is " << hr << endl;
  342. err = COM_FAILURE;
  343. goto cleanup;
  344. }
  345. // Initializes network configuration by loading into
  346. // memory all basic networking information
  347. //
  348. hr = pnc->Initialize( NULL );
  349. if( !SUCCEEDED( hr ) )
  350. {
  351. // failure to Initialize
  352. cout << "INetCfg::Initialize failed with " << hr << endl;
  353. err = COM_FAILURE;
  354. goto cleanup;
  355. }
  356. // check if physical nic object of nameType, and nicName exists.
  357. //
  358. err = findNIC( nameType,
  359. nicName,
  360. &pnccNic );
  361. if( err != NICCard_SUCCESS )
  362. {
  363. // nic specified does not exist.
  364. wprintf(L"nic specified as %s does not exist\n", nicName.c_str() );
  365. goto cleanup;
  366. }
  367. if (FAILED(hr = pnc->FindComponent(component.c_str(),
  368. &pnccComponent)))
  369. {
  370. // not protocol, service or client. No such component exists.
  371. wprintf(L"component specified as %s does not exist\n", component.c_str() );
  372. err = NO_SUCH_COMPONENT;
  373. goto cleanup;
  374. }
  375. // check if binding exists.
  376. //
  377. hr = pnccComponent->QueryInterface( IID_INetCfgComponentBindings, (void **) &pnccb );
  378. if( !SUCCEEDED( hr ) )
  379. {
  380. cout << "INetCfgComponent::QueryInterface failed " << endl;
  381. err = COM_FAILURE;
  382. goto cleanup;
  383. }
  384. hr = pnccb->IsBoundTo( pnccNic );
  385. if( hr == S_OK )
  386. {
  387. hr = pnccb->UnbindFrom( pnccNic );
  388. if( !SUCCEEDED( hr ) )
  389. {
  390. if( hr == NETCFG_E_NO_WRITE_LOCK )
  391. cout << "Unable to obtain write lock. Please verify properties page not already open" << endl;
  392. else
  393. cout << "INetCfgBindings::UnbindFrom failed with " << hr << endl;
  394. err = COM_FAILURE;
  395. }
  396. else
  397. {
  398. // apply the binding change made.
  399. hr = pnc->Apply();
  400. if( !SUCCEEDED( hr ) )
  401. {
  402. cout << "INetCfg::Apply failed with " << hr << endl;
  403. err = COM_FAILURE;
  404. }
  405. else
  406. {
  407. err = NICCard_SUCCESS;
  408. }
  409. }
  410. }
  411. else if( hr == S_FALSE )
  412. {
  413. hr = pnccb->BindTo( pnccNic );
  414. if( !SUCCEEDED( hr ) )
  415. {
  416. if( hr == NETCFG_E_NO_WRITE_LOCK )
  417. cout << "Unable to obtain write lock. Please verify properties page not already open" << endl;
  418. else
  419. cout << "INetCfgBindings::BindTo failed with " << hr << endl;
  420. err = COM_FAILURE;
  421. }
  422. else
  423. {
  424. // apply the binding change made.
  425. hr = pnc->Apply();
  426. if( !SUCCEEDED( hr ) )
  427. {
  428. cout << "INetCfg::Apply failed with " << hr << endl;
  429. err = COM_FAILURE;
  430. }
  431. else
  432. {
  433. err = NICCard_SUCCESS;
  434. }
  435. }
  436. }
  437. else
  438. {
  439. cout << "INetCfgComponentBindings::IsBoundTo failed with " << hr << endl;
  440. err = COM_FAILURE;
  441. }
  442. goto cleanup;
  443. // free up com resources.
  444. cleanup:
  445. if( pnccb )
  446. {
  447. pnccb->Release();
  448. pnccb = 0;
  449. }
  450. if( pnccComponent )
  451. {
  452. pnccComponent->Release();
  453. pnccComponent = 0;
  454. }
  455. if( pnccNic )
  456. {
  457. pnccNic->Release();
  458. pnccNic = 0;
  459. }
  460. if( pnc )
  461. pnc->Uninitialize();
  462. if( pncl )
  463. {
  464. pncl->ReleaseWriteLock();
  465. pncl->Release();
  466. pncl = 0;
  467. }
  468. delete [] presentLockHolder;
  469. return err;
  470. }
  471. // getNics
  472. //
  473. NICCard::NICCard_Error
  474. NICCard::getNics( vector<NICCard::Info>* nicList )
  475. {
  476. HRESULT hr;
  477. INetCfg *pncStatic = 0;
  478. IEnumNetCfgComponent *pencc = 0;
  479. INetCfgComponent *pncc = 0;
  480. wstring name;
  481. LPWSTR pName;
  482. NICCard_Error err;
  483. ULONG countToFetch = 1;
  484. ULONG countFetched;
  485. ULONG status;
  486. Info info;
  487. DWORD characteristics = 0;
  488. // initialize com.
  489. hr = CoInitializeEx(NULL,
  490. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  491. if( !SUCCEEDED( hr ) )
  492. {
  493. // failure to initialize com
  494. cout << "CoInitializeEx failure " << endl;
  495. // err = COM_FAILURE;
  496. // goto cleanup;
  497. }
  498. // get INetCfg com interface.
  499. hr = CoCreateInstance( CLSID_CNetCfg,
  500. NULL,
  501. CLSCTX_SERVER,
  502. IID_INetCfg,
  503. (void **) &pncStatic );
  504. if( !SUCCEEDED( hr ) )
  505. {
  506. // failure to create instance.
  507. cout << "CoCreateInstance failure" << endl;
  508. err = COM_FAILURE;
  509. goto cleanup;
  510. }
  511. // Initializes network configuration by loading into
  512. // memory all basic networking information
  513. //
  514. hr = pncStatic->Initialize( NULL );
  515. if( !SUCCEEDED( hr ) )
  516. {
  517. // failure to Initialize
  518. cout << "INetCfg::Initialize failure " << endl;
  519. err = COM_FAILURE;
  520. goto cleanup;
  521. }
  522. hr = pncStatic->EnumComponents( &GUID_DEVCLASS_NET,
  523. &pencc );
  524. if( !SUCCEEDED( hr ) )
  525. {
  526. // failure to Enumerate net components
  527. cout << "INetCfg::EnumComponents failure " << endl;
  528. err = COM_FAILURE;
  529. goto cleanup;
  530. }
  531. while( ( hr = pencc->Next( countToFetch, &pncc, &countFetched ) )== S_OK )
  532. {
  533. hr = pncc->GetBindName( &pName );
  534. if( !SUCCEEDED( hr ) )
  535. {
  536. cout << "INetCfgComponent::GetBindName failure " << endl;
  537. err = COM_FAILURE;
  538. goto cleanup;
  539. }
  540. info.guid = pName;
  541. CoTaskMemFree( pName );
  542. hr = pncc->GetDisplayName( &pName );
  543. if( !SUCCEEDED( hr ) )
  544. {
  545. cout << "INetCfgComponent::GetDisplayName failure " << endl;
  546. err = COM_FAILURE;
  547. goto cleanup;
  548. }
  549. info.fullName = pName;
  550. CoTaskMemFree( pName );
  551. hr = pncc->GetCharacteristics( &characteristics );
  552. if( !SUCCEEDED( hr ) )
  553. {
  554. cout << "INetCfgComponent::GetCharacteristics failure " << endl;
  555. err = COM_FAILURE;
  556. goto cleanup;
  557. }
  558. GetFriendlyNICName( info.guid, info.friendlyName );
  559. if( characteristics & NCF_PHYSICAL )
  560. {
  561. // this is a physical network card.
  562. // we are only interested in such devices
  563. // check if the nic is enabled, we are only
  564. // interested in enabled nics.
  565. //
  566. hr = pncc->GetDeviceStatus( &status );
  567. if( !SUCCEEDED( hr ) )
  568. {
  569. cout << "INetCfgComponent::GetDeviceStatus failure " << endl;
  570. err = COM_FAILURE;
  571. goto cleanup;
  572. }
  573. // if any of the nics has any of the problem codes
  574. // then it cannot be used.
  575. if( status != CM_PROB_NOT_CONFIGURED
  576. &&
  577. status != CM_PROB_FAILED_START
  578. &&
  579. status != CM_PROB_NORMAL_CONFLICT
  580. &&
  581. status != CM_PROB_NEED_RESTART
  582. &&
  583. status != CM_PROB_REINSTALL
  584. &&
  585. status != CM_PROB_WILL_BE_REMOVED
  586. &&
  587. status != CM_PROB_DISABLED
  588. &&
  589. status != CM_PROB_FAILED_INSTALL
  590. &&
  591. status != CM_PROB_FAILED_ADD
  592. )
  593. {
  594. // no problem with this nic and also
  595. // physical device
  596. // thus we want it.
  597. nicList->push_back( info );
  598. }
  599. }
  600. if( pncc )
  601. {
  602. pncc->Release();
  603. pncc = 0;
  604. }
  605. characteristics = 0;
  606. countToFetch = 1;
  607. }
  608. err = NICCard_SUCCESS;
  609. cleanup:
  610. if( pncStatic )
  611. {
  612. pncStatic->Uninitialize();
  613. pncStatic->Release();
  614. pncStatic = 0;
  615. }
  616. if( pencc )
  617. {
  618. pencc->Release();
  619. pencc = 0;
  620. }
  621. if( pncc )
  622. {
  623. pncc->Release();
  624. pncc = 0;
  625. }
  626. return err;
  627. }
  628. NICCard::NICCard_Error
  629. NICCard::isNetCfgAvailable()
  630. {
  631. NICCard_Error err;
  632. HRESULT hr;
  633. INetCfgLock *pncl = NULL;
  634. LPWSTR presentLockHolder = new wchar_t[1000];
  635. hr = pnc->QueryInterface( IID_INetCfgLock, ( void **) &pncl );
  636. if( !SUCCEEDED( hr ) )
  637. {
  638. cout << "INetCfg QueryInterface for IID_INetCfgLock failed " << endl;
  639. err = COM_FAILURE;
  640. goto cleanup;
  641. }
  642. hr = pncl->AcquireWriteLock( TIME_TO_WAIT,
  643. L"NLBManager",
  644. &presentLockHolder );
  645. if( hr != S_OK )
  646. {
  647. cout << "INetCfgLock::AcquireWriteLock failure is " << hr << endl;
  648. err = COM_FAILURE;
  649. goto cleanup;
  650. }
  651. err = NICCard_SUCCESS;
  652. cleanup:
  653. if( pncl )
  654. {
  655. pncl->ReleaseWriteLock();
  656. pncl->Release();
  657. pncl = 0;
  658. }
  659. delete [] presentLockHolder;
  660. return err;
  661. }
  662. // code to find friendly name of nic.
  663. // this code is modified for wstring, but
  664. // otherwise is courtesy of hengz.
  665. //
  666. int
  667. NICCard::GetFriendlyNICName(const wstring& guid, wstring& name )
  668. {
  669. // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network
  670. HKEY hregkey=NULL, hkOpenKey=NULL, hkQueryKey=NULL;
  671. DWORD dwDataBuffer=200;
  672. DWORD dwValueType=REG_SZ;
  673. wchar_t data[200], path[200], adapters[200];
  674. wchar_t *pHost=NULL;
  675. int num=0, ret;
  676. FILETIME time;
  677. //make connection to the machine's registry
  678. if ((ret=RegConnectRegistry(pHost, HKEY_LOCAL_MACHINE, &hregkey)) != ERROR_SUCCESS) {
  679. // TRACE( MTrace::SEVERE_ERROR, L"(RegConnectRegistry) failure" );
  680. return (-1);
  681. }
  682. //look for the GUID
  683. if( (ret=RegOpenKeyEx(hregkey, L"SYSTEM\\CurrentControlSet\\Control\\Network", 0, KEY_READ, &hkOpenKey)) != ERROR_SUCCESS ) {
  684. RegCloseKey(hregkey);
  685. // TRACE( MTrace::SEVERE_ERROR, L"(RegOpenKeyEx) failed to open HKLM\\SYSTEM\\CurrentControlSet\\Control\\Network" );
  686. return (-1);
  687. }
  688. while (1) {
  689. adapters[0]=L'\0';
  690. dwDataBuffer=200;
  691. if( ((ret=RegEnumKeyEx(hkOpenKey, num, adapters, &dwDataBuffer, NULL,NULL,NULL, &time)) != ERROR_SUCCESS)
  692. && (ret!=ERROR_MORE_DATA) )
  693. {
  694. if (ret==ERROR_NO_MORE_ITEMS)
  695. {
  696. // TRACE(MTrace::SEVERE_ERROR,L"(RegEnumKeyEx): failed to find network adapters in HKLM\\SYSTEM\\CurrentControlSet\\Control\\Network");
  697. }
  698. else
  699. {
  700. // TRACE(MTrace::SEVERE_ERROR,L"(RegEnumKeyEx): fail to enum HKLM\\SYSTEM\\CurrentControlSet\\Control\\Network" );
  701. }
  702. RegCloseKey(hkOpenKey);
  703. RegCloseKey(hregkey);
  704. return (-1);
  705. }
  706. //open the items one by one
  707. swprintf(path, L"SYSTEM\\CurrentControlSet\\Control\\Network\\%s", adapters);
  708. if( (ret=RegOpenKeyEx(hregkey, path, 0, KEY_READ, &hkQueryKey)) != ERROR_SUCCESS ) {
  709. num++;
  710. continue;
  711. }
  712. dwDataBuffer=200;
  713. data[0]=L'\0';
  714. if((ret=RegQueryValueEx(hkQueryKey, L"", 0, &dwValueType, (LPBYTE)data, &dwDataBuffer)) != ERROR_SUCCESS) {
  715. num++;
  716. continue;
  717. }
  718. RegCloseKey(hkQueryKey);
  719. num++;
  720. if (wcscmp(L"Network Adapters", data)==0)
  721. break;
  722. }
  723. RegCloseKey(hkOpenKey);
  724. //found the guid now
  725. //look for friendly nic name
  726. swprintf(path, L"SYSTEM\\CurrentControlSet\\Control\\Network\\%s\\%s\\Connection", adapters, guid.c_str() );
  727. if( (ret=RegOpenKeyEx(hregkey, path, 0, KEY_READ, &hkOpenKey)) != ERROR_SUCCESS ) {
  728. RegCloseKey(hregkey);
  729. // TRACE(MTrace::SEVERE_ERROR, L"(RegOpenKeyEx) fail to open " + wstring( path ) );
  730. return (-1);
  731. }
  732. dwDataBuffer=200;
  733. data[0]=L'\0';
  734. if((ret=RegQueryValueEx(hkOpenKey, L"Name", 0, &dwValueType, (LPBYTE)data, &dwDataBuffer)) != ERROR_SUCCESS) {
  735. // TRACE(MTrace::SEVERE_ERROR, L"(RegQueryValueEx) fail to query name " + wstring (path ) );
  736. RegCloseKey(hkOpenKey);
  737. RegCloseKey(hregkey);
  738. return (-1);
  739. }
  740. RegCloseKey(hkOpenKey);
  741. RegCloseKey(hregkey);
  742. name = data;
  743. return 0;
  744. }