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.

426 lines
10 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. copyreg.c
  5. Abstract:
  6. This module provides functions to copy registry keys
  7. Author:
  8. Krishna Ganugapati (KrishnaG) 20-Apr-1994
  9. Notes:
  10. List of functions include
  11. CopyValues
  12. CopyRegistryKeys
  13. Revision History:
  14. --*/
  15. #include <precomp.h>
  16. #pragma hdrstop
  17. #include "clusspl.h"
  18. extern WCHAR *szRegistryPrinters;
  19. VOID
  20. CopyValues(
  21. HKEY hSourceKey,
  22. HKEY hDestKey,
  23. PINISPOOLER pIniSpooler
  24. );
  25. BOOL
  26. CopyRegistryKeys(
  27. HKEY hSourceParentKey,
  28. LPWSTR szSourceKey,
  29. HKEY hDestParentKey,
  30. LPWSTR szDestKey,
  31. PINISPOOLER pIniSpooler
  32. );
  33. VOID
  34. CopyValues(
  35. HKEY hSourceKey,
  36. HKEY hDestKey,
  37. PINISPOOLER pIniSpooler
  38. )
  39. /*++
  40. Description: This function copies all the values from hSourceKey to hDestKey.
  41. hSourceKey should be opened with KEY_READ and hDestKey should be opened with
  42. KEY_WRITE.
  43. Returns: VOID
  44. --*/
  45. {
  46. DWORD iCount = 0;
  47. WCHAR szValueString[MAX_PATH];
  48. DWORD dwSizeValueString;
  49. DWORD dwType = 0;
  50. PBYTE pData;
  51. DWORD cbData = 1024;
  52. DWORD dwSizeData;
  53. SplRegQueryInfoKey( hSourceKey,
  54. NULL,
  55. NULL,
  56. NULL,
  57. NULL,
  58. &cbData,
  59. NULL,
  60. NULL,
  61. pIniSpooler );
  62. pData = (PBYTE)AllocSplMem( cbData );
  63. if( pData ){
  64. dwSizeValueString = COUNTOF(szValueString);
  65. dwSizeData = cbData;
  66. while ((SplRegEnumValue(hSourceKey,
  67. iCount,
  68. szValueString,
  69. &dwSizeValueString,
  70. &dwType,
  71. pData,
  72. &dwSizeData,
  73. pIniSpooler
  74. )) == ERROR_SUCCESS ) {
  75. SplRegSetValue( hDestKey,
  76. szValueString,
  77. dwType,
  78. pData,
  79. dwSizeData, pIniSpooler);
  80. dwSizeValueString = COUNTOF(szValueString);
  81. dwType = 0;
  82. dwSizeData = cbData;
  83. iCount++;
  84. }
  85. FreeSplMem( pData );
  86. }
  87. }
  88. BOOL
  89. CopyRegistryKeys(
  90. HKEY hSourceParentKey,
  91. LPWSTR szSourceKey,
  92. HKEY hDestParentKey,
  93. LPWSTR szDestKey,
  94. PINISPOOLER pIniSpooler
  95. )
  96. /*++
  97. Description:This function recursively copies the szSourceKey to szDestKey. hSourceParentKey
  98. is the parent key of szSourceKey and hDestParentKey is the parent key of szDestKey.
  99. Returns: TRUE if the function succeeds; FALSE on failure.
  100. --*/
  101. {
  102. DWORD dwRet;
  103. DWORD iCount;
  104. HKEY hSourceKey, hDestKey;
  105. WCHAR lpszName[MAX_PATH];
  106. DWORD dwSize;
  107. dwRet = SplRegOpenKey(hSourceParentKey,
  108. szSourceKey, KEY_READ, &hSourceKey, pIniSpooler);
  109. if (dwRet != ERROR_SUCCESS) {
  110. return(FALSE);
  111. }
  112. dwRet = SplRegCreateKey(hDestParentKey,
  113. szDestKey, 0, KEY_WRITE, NULL, &hDestKey, NULL, pIniSpooler);
  114. if (dwRet != ERROR_SUCCESS) {
  115. SplRegCloseKey(hSourceKey, pIniSpooler);
  116. return(FALSE);
  117. }
  118. iCount = 0;
  119. memset(lpszName, 0, sizeof(WCHAR)*MAX_PATH);
  120. dwSize = COUNTOF(lpszName);
  121. while((SplRegEnumKey(hSourceKey, iCount, lpszName,
  122. &dwSize,NULL,pIniSpooler)) == ERROR_SUCCESS) {
  123. CopyRegistryKeys( hSourceKey,
  124. lpszName,
  125. hDestKey,
  126. lpszName,
  127. pIniSpooler );
  128. memset(lpszName, 0, sizeof(WCHAR)*MAX_PATH);
  129. dwSize = COUNTOF(lpszName);
  130. iCount++;
  131. }
  132. CopyValues(hSourceKey, hDestKey, pIniSpooler);
  133. SplRegCloseKey(hSourceKey, pIniSpooler);
  134. SplRegCloseKey(hDestKey, pIniSpooler);
  135. return(TRUE);
  136. }
  137. BOOL
  138. bValidPrinter(
  139. HKEY hKey
  140. )
  141. /*++
  142. Description: Assume that hKey is a printer key and check for existence of value Attributes.
  143. If Attributes exists, assume it's a valid printer key.
  144. This function is called when migrate keys between SYSTEM and SOFTWARE hives.
  145. Returns: TRUE if it is a valid printer key
  146. --*/
  147. {
  148. DWORD dwRet;
  149. dwRet = RegQueryValueEx( hKey, L"Attributes", 0, NULL, NULL, NULL );
  150. return dwRet == ERROR_SUCCESS;
  151. }
  152. VOID
  153. CopyPrinterValues(
  154. HKEY hSourceKey,
  155. HKEY hDestKey
  156. )
  157. /*++
  158. Description: Copy values from hSourceKey to hDestKey if their names are: Name, Printer Driver or Default DevMode
  159. This function is called when migrate keys between SYSTEM and SOFTWARE hive in order to copy only
  160. few values( the most important one)
  161. Returns: VOID
  162. --*/
  163. {
  164. DWORD dwSizeData;
  165. DWORD dwType;
  166. PBYTE pData;
  167. int iCount;
  168. DWORD cbData = 1024;
  169. static LPCWSTR PrinterValuesTable [] = {{L"Name"},
  170. {L"Printer Driver"},
  171. {L"Default DevMode"},
  172. {L"Port"},
  173. {0}};
  174. RegQueryInfoKey( hSourceKey,
  175. NULL,
  176. NULL,
  177. NULL,
  178. NULL,
  179. NULL,
  180. NULL,
  181. NULL,
  182. NULL,
  183. &cbData,
  184. NULL,
  185. NULL );
  186. pData = (PBYTE)AllocSplMem( cbData );
  187. if( pData ){
  188. for ( iCount = 0;
  189. dwSizeData = cbData, PrinterValuesTable[iCount];
  190. iCount++ ){
  191. if ( RegQueryValueEx(hSourceKey,
  192. PrinterValuesTable[iCount],
  193. 0,
  194. &dwType,
  195. pData,
  196. &dwSizeData
  197. ) == ERROR_SUCCESS ) {
  198. RegSetValueEx( hDestKey,
  199. PrinterValuesTable[iCount],
  200. 0,
  201. dwType,
  202. pData,
  203. dwSizeData);
  204. }
  205. }
  206. FreeSplMem( pData );
  207. }
  208. }
  209. BOOL
  210. CopyPrinters(
  211. HKEY hSourceParentKey,
  212. LPWSTR szSourceKey,
  213. HKEY hDestParentKey,
  214. LPWSTR szDestKey,
  215. BOOL bTruncated)
  216. /*++
  217. Description: Recursively copy printer keys from hSourceParentKey,szSourceKey to hDestParentKey,szDestKey.
  218. This function is called at registry migration time and szDestKey and szSourceKey
  219. are one of the ...\Print\Printers paths in SYSTEM or SOFTWARE hives.
  220. bTruncated on TRUE specifies that subkeys under "\Printers" must not be copy and only a minimal
  221. set of value should be copied.bTruncated must be true when copy from SOFTWARE to SYSTEM.
  222. Also, the keys under "\Printers" key are verified from a "valid printer" point of view.
  223. Returns: TRUE if the function succeeds; FALSE on failure.
  224. --*/
  225. {
  226. DWORD dwRet;
  227. INT iCount;
  228. WCHAR szName[MAX_PATH];
  229. DWORD dwSize;
  230. BOOL bRetValue;
  231. HKEY hSourceKey = NULL, hDestKey = NULL;
  232. HKEY hSourceSubKey, hDestSubKey;
  233. bRetValue = FALSE;
  234. //
  235. // Open source "Printers" key
  236. //
  237. dwRet = RegOpenKeyEx(hSourceParentKey,
  238. szSourceKey,
  239. 0,
  240. KEY_READ,
  241. &hSourceKey);
  242. if (dwRet != ERROR_SUCCESS) {
  243. goto End;
  244. }
  245. //
  246. // Create/Open destination "Printers" key
  247. //
  248. dwRet = RegCreateKeyEx(hDestParentKey,
  249. szDestKey,
  250. 0,
  251. NULL,
  252. 0,
  253. KEY_WRITE,
  254. NULL,
  255. &hDestKey,
  256. NULL);
  257. if ( dwRet != ERROR_SUCCESS ) {
  258. goto End;
  259. }
  260. //
  261. // Enumerates printer keys
  262. //
  263. for ( iCount = 0, bRetValue = TRUE;
  264. dwSize = COUNTOF(szName),
  265. (RegEnumKeyEx(hSourceKey,
  266. iCount,
  267. szName,
  268. &dwSize,
  269. NULL,
  270. NULL,
  271. NULL,
  272. NULL) == ERROR_SUCCESS ) &&
  273. bRetValue;
  274. iCount++
  275. ) {
  276. dwRet = RegOpenKeyEx(hSourceKey,
  277. szName,
  278. 0,
  279. KEY_READ,
  280. &hSourceSubKey);
  281. if ( bRetValue = (dwRet == ERROR_SUCCESS) ) {
  282. if( bValidPrinter(hSourceSubKey) ){
  283. //
  284. // Copy printer key if valid
  285. //
  286. dwRet = RegCreateKeyEx(hDestKey,
  287. szName,
  288. 0,
  289. NULL,
  290. 0,
  291. KEY_WRITE,
  292. NULL,
  293. &hDestSubKey,
  294. NULL);
  295. if( bRetValue = (dwRet == ERROR_SUCCESS) ){
  296. if( bTruncated ){
  297. //
  298. // It is a copying from SOFTWARE to SYSTEM
  299. // Copy only most important values , without any subkeys
  300. //
  301. CopyPrinterValues(hSourceSubKey, hDestSubKey);
  302. }else{
  303. //
  304. // Recursive copy printer key with all it's values and subkeys
  305. //
  306. bRetValue = CopyRegistryKeys( hSourceKey,
  307. szName,
  308. hDestKey,
  309. szName,
  310. NULL);
  311. }
  312. RegCloseKey(hDestSubKey);
  313. }
  314. }
  315. RegCloseKey(hSourceSubKey);
  316. }
  317. }
  318. //
  319. // Copy all values from '.../Printers" to ".../Printers' keys
  320. //
  321. CopyValues(hSourceKey, hDestKey, NULL);
  322. if( hSourceKey ){
  323. RegCloseKey(hSourceKey);
  324. }
  325. if( hDestKey ){
  326. RegCloseKey(hDestKey);
  327. }
  328. End:
  329. return bRetValue;
  330. }