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.

456 lines
13 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <ncxbase.h>
  4. #include "advanced.h"
  5. #include "advstrs.h"
  6. #include "kkcwinf.h"
  7. #include "ncatlui.h"
  8. #include "ncreg.h"
  9. #include "ncsetup.h"
  10. #include "ncui.h"
  11. #include "resource.h"
  12. //+---------------------------------------------------------------------------
  13. //
  14. // Member: CAdvancedParams::CAdvancedParams (constructor)
  15. //
  16. // Purpose: Init some variables.
  17. //
  18. // Author: t-nabilr 06 Apr 1997
  19. //
  20. // Notes: The bulk of the setting up occurs in FInit().
  21. //
  22. CAdvancedParams::CAdvancedParams()
  23. : m_hkRoot(NULL),
  24. m_pparam(NULL),
  25. m_nCurSel(0),
  26. m_fInit(FALSE),
  27. m_hdi(NULL),
  28. m_pdeid(NULL)
  29. {
  30. }
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Member: CAdvancedParams::HrInit
  34. //
  35. // Purpose: Initializes the class.
  36. //
  37. // Arguments:
  38. // pnccItem [in] ptr to my INetCfgComponent interface.
  39. //
  40. // Returns: TRUE if initialization was okay, FALSE if couldn't init.
  41. //
  42. // Author: t-nabilr 06 Apr 1997
  43. //
  44. // Notes: We needed to separate this from the constructor since the
  45. // initialization can fail.
  46. //
  47. HRESULT CAdvancedParams::HrInit(HDEVINFO hdi, PSP_DEVINFO_DATA pdeid)
  48. {
  49. HKEY hkNdiParamKey;
  50. Assert(IsValidHandle(hdi));
  51. Assert(pdeid);
  52. // Open the device's instance key
  53. HRESULT hr = HrSetupDiOpenDevRegKey(hdi, pdeid,
  54. DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_ALL_ACCESS,
  55. &m_hkRoot);
  56. if (SUCCEEDED(hr))
  57. {
  58. hr = HrRegOpenKeyEx(m_hkRoot, c_szRegKeyParamsFromInstance,
  59. KEY_READ | KEY_SET_VALUE, &hkNdiParamKey);
  60. // populate the parameter list
  61. if (SUCCEEDED(hr))
  62. {
  63. FillParamList(m_hkRoot, hkNdiParamKey);
  64. RegSafeCloseKey(hkNdiParamKey);
  65. m_fInit = TRUE;
  66. m_hdi = hdi;
  67. m_pdeid = pdeid;
  68. hr = S_OK;
  69. }
  70. }
  71. TraceErrorOptional("CAdvancedParams::HrInit", hr,
  72. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr);
  73. return hr;
  74. }
  75. CAdvancedParams::~CAdvancedParams()
  76. {
  77. vector<CParam *>::iterator ppParam;
  78. // delete everything from the list
  79. for (ppParam = m_listpParam.begin(); ppParam != m_listpParam.end();
  80. ppParam++)
  81. {
  82. delete *ppParam;
  83. }
  84. RegSafeCloseKey(m_hkRoot);
  85. }
  86. //+---------------------------------------------------------------------------
  87. //
  88. // Member: CAdvancedParams::FSave
  89. //
  90. // Purpose: Saves values from InMemory storage to the registry.
  91. //
  92. // Returns: TRUE if something was changed; FALSE if nothig changed
  93. // registry.
  94. //
  95. // Author: t-nabilr 06 Apr 1997
  96. //
  97. // Notes:
  98. //
  99. BOOL CAdvancedParams::FSave()
  100. {
  101. vector<CParam *>::iterator ppParam;
  102. BOOL fErrorOccurred = FALSE;
  103. // Save any changed params
  104. BOOL fDirty = FALSE;
  105. for (ppParam = m_listpParam.begin(); ppParam != m_listpParam.end();
  106. ppParam++)
  107. {
  108. Assert(ppParam);
  109. Assert(*ppParam);
  110. if ((*ppParam)->FIsModified())
  111. {
  112. fDirty = TRUE;
  113. TraceTag(ttidNetComm, "Parameter %S has changed",
  114. (*ppParam)->SzGetKeyName());
  115. if (!(*ppParam)->Apply())
  116. {
  117. fErrorOccurred = TRUE;
  118. }
  119. }
  120. }
  121. if (fErrorOccurred)
  122. {
  123. TraceTag(ttidError, "An error occurred saving adapter's %S "
  124. "parameter.", (*ppParam)->GetDesc());
  125. }
  126. return fDirty;
  127. }
  128. //+---------------------------------------------------------------------------
  129. //
  130. // Member: CAdvancedParams::FillParamList
  131. //
  132. // Purpose: Populates the internal parameter list (m_listpParam) with
  133. // values from the registry.
  134. //
  135. // Arguments:
  136. // hk [in] The key from which to enumerate the parameters.
  137. // Normally obtained from a call to INCC->OpenNdiParamKey().
  138. //
  139. // Author: t-nabilr 06 Apr 1997
  140. //
  141. // Notes:
  142. //
  143. VOID CAdvancedParams::FillParamList(HKEY hkRoot, HKEY hk)
  144. {
  145. DWORD iValue;
  146. CParam *pParam;
  147. WCHAR szRegValue[_MAX_PATH];
  148. DWORD cchRegValue;
  149. HRESULT hr = S_OK;
  150. FILETIME ft;
  151. // Initialize the list.
  152. m_listpParam.erase(m_listpParam.begin(), m_listpParam.end());
  153. iValue = 0;
  154. for (iValue = 0; SUCCEEDED(hr); iValue++)
  155. {
  156. cchRegValue = celems(szRegValue);
  157. hr = HrRegEnumKeyEx(hk, iValue, szRegValue, &cchRegValue,
  158. NULL,NULL,&ft);
  159. if (SUCCEEDED(hr))
  160. {
  161. // Create the param structure
  162. pParam = new CParam;
  163. if (pParam == NULL)
  164. {
  165. return;
  166. }
  167. if (pParam->FInit(hkRoot, hk,szRegValue))
  168. {
  169. // Add parameter to list.
  170. m_listpParam.push_back(pParam);
  171. }
  172. else
  173. {
  174. // couldn't Create() it...
  175. delete pParam;
  176. }
  177. }
  178. }
  179. }
  180. //+---------------------------------------------------------------------------
  181. //
  182. // Member: CAdvancedParams::FValidateAllParams
  183. //
  184. // Purpose: Validates values of all parameters. Displays optional
  185. // error UI.
  186. //
  187. // Arguments:
  188. // fDisplayUI [in] TRUE - on error, focus is set to offending
  189. // parameter and the error message box is displayed.
  190. // FALSE - don't do any UI thing on error. Useful
  191. // when the dialog has not been initialized.
  192. // Returns: TRUE - everything validated okay.
  193. // FALSE - error with one of the parameters. If (fDisplayUI),
  194. // then the currently displayed parameter is the offending one.
  195. //
  196. // Author: t-nabilr 06 Apr 1997
  197. //
  198. // Notes: Calls FValidateSingleParam() for each parameter.
  199. //
  200. BOOL CAdvancedParams::FValidateAllParams(BOOL fDisplayUI, HWND hwndParent)
  201. {
  202. BOOL fRetval = TRUE;
  203. for (size_t i = 0; i < m_listpParam.size(); i++)
  204. {
  205. if (!FValidateSingleParam(m_listpParam[i], fDisplayUI, hwndParent))
  206. {
  207. TraceTag(ttidError, "NetComm : %S parameter failed validation",
  208. m_listpParam[i]->GetDesc());
  209. fRetval = FALSE;
  210. break;
  211. }
  212. }
  213. return fRetval;
  214. }
  215. //+---------------------------------------------------------------------------
  216. //
  217. // Member: CAdvancedParams::FValidateSingleParam
  218. //
  219. // Purpose: Validates a single parameter. Displaying an optional
  220. // error UI.
  221. //
  222. // Arguments:
  223. // pparam [in] ptr to the param to be validated. If
  224. // (fDisplayUI), then this must be the currently
  225. // displayed parameter.
  226. // fDisplayUI [in] TRUE - error UI is to be displayed.
  227. // FALSE - no error UI is to be displayed.
  228. //
  229. // Returns: TRUE - parameter validated okay; FALSE - error in parameter.
  230. //
  231. // Author: t-nabilr 06 Apr 1997
  232. //
  233. // Notes: If fDisplayUI, then pparam must be the currently displayed
  234. // param, since the error box will pop up indicating the error.
  235. //
  236. BOOL CAdvancedParams::FValidateSingleParam(CParam * pparam, BOOL fDisplayUI, HWND hwndParent)
  237. {
  238. BOOL fRetval = FALSE;
  239. WCHAR szMin[c_cchMaxNumberSize];
  240. WCHAR szMax[c_cchMaxNumberSize];
  241. WCHAR szStep[c_cchMaxNumberSize];
  242. // ensure we're the currently displayed param if fDisplayUI
  243. AssertSz(FImplies(fDisplayUI, m_pparam == pparam),
  244. "Not the currently displayed param.");
  245. switch (pparam->Validate())
  246. {
  247. case VALUE_OK:
  248. fRetval = TRUE;
  249. break;
  250. case VALUE_BAD_CHARS:
  251. if (fDisplayUI)
  252. {
  253. NcMsgBox(hwndParent, IDS_ERROR_CAPTION, IDS_ERR_VALUE_BAD_CHARS,
  254. MB_ICONWARNING);
  255. }
  256. break;
  257. case VALUE_EMPTY:
  258. if (fDisplayUI)
  259. {
  260. NcMsgBox(hwndParent, IDS_ERROR_CAPTION, IDS_ERR_VALUE_EMPTY,
  261. MB_ICONWARNING);
  262. }
  263. break;
  264. case VALUE_OUTOFRANGE:
  265. Assert(pparam->GetValue()->IsNumeric());
  266. pparam->GetMin()->ToString(szMin, celems(szMin));
  267. pparam->GetMax()->ToString(szMax, celems(szMax));
  268. if (fDisplayUI)
  269. {
  270. // need to select between two dialogs depending on the step size.
  271. if (pparam->GetStep()->GetNumericValueAsDword() == 1)
  272. {
  273. // no step
  274. NcMsgBox(hwndParent, IDS_ERROR_CAPTION, IDS_PARAM_RANGE,
  275. MB_ICONWARNING, szMin, szMax);
  276. }
  277. else
  278. {
  279. pparam->GetStep()->ToString(szStep, celems(szStep));
  280. NcMsgBox(hwndParent, IDS_ERROR_CAPTION, IDS_PARAM_RANGE_STEP,
  281. MB_ICONWARNING, szMin, szMax, szStep);
  282. }
  283. }
  284. else
  285. {
  286. TraceTag(ttidNetComm, "The parameter %S was out of range. "
  287. "Attempting to correct.", pparam->SzGetKeyName());
  288. // Since we can't bring up UI, we will try to correct the
  289. // error for the user
  290. //
  291. if (pparam->GetMin() > pparam->GetValue())
  292. {
  293. // Try to set to the minimum value. If it fails, we must still
  294. // continue
  295. (void) FSetParamValue(pparam->SzGetKeyName(), szMin);
  296. }
  297. if (pparam->GetMax() < pparam->GetValue())
  298. {
  299. // Try to set to the maximum value. If it fails, we must still
  300. // continue
  301. (void) FSetParamValue(pparam->SzGetKeyName(), szMax);
  302. }
  303. }
  304. break;
  305. default:
  306. AssertSz(FALSE,"Hit the default on a switch");
  307. }
  308. return fRetval;
  309. }
  310. //+---------------------------------------------------------------------------
  311. //
  312. // Member: CAdvancedParams::UseAnswerFile
  313. //
  314. // Purpose: Get adapter specific params from the answerfile
  315. //
  316. // Arguments:
  317. // szAnswerFile [in] path of answerfile
  318. // szSection [in] section within answerfile
  319. //
  320. // Author: t-nabilr 06 Apr 1997
  321. //
  322. // Notes:
  323. //
  324. VOID CAdvancedParams::UseAnswerFile(const WCHAR * szAnswerFile,
  325. const WCHAR * szSection)
  326. {
  327. CWInfFile AnswerFile;
  328. CWInfSection* pSection;
  329. const WCHAR* szAFKeyName;
  330. const WCHAR* szAFKeyValue;
  331. const WCHAR* szAdditionalParamsSection;
  332. // initialize answer file class
  333. if (AnswerFile.Init() == FALSE)
  334. {
  335. AssertSz(FALSE,"CAdvancedParams::UseAnswerFile - Failed to initialize CWInfFile");
  336. return;
  337. }
  338. // Open the answerfile and find the desired section.
  339. AnswerFile.Open(szAnswerFile);
  340. pSection = AnswerFile.FindSection(szSection);
  341. if (pSection)
  342. {
  343. // go through all the keys in this section.
  344. CWInfKey * pInfKey;
  345. // Now, go to AdditionalParams section and read key values from there
  346. szAdditionalParamsSection =
  347. pSection->GetStringValue(L"AdditionalParams", L"");
  348. Assert(szAdditionalParamsSection);
  349. if (lstrlenW(szAdditionalParamsSection) < 1)
  350. {
  351. TraceTag(ttidNetComm, "No additional params section");
  352. }
  353. else
  354. {
  355. pSection = AnswerFile.FindSection(szAdditionalParamsSection);
  356. if (!pSection)
  357. {
  358. TraceTag(ttidNetComm, "Specified AdditionalParams section not "
  359. "found.");
  360. }
  361. else
  362. {
  363. for (pInfKey = pSection->FirstKey();
  364. pInfKey;
  365. pInfKey = pSection->NextKey())
  366. {
  367. // get key name
  368. szAFKeyName = pInfKey->Name();
  369. szAFKeyValue = pInfKey->GetStringValue(L"");
  370. Assert(szAFKeyName && szAFKeyValue);
  371. if (!FSetParamValue(szAFKeyName, szAFKeyValue))
  372. {
  373. TraceTag(ttidNetComm, "Key %S not in ndi\\params. "
  374. "Assuming it is a static parameter.",
  375. szAFKeyName);
  376. }
  377. } // for
  378. } // if
  379. } // if
  380. }
  381. }
  382. //+---------------------------------------------------------------------------
  383. //
  384. // Member: CAdvancedParams::SetParamValue
  385. //
  386. // Purpose: Sets a parameter's value
  387. //
  388. // Arguments:
  389. // szName [in] Name of parameter.
  390. // szValue [in] value (in text) to give param (from Answerfile)
  391. //
  392. // Returns: TRUE if szName was found.
  393. //
  394. // Author: t-nabilr 06 Apr 1997
  395. //
  396. // Notes:
  397. //
  398. BOOL
  399. CAdvancedParams::FSetParamValue (
  400. const WCHAR* pszName,
  401. const WCHAR* const pszValue)
  402. {
  403. for (size_t i = 0; i < m_listpParam.size(); i++)
  404. {
  405. if (0 == lstrcmpiW (pszName, m_listpParam[i]->SzGetKeyName()))
  406. {
  407. // found the param
  408. // set it's current value
  409. m_listpParam[i]->GetValue()->FromString (pszValue);
  410. m_listpParam[i]->SetModified (TRUE);
  411. return TRUE; // found
  412. }
  413. }
  414. return FALSE; // not found
  415. }