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.

614 lines
18 KiB

  1. //-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: A R P S F U N C . C P P
  7. //
  8. // Contents: CArpsCfg help member function implementation
  9. //
  10. // Notes:
  11. //
  12. // Author: tongl 12 Mar 1997
  13. //
  14. //-----------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "arpsobj.h"
  18. #include "arpsdlg.h"
  19. #include "atmutil.h"
  20. #include "ncreg.h"
  21. #include "ncatlui.h"
  22. #include "ncstl.h"
  23. //#include "ncui.h"
  24. #include "netconp.h"
  25. #include "atmhelp.h"
  26. extern const WCHAR c_szAdapters[];
  27. //
  28. // Load cards on bind path to m_listAdapters on Initialize
  29. //
  30. HRESULT CArpsCfg::HrLoadSettings()
  31. {
  32. HRESULT hr = S_OK;
  33. CIterNetCfgBindingPath ncbpIter(m_pnccArps);
  34. INetCfgBindingPath * pncbp;
  35. // Go through all binding paths in search of arps to netcard bindings
  36. while(SUCCEEDED(hr) && (hr = ncbpIter.HrNext(&pncbp)) == S_OK)
  37. {
  38. INetCfgComponent * pnccNetComponent;
  39. hr = HrGetLastComponentAndInterface(pncbp,
  40. &pnccNetComponent,
  41. NULL);
  42. if (SUCCEEDED(hr))
  43. {
  44. Assert(pnccNetComponent);
  45. // The last component should be of NET CLASS
  46. GUID ClassGuid;
  47. // What type is it?
  48. hr = pnccNetComponent->GetClassGuid(&ClassGuid);
  49. if (SUCCEEDED(hr))
  50. {
  51. // Is it a netcard?
  52. if (IsEqualGUID(ClassGuid, GUID_DEVCLASS_NET))
  53. {
  54. hr = HrAddAdapter(pnccNetComponent);
  55. if (SUCCEEDED(hr))
  56. {
  57. // Is the binding enabled ??
  58. hr = pncbp->IsEnabled();
  59. // hr == S_OK if the card is enabled (ie: bound)
  60. if (hr == S_OK)
  61. { // bind the card in our data strucutres
  62. hr = HrBindAdapter(pnccNetComponent);
  63. }
  64. else if (hr == S_FALSE)
  65. {
  66. hr = HrUnBindAdapter(pnccNetComponent);
  67. }
  68. // Now load cofigurable parameters
  69. if (SUCCEEDED(hr))
  70. {
  71. HKEY hkeyArpsParam;
  72. hr = m_pnccArps->OpenParamKey(&hkeyArpsParam);
  73. if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
  74. {
  75. hr = S_OK;
  76. }
  77. else if (SUCCEEDED(hr))
  78. {
  79. Assert(hkeyArpsParam);
  80. hr = HrLoadArpsRegistry(hkeyArpsParam);
  81. RegCloseKey(hkeyArpsParam);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. ReleaseObj(pnccNetComponent);
  88. }
  89. ReleaseObj(pncbp);
  90. }
  91. AssertSz(!pncbp, "BindingPath wasn't released");
  92. if (hr == S_FALSE) // We just got to the end of the loop
  93. hr = S_OK;
  94. TraceError("CArpsCfg::HrLoadSettings", hr);
  95. return hr;
  96. }
  97. // Load registry settings for configurable parameters into memory
  98. HRESULT CArpsCfg::HrLoadArpsRegistry(HKEY hkeyArpsParam)
  99. {
  100. HRESULT hr = S_OK;
  101. HKEY hkeyAdapters = NULL;
  102. hr = HrRegOpenKeyEx(hkeyArpsParam, c_szAdapters,
  103. KEY_READ, &hkeyAdapters);
  104. if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
  105. hr = S_OK;
  106. else if(SUCCEEDED(hr))
  107. {
  108. HKEY hkeyAdapterParam = NULL;
  109. for(ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  110. iterAdapter != m_listAdapters.end();
  111. iterAdapter ++)
  112. {
  113. // Open the AtmArps\Adapters to get per adapter ARPS settings
  114. hr = HrRegOpenKeyEx(hkeyAdapters,
  115. (*iterAdapter)->m_strBindName.c_str(),
  116. KEY_READ, &hkeyAdapterParam);
  117. if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
  118. {
  119. hr = S_OK;
  120. }
  121. else if (S_OK == hr)
  122. {
  123. TraceTag(ttidAtmArps, "CArpsCfg::HrLoadArpsRegistry");
  124. TraceTag(ttidAtmArps, "Adapter: %S", (*iterAdapter)->m_strBindName.c_str());
  125. HRESULT hrTmp = S_OK;
  126. // Sap selecter
  127. hrTmp = HrRegQueryDword(hkeyAdapterParam,
  128. c_szSapSel,
  129. &((*iterAdapter)->m_dwSapSelector));
  130. (*iterAdapter)->m_dwOldSapSelector = (*iterAdapter)->m_dwSapSelector;
  131. hr = hrTmp;
  132. // Registered addresses
  133. hrTmp = HrRegQueryColString(hkeyAdapterParam,
  134. c_szRegAddrs,
  135. &((*iterAdapter)->m_vstrRegisteredAtmAddrs));
  136. // Save the registry value in old address
  137. CopyColString(&((*iterAdapter)->m_vstrOldRegisteredAtmAddrs),
  138. (*iterAdapter)->m_vstrRegisteredAtmAddrs);
  139. if (S_OK == hr)
  140. {
  141. hr = hrTmp;
  142. }
  143. // Multicast addresses
  144. hrTmp = HrRegQueryColString(hkeyAdapterParam,
  145. c_szMCAddrs,
  146. &((*iterAdapter)->m_vstrMulticastIpAddrs));
  147. CopyColString(&((*iterAdapter)->m_vstrOldMulticastIpAddrs),
  148. (*iterAdapter)->m_vstrMulticastIpAddrs);
  149. if (S_OK == hr)
  150. {
  151. hr = hrTmp;
  152. }
  153. }
  154. RegSafeCloseKey(hkeyAdapterParam);
  155. hkeyAdapterParam = NULL;
  156. }
  157. }
  158. RegSafeCloseKey(hkeyAdapters);
  159. TraceError("CArpsCfg::HrLoadArpsRegistry", hr);
  160. return hr;
  161. }
  162. //
  163. // Update registry with info in m_listAdapters on Apply
  164. //
  165. HRESULT CArpsCfg::HrSaveSettings()
  166. {
  167. HRESULT hr = S_OK;
  168. HKEY hkeyArpsParam = NULL;
  169. hr = m_pnccArps->OpenParamKey(&hkeyArpsParam);
  170. if (S_OK == hr)
  171. {
  172. HKEY hkeyAdapters = NULL;
  173. DWORD dwDisposition;
  174. // Create or open the "Adapters" key under "Services\Atmarps\Parameters"
  175. hr = HrRegCreateKeyEx(hkeyArpsParam,
  176. c_szAdapters,
  177. REG_OPTION_NON_VOLATILE,
  178. KEY_ALL_ACCESS,
  179. NULL,
  180. &hkeyAdapters,
  181. &dwDisposition);
  182. if (S_OK == hr)
  183. {
  184. Assert(hkeyAdapters);
  185. HRESULT hrTmp = S_OK;
  186. if (dwDisposition == REG_OPENED_EXISTING_KEY)
  187. { // if the "adapters" key existed, there might be some old cards
  188. // Cleanup subkeys for adapters that are not in our memory structure
  189. VECSTR vstrAdapters;
  190. hrTmp = HrLoadSubkeysFromRegistry(hkeyAdapters, &vstrAdapters);
  191. if SUCCEEDED(hrTmp)
  192. {
  193. for (size_t i=0; i<vstrAdapters.size(); i++)
  194. {
  195. BOOL fFound = FALSE;
  196. for (ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  197. iterAdapter != m_listAdapters.end();
  198. iterAdapter ++)
  199. {
  200. if ((*iterAdapter)->m_strBindName == vstrAdapters[i]->c_str())
  201. {
  202. fFound = TRUE;
  203. break;
  204. }
  205. }
  206. if ((!fFound) || ((*iterAdapter)->m_fDeleted))
  207. {
  208. hrTmp = HrRegDeleteKeyTree(hkeyAdapters,
  209. vstrAdapters[i]->c_str());
  210. if SUCCEEDED(hr)
  211. hr = hrTmp;
  212. }
  213. }
  214. }
  215. }
  216. // Save adapter info in memory state to registry
  217. for (ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  218. iterAdapter != m_listAdapters.end();
  219. iterAdapter ++)
  220. {
  221. if ((*iterAdapter)->m_fDeleted)
  222. continue;
  223. HKEY hkeyAdapterParam;
  224. // Create specific card bindname key under
  225. // "Services\Atmarps\Parameters\Adapters\<card bind name>"
  226. hrTmp = HrRegCreateKeyEx(hkeyAdapters,
  227. ((*iterAdapter)->m_strBindName).c_str(),
  228. REG_OPTION_NON_VOLATILE,
  229. KEY_ALL_ACCESS,
  230. NULL,
  231. &hkeyAdapterParam,
  232. &dwDisposition);
  233. if(SUCCEEDED(hr))
  234. hr = hrTmp;
  235. if(SUCCEEDED(hrTmp))
  236. {
  237. /*
  238. hrTmp = HrSetDefaultAdapterParam(hkeyAdapterParam);
  239. if SUCCEEDED(hr)
  240. hr = hrTmp;
  241. */
  242. // Sap selecter
  243. hrTmp = HrRegSetDword(hkeyAdapterParam,
  244. c_szSapSel,
  245. (*iterAdapter)->m_dwSapSelector);
  246. if SUCCEEDED(hr)
  247. hr = hrTmp;
  248. // Registered addresses
  249. hrTmp = HrRegSetColString(hkeyAdapterParam,
  250. c_szRegAddrs,
  251. (*iterAdapter)->m_vstrRegisteredAtmAddrs);
  252. if SUCCEEDED(hr)
  253. hr = hrTmp;
  254. // Multicast addresses
  255. hrTmp = HrRegSetColString(hkeyAdapterParam,
  256. c_szMCAddrs,
  257. (*iterAdapter)->m_vstrMulticastIpAddrs);
  258. if SUCCEEDED(hr)
  259. hr = hrTmp;
  260. }
  261. RegSafeCloseKey(hkeyAdapterParam);
  262. }
  263. }
  264. RegSafeCloseKey(hkeyAdapters);
  265. }
  266. RegSafeCloseKey(hkeyArpsParam);
  267. TraceError("CArpsCfg::HrSaveSettings", hr);
  268. return hr;
  269. }
  270. //
  271. // Adding a card
  272. //
  273. HRESULT CArpsCfg::HrAddAdapter(INetCfgComponent * pncc)
  274. {
  275. HRESULT hr = S_OK;
  276. PWSTR pszwBindName = NULL;
  277. hr = pncc->GetBindName(&pszwBindName);
  278. AssertSz( SUCCEEDED(hr), "Net card on binding path with no bind path name!!");
  279. // check if the adapter already existsed and marked as deleted,
  280. // if so, just unmark it
  281. BOOL fFound = FALSE;
  282. for (ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  283. iterAdapter != m_listAdapters.end();
  284. iterAdapter ++)
  285. {
  286. if ((*iterAdapter)->m_strBindName == pszwBindName)
  287. {
  288. Assert((*iterAdapter)->m_fDeleted);
  289. (*iterAdapter)->m_fDeleted = FALSE;
  290. fFound = TRUE;
  291. break;
  292. }
  293. }
  294. if (!fFound) // add a new item
  295. {
  296. CArpsAdapterInfo * pAdapterInfo = new CArpsAdapterInfo;
  297. pAdapterInfo->HrSetDefaults(pszwBindName);
  298. // create a new item for the ARPS_ADAPT_INFO list
  299. m_listAdapters.push_back(pAdapterInfo);
  300. }
  301. CoTaskMemFree(pszwBindName);
  302. TraceError("CArpsCfg::HrAddAdapter", hr);
  303. return hr;
  304. }
  305. //
  306. // Removing a card
  307. //
  308. HRESULT CArpsCfg::HrRemoveAdapter(INetCfgComponent * pncc)
  309. {
  310. HRESULT hr = S_OK;
  311. PWSTR pszwBindName = NULL;
  312. hr = pncc->GetBindName(&pszwBindName);
  313. AssertSz( SUCCEEDED(hr), "Net card on binding path with no bind path name!!");
  314. // mark the adapter as removed
  315. for (ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  316. iterAdapter != m_listAdapters.end();
  317. iterAdapter ++)
  318. {
  319. if ((*iterAdapter)->m_strBindName == pszwBindName)
  320. {
  321. (*iterAdapter)->m_fDeleted = TRUE;
  322. }
  323. }
  324. CoTaskMemFree(pszwBindName);
  325. TraceError("CArpsCfg::HrRemoveAdapter", hr);
  326. return hr;
  327. }
  328. HRESULT CArpsCfg::HrBindAdapter(INetCfgComponent * pnccAdapter)
  329. {
  330. HRESULT hr = S_OK;
  331. PWSTR pszwBindName = NULL;
  332. hr = pnccAdapter->GetBindName(&pszwBindName);
  333. AssertSz(SUCCEEDED(hr), "Net card on binding path with no bind path name!!");
  334. for (ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  335. iterAdapter != m_listAdapters.end();
  336. iterAdapter ++)
  337. {
  338. if ((*iterAdapter)->m_strBindName == pszwBindName)
  339. {
  340. (*iterAdapter)->m_BindingState = BIND_ENABLE;
  341. break;
  342. }
  343. }
  344. CoTaskMemFree(pszwBindName);
  345. TraceError("CArpsCfg::HrBindAdapter", hr);
  346. return hr;
  347. }
  348. HRESULT CArpsCfg::HrUnBindAdapter(INetCfgComponent * pnccAdapter)
  349. {
  350. HRESULT hr = S_OK;
  351. PWSTR pszwBindName = NULL;
  352. hr = pnccAdapter->GetBindName(&pszwBindName);
  353. AssertSz(SUCCEEDED(hr), "Net card on binding path with no bind path name!!");
  354. for (ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  355. iterAdapter != m_listAdapters.end();
  356. iterAdapter ++)
  357. {
  358. if ((*iterAdapter)->m_strBindName == pszwBindName)
  359. {
  360. (*iterAdapter)->m_BindingState = BIND_DISABLE;
  361. break;
  362. }
  363. }
  364. CoTaskMemFree(pszwBindName);
  365. TraceError("CArpsCfg::HrUnBindAdapter", hr);
  366. return hr;
  367. }
  368. // Called by CArpsCfg::MergePropPages
  369. // Set the context in which the UI is brought up
  370. HRESULT CArpsCfg::HrSetConnectionContext()
  371. {
  372. AssertSz(m_pUnkContext, "Invalid IUnknown pointer passed to CArpsCfg::SetContext?");
  373. if (!m_pUnkContext)
  374. return E_FAIL;
  375. HRESULT hr = S_OK;
  376. GUID guidConn;
  377. // Is this a lan connection ?
  378. INetLanConnectionUiInfo * pLanConnUiInfo;
  379. hr = m_pUnkContext->QueryInterface( IID_INetLanConnectionUiInfo,
  380. reinterpret_cast<LPVOID *>(&pLanConnUiInfo));
  381. if (SUCCEEDED(hr))
  382. {
  383. // yes, lan connection
  384. pLanConnUiInfo->GetDeviceGuid(&guidConn);
  385. ReleaseObj(pLanConnUiInfo);
  386. WCHAR szGuid[c_cchGuidWithTerm];
  387. BOOL fSucceeded = StringFromGUID2(guidConn,
  388. szGuid,
  389. c_cchGuidWithTerm);
  390. Assert(fSucceeded);
  391. m_strGuidConn = szGuid;
  392. }
  393. TraceError("CArpsCfg::HrSetConnectionContext", hr);
  394. return hr;
  395. }
  396. // Called by CArpsCfg::MergePropPages
  397. // Allocate property pages
  398. HRESULT CArpsCfg::HrSetupPropSheets(HPROPSHEETPAGE ** pahpsp, INT * pcPages)
  399. {
  400. HRESULT hr = S_OK;
  401. int cPages = 0;
  402. HPROPSHEETPAGE *ahpsp = NULL;
  403. m_fSecondMemoryModified = FALSE;
  404. // Copy adapter specific info: enabled cards only !!
  405. hr = HrLoadAdapterInfo();
  406. // If we have found the matching adapter
  407. if SUCCEEDED(hr)
  408. {
  409. cPages = 1;
  410. delete m_arps;
  411. m_arps = new CArpsPage(this, g_aHelpIDs_IDD_ARPS_PROP);
  412. if (m_arps == NULL)
  413. {
  414. return(ERROR_NOT_ENOUGH_MEMORY);
  415. }
  416. // Allocate a buffer large enough to hold the handles to all of our
  417. // property pages.
  418. ahpsp = (HPROPSHEETPAGE *)CoTaskMemAlloc(sizeof(HPROPSHEETPAGE)
  419. * cPages);
  420. if (!ahpsp)
  421. {
  422. hr = E_OUTOFMEMORY;
  423. goto err;
  424. }
  425. cPages =0;
  426. ahpsp[cPages++] = m_arps->CreatePage(IDD_ARPS_PROP, 0);
  427. *pahpsp = ahpsp;
  428. *pcPages = cPages;
  429. }
  430. else // if we don't have any bound cards, pop-up message box and don't show UI
  431. {
  432. NcMsgBox(::GetActiveWindow(),
  433. IDS_MSFT_ARPS_TEXT,
  434. IDS_ARPS_NO_BOUND_CARDS,
  435. MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  436. AssertSz((0== *pcPages), "Invalid page number when no bound cards");
  437. AssertSz((NULL == *pahpsp), "Invalid page array pointer when no bound cards");
  438. }
  439. err:
  440. TraceError("CArpsCfg::HrSetupPropSheets", hr);
  441. return hr;
  442. }
  443. // Called by CArpsCfg::HrSetupPropSheets
  444. // Creates the second memory adapter info from the first memory structure
  445. // Note: Bound cards only
  446. HRESULT CArpsCfg::HrLoadAdapterInfo()
  447. {
  448. HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_MATCH);
  449. delete m_pSecondMemoryAdapterInfo;
  450. m_pSecondMemoryAdapterInfo = NULL;
  451. for(ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  452. iterAdapter != m_listAdapters.end();
  453. iterAdapter++)
  454. {
  455. if (FIsSubstr(m_strGuidConn.c_str(), (*iterAdapter)->m_strBindName.c_str()))
  456. {
  457. // enabled LAN adapter
  458. if ((*iterAdapter)->m_BindingState == BIND_ENABLE)
  459. {
  460. m_pSecondMemoryAdapterInfo = new CArpsAdapterInfo;
  461. if (m_pSecondMemoryAdapterInfo == NULL)
  462. {
  463. return(ERROR_NOT_ENOUGH_MEMORY);
  464. }
  465. *m_pSecondMemoryAdapterInfo = **iterAdapter;
  466. hr = S_OK;
  467. }
  468. }
  469. }
  470. AssertSz((S_OK == hr), "Can not raise UI on a disabled or non-exist adapter !");
  471. TraceError("CArpsCfg::HrLoadAdapterInfo", hr);
  472. return hr;
  473. }
  474. // Called by CArpsCfg::ApplyProperties
  475. // Saves the second memory state back into the first
  476. HRESULT CArpsCfg::HrSaveAdapterInfo()
  477. {
  478. HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_MATCH);
  479. for(ARPS_ADAPTER_LIST::iterator iterAdapter = m_listAdapters.begin();
  480. iterAdapter != m_listAdapters.end();
  481. iterAdapter++)
  482. {
  483. if(m_pSecondMemoryAdapterInfo->m_strBindName == (*iterAdapter)->m_strBindName)
  484. {
  485. // The card can not get unbound while in the properties UI !
  486. Assert((*iterAdapter)->m_BindingState == BIND_ENABLE);
  487. Assert(m_pSecondMemoryAdapterInfo->m_BindingState == BIND_ENABLE);
  488. **iterAdapter = *m_pSecondMemoryAdapterInfo;
  489. hr = S_OK;
  490. break;
  491. }
  492. }
  493. AssertSz((S_OK == hr),
  494. "Adapter in second memory not found in first memory!");
  495. TraceError("CArpsCfg::HrSaveAdapterInfo", hr);
  496. return hr;
  497. }