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.

565 lines
16 KiB

  1. // This is a part of the Microsoft Management Console.
  2. // Copyright (C) 1995-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Management Console and related
  7. // electronic documentation provided with the interfaces.
  8. #include "stdafx.h"
  9. #include "snapmgr.h"
  10. #include "util.h"
  11. #include "regvldlg.h"
  12. //#include <shlwapi.h>
  13. //#include <shlwapip.h>
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. //
  20. // create registry value list under configuration node
  21. //
  22. void CSnapin::CreateProfileRegValueList(MMC_COOKIE cookie,
  23. PEDITTEMPLATE pSceInfo,
  24. LPDATAOBJECT pDataObj)
  25. {
  26. if ( !pSceInfo || !(pSceInfo->pTemplate) ) {
  27. return;
  28. }
  29. DWORD nCount = pSceInfo->pTemplate->RegValueCount;
  30. PSCE_REGISTRY_VALUE_INFO regArray = pSceInfo->pTemplate->aRegValues;
  31. CString strDisplayName;
  32. LPTSTR pDisplayName=NULL;
  33. DWORD displayType = 0;
  34. LPTSTR szUnits=NULL;
  35. PREGCHOICE pChoices=NULL;
  36. PREGFLAGS pFlags=NULL;
  37. CResult *pResult;
  38. for ( DWORD i=0; i<nCount; i++) {
  39. if ( !LookupRegValueProperty(regArray[i].FullValueName,
  40. &pDisplayName,
  41. &displayType,
  42. &szUnits,
  43. &pChoices,
  44. &pFlags) ) {
  45. continue;
  46. }
  47. if ( !pDisplayName ) {
  48. strDisplayName = regArray[i].FullValueName;
  49. } else {
  50. strDisplayName = pDisplayName;
  51. LocalFree(pDisplayName);
  52. }
  53. //
  54. // add this item
  55. //
  56. pResult = AddResultItem(strDisplayName,
  57. NULL,
  58. (LONG_PTR)&(regArray[i]),
  59. ITEM_PROF_REGVALUE,
  60. -1,
  61. cookie,
  62. false,
  63. szUnits,
  64. displayType,
  65. pSceInfo,
  66. pDataObj);
  67. if (pResult && pChoices) {
  68. pResult->SetRegChoices(pChoices);
  69. }
  70. if (pResult && pFlags) {
  71. pResult->SetRegFlags(pFlags);
  72. }
  73. if ( szUnits ) {
  74. LocalFree(szUnits);
  75. }
  76. szUnits = NULL;
  77. }
  78. return;
  79. }
  80. void
  81. CSnapin::CreateAnalysisRegValueList(MMC_COOKIE cookie,
  82. PEDITTEMPLATE pAnalTemp,
  83. PEDITTEMPLATE pEditTemp,
  84. LPDATAOBJECT pDataObj,
  85. RESULT_TYPES type)
  86. {
  87. if ( !pAnalTemp || !(pAnalTemp->pTemplate) ||
  88. !pEditTemp || !(pEditTemp->pTemplate) ) {
  89. return;
  90. }
  91. DWORD nEditCount = pEditTemp->pTemplate->RegValueCount; // should be everything
  92. PSCE_REGISTRY_VALUE_INFO paEdit = pEditTemp->pTemplate->aRegValues;
  93. PSCE_REGISTRY_VALUE_INFO paAnal = pAnalTemp->pTemplate->aRegValues;
  94. CString strDisplayName;
  95. LPTSTR pDisplayName=NULL;
  96. DWORD displayType = 0;
  97. LPTSTR szUnits = NULL;
  98. PREGCHOICE pChoices = NULL;
  99. PREGFLAGS pFlags = NULL;
  100. CResult *pResult=NULL;
  101. for ( DWORD i=0; i<nEditCount; i++) {
  102. if ( !LookupRegValueProperty(paEdit[i].FullValueName,
  103. &pDisplayName,
  104. &displayType,
  105. &szUnits,
  106. &pChoices,
  107. &pFlags) ) {
  108. continue;
  109. }
  110. if ( !pDisplayName ) {
  111. strDisplayName = paEdit[i].FullValueName;
  112. } else {
  113. strDisplayName = pDisplayName;
  114. LocalFree(pDisplayName);
  115. }
  116. //
  117. // find the match in the analysis array
  118. // should always find a match because all existing reg values are
  119. // added to the array when getinfo is called
  120. //
  121. for ( DWORD j=0; j< pAnalTemp->pTemplate->RegValueCount; j++ ) {
  122. if ( pAnalTemp->pTemplate->aRegValues &&
  123. pAnalTemp->pTemplate->aRegValues[j].FullValueName &&
  124. _wcsicmp(pAnalTemp->pTemplate->aRegValues[j].FullValueName, paEdit[i].FullValueName) == 0 ) {
  125. if( reinterpret_cast<CFolder *>(cookie)->GetModeBits() & MB_LOCAL_POLICY ){
  126. break;
  127. }
  128. //
  129. // find a analysis result - this item may be a mismatch (when Value is not NULL)
  130. // SceEnumAllRegValues will set the status field to good if this item was not
  131. // added because it did not exist when it was originally loaded from the SAP table.
  132. // This tells us that this item is a MATCH and we should copy the value.
  133. //
  134. if ( !(paAnal[j].Value) && paEdit[i].Value &&
  135. paAnal[j].Status != SCE_STATUS_ERROR_NOT_AVAILABLE &&
  136. paAnal[j].Status != SCE_STATUS_NOT_ANALYZED ) {
  137. //
  138. // this is a good item, copy the config info as the analysis info
  139. //
  140. paAnal[j].Value = (PWSTR)LocalAlloc(0,
  141. (wcslen(paEdit[i].Value)+1)*sizeof(WCHAR));
  142. if ( paAnal[j].Value ) {
  143. wcscpy(paAnal[j].Value, paEdit[i].Value);
  144. } else {
  145. // else out of memory
  146. if ( szUnits ) {
  147. LocalFree(szUnits);
  148. }
  149. szUnits = NULL;
  150. return;
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. DWORD status = SCE_STATUS_GOOD;
  157. if ( j < pAnalTemp->pTemplate->RegValueCount ) {
  158. status = CEditTemplate::ComputeStatus( &paEdit[i], &pAnalTemp->pTemplate->aRegValues[j] );
  159. } else {
  160. //
  161. // did not find the analysis array, shouldn't happen
  162. //
  163. status = SCE_STATUS_NOT_CONFIGURED;
  164. }
  165. //
  166. // add this item
  167. //
  168. if ( j < pAnalTemp->pTemplate->RegValueCount) {
  169. pResult = AddResultItem(strDisplayName,
  170. (LONG_PTR)&(pAnalTemp->pTemplate->aRegValues[j]),
  171. (LONG_PTR)&(paEdit[i]),
  172. type,
  173. status,
  174. cookie,
  175. false,
  176. szUnits,
  177. displayType,
  178. pEditTemp,
  179. pDataObj);
  180. if (pResult && pChoices) {
  181. pResult->SetRegChoices(pChoices);
  182. }
  183. if (pResult && pFlags) {
  184. pResult->SetRegFlags(pFlags);
  185. }
  186. } else {
  187. //
  188. // a good/not configured item
  189. //
  190. pResult = AddResultItem(strDisplayName,
  191. NULL,
  192. (LONG_PTR)&(paEdit[i]),
  193. type,
  194. status,
  195. cookie,
  196. false,
  197. szUnits,
  198. displayType,
  199. pEditTemp,
  200. pDataObj);
  201. if (pResult && pChoices) {
  202. pResult->SetRegChoices(pChoices);
  203. }
  204. }
  205. if ( szUnits ) {
  206. LocalFree(szUnits);
  207. }
  208. szUnits = NULL;
  209. }
  210. return;
  211. }
  212. BOOL
  213. LookupRegValueProperty(
  214. IN LPTSTR RegValueFullName,
  215. OUT LPTSTR *pDisplayName,
  216. OUT PDWORD displayType,
  217. OUT LPTSTR *pUnits OPTIONAL,
  218. OUT PREGCHOICE *pChoices OPTIONAL,
  219. OUT PREGFLAGS *pFlags OPTIONAL
  220. )
  221. {
  222. if ( !RegValueFullName || !pDisplayName || !displayType ) {
  223. return FALSE;
  224. }
  225. CString strTmp = RegValueFullName;
  226. //
  227. // replace the \\ with / before search reg
  228. //
  229. int npos = strTmp.Find(L'\\');
  230. while (npos > 0) {
  231. *(strTmp.GetBuffer(1)+npos) = L'/';
  232. npos = strTmp.Find(L'\\');
  233. }
  234. //
  235. // query the values from registry
  236. //
  237. *pDisplayName = NULL;
  238. HKEY hKey=NULL;
  239. HKEY hKey2=NULL;
  240. DWORD rc = RegOpenKeyEx(
  241. HKEY_LOCAL_MACHINE,
  242. SCE_ROOT_REGVALUE_PATH,
  243. 0,
  244. KEY_READ,
  245. &hKey
  246. );
  247. if (rc == ERROR_SUCCESS) {
  248. rc = RegOpenKeyEx(
  249. hKey,
  250. (PWSTR)(LPCTSTR)strTmp,
  251. 0,
  252. KEY_READ,
  253. &hKey2
  254. );
  255. }
  256. BOOL bRet;
  257. if ( ERROR_SUCCESS == rc) {
  258. DWORD RegType = 0;
  259. PWSTR Value=NULL;
  260. HRESULT hr = S_OK;
  261. Value = (PWSTR) LocalAlloc(LPTR,MAX_PATH*sizeof(WCHAR));
  262. if (Value) {
  263. //
  264. // 126714 - shouldn't hard code display strings in the registry
  265. // store them indirectly so they can support MUI
  266. //
  267. hr = SHLoadRegUIString(hKey2,
  268. SCE_REG_DISPLAY_NAME,
  269. Value,
  270. MAX_PATH);
  271. if (FAILED(hr)) {
  272. rc = MyRegQueryValue(
  273. hKey,
  274. (PWSTR)(LPCTSTR)strTmp,
  275. SCE_REG_DISPLAY_NAME,
  276. (PVOID *)&Value,
  277. &RegType
  278. );
  279. } else {
  280. rc = ERROR_SUCCESS;
  281. }
  282. }
  283. if ( rc == ERROR_SUCCESS ) {
  284. if ( Value ) {
  285. *pDisplayName = Value;
  286. Value = NULL;
  287. } else {
  288. //
  289. // did not find correct display name, use the reg name (outsize)
  290. //
  291. *pDisplayName = NULL;
  292. }
  293. }
  294. rc = MyRegQueryValue(
  295. hKey,
  296. (PWSTR)(LPCTSTR)strTmp,
  297. SCE_REG_DISPLAY_TYPE,
  298. (PVOID *)&displayType,
  299. &RegType
  300. );
  301. if ( Value ) {
  302. LocalFree(Value);
  303. Value = NULL;
  304. }
  305. if ( pUnits ) {
  306. //
  307. // query the units
  308. //
  309. rc = MyRegQueryValue(
  310. hKey,
  311. (PWSTR)(LPCTSTR)strTmp,
  312. SCE_REG_DISPLAY_UNIT,
  313. (PVOID *)&Value,
  314. &RegType
  315. );
  316. if ( rc == ERROR_SUCCESS ) {
  317. if ( RegType == REG_SZ && Value ) {
  318. *pUnits = Value;
  319. Value = NULL;
  320. } else {
  321. //
  322. // did not find units
  323. //
  324. *pUnits = NULL;
  325. }
  326. }
  327. if ( Value ) {
  328. LocalFree(Value);
  329. Value = NULL;
  330. }
  331. }
  332. //
  333. // find the registry key but may not find the display name
  334. //
  335. bRet = TRUE;
  336. if ( pChoices ) {
  337. //
  338. // query the choices
  339. //
  340. *pChoices = NULL;
  341. rc = MyRegQueryValue(hKey,
  342. (PWSTR)(LPCTSTR)strTmp,
  343. SCE_REG_DISPLAY_CHOICES,
  344. (PVOID *)&Value,
  345. &RegType
  346. );
  347. if (ERROR_SUCCESS == rc) {
  348. if ((REG_MULTI_SZ == RegType) && Value) {
  349. LPTSTR szChoice = NULL;
  350. LPTSTR szLabel = NULL; // max field size for szChoice + dwVal
  351. DWORD dwVal = -1;
  352. PREGCHOICE pRegChoice = NULL;
  353. PREGCHOICE pLast = NULL;
  354. szChoice = Value;
  355. do {
  356. //
  357. // Divide szChoice into dwValue and szLabel sections
  358. //
  359. szLabel = _tcschr(szChoice,L'|');
  360. *szLabel = L'\0';
  361. szLabel++;
  362. dwVal = _ttoi(szChoice);
  363. pRegChoice = (PREGCHOICE) LocalAlloc(LPTR,sizeof(REGCHOICE));
  364. if (pRegChoice) {
  365. //
  366. // Fill in fields of new reg choice
  367. //
  368. pRegChoice->dwValue = dwVal;
  369. pRegChoice->szName = (LPTSTR) LocalAlloc(LPTR,(lstrlen(szLabel)+1)*sizeof(TCHAR));
  370. if (NULL == pRegChoice->szName) {
  371. //
  372. // Out of memory. Bummer.
  373. //
  374. LocalFree(pRegChoice);
  375. pRegChoice = NULL;
  376. break;
  377. }
  378. lstrcpy(pRegChoice->szName,szLabel);
  379. //
  380. // Attach new item to end of list
  381. //
  382. if (NULL == *pChoices) {
  383. *pChoices = pRegChoice;
  384. } else {
  385. pLast->pNext = pRegChoice;
  386. }
  387. pLast = pRegChoice;
  388. }
  389. szChoice = _tcschr(szLabel,L'\0');
  390. szChoice++;
  391. } while (*szChoice);
  392. } else {
  393. //
  394. // Did not find choices
  395. //
  396. bRet = FALSE;
  397. }
  398. }
  399. if ( Value ) {
  400. LocalFree(Value);
  401. Value = NULL;
  402. }
  403. }
  404. if ( pFlags ) {
  405. //
  406. // query the Flags
  407. //
  408. *pFlags = NULL;
  409. rc = MyRegQueryValue(hKey,
  410. (PWSTR)(LPCTSTR)strTmp,
  411. SCE_REG_DISPLAY_FLAGLIST,
  412. (PVOID *)&Value,
  413. &RegType
  414. );
  415. if (ERROR_SUCCESS == rc) {
  416. if ((REG_MULTI_SZ == RegType) && Value) {
  417. LPTSTR szFlag = NULL;
  418. LPTSTR szLabel = NULL; // max field size for szFlag + dwVal
  419. DWORD dwVal = -1;
  420. PREGFLAGS pRegFlag = NULL;
  421. PREGFLAGS pLast = NULL;
  422. szFlag = Value;
  423. do {
  424. //
  425. // Divide szFlag into dwValue and szLabel sections
  426. //
  427. szLabel = _tcschr(szFlag,L'|');
  428. *szLabel = L'\0';
  429. szLabel++;
  430. dwVal = _ttoi(szFlag);
  431. pRegFlag = (PREGFLAGS) LocalAlloc(LPTR,sizeof(REGFLAGS));
  432. if (pRegFlag) {
  433. //
  434. // Fill in fields of new reg Flag
  435. //
  436. pRegFlag->dwValue = dwVal;
  437. pRegFlag->szName = (LPTSTR) LocalAlloc(LPTR,(lstrlen(szLabel)+1)*sizeof(TCHAR));
  438. if (NULL == pRegFlag->szName) {
  439. //
  440. // Out of memory. Bummer.
  441. //
  442. LocalFree(pRegFlag);
  443. pRegFlag = NULL;
  444. break;
  445. }
  446. lstrcpy(pRegFlag->szName,szLabel);
  447. //
  448. // Attach new item to end of list
  449. //
  450. if (NULL == *pFlags) {
  451. *pFlags = pRegFlag;
  452. } else {
  453. pLast->pNext = pRegFlag;
  454. }
  455. pLast = pRegFlag;
  456. }
  457. szFlag = wcschr(szLabel,L'\0');
  458. szFlag++;
  459. } while (*szFlag);
  460. } else {
  461. //
  462. // Did not find Flags
  463. //
  464. bRet = FALSE;
  465. }
  466. }
  467. if ( Value ) {
  468. LocalFree(Value);
  469. Value = NULL;
  470. }
  471. }
  472. } else {
  473. //
  474. // did not find the registry key
  475. //
  476. bRet = FALSE;
  477. }
  478. if ( hKey ) {
  479. RegCloseKey(hKey);
  480. }
  481. if ( hKey2 ) {
  482. RegCloseKey(hKey2);
  483. }
  484. return bRet;
  485. }