static BOOL CopyKeys(HKEY hkFrom, HKEY hkTo) { TCHAR lpstrName[nMaxKeyNameSize]; TCHAR lpstrClass[nMaxClassSize]; for (int i = 0;; i++) { DWORD nNameSize = nMaxKeyNameSize; DWORD nClassSize = nMaxClassSize; LONG res = ::RegEnumKeyEx(hkFrom, i, lpstrName, &nNameSize, 0, lpstrClass, &nClassSize, 0); if (ERROR_NO_MORE_ITEMS == res) { break; } HKEY hkNew; res = ::RegCreateKeyEx(hkTo, lpstrName, 0, lpstrClass, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hkNew, 0); ; if (ERROR_SUCCESS != res) { return FALSE; } ::RegCloseKey(hkNew); BOOL bRes = CopyRegistryKey(hkFrom, lpstrName, hkTo, lpstrName); if (! bRes) { return FALSE; } } return TRUE; }
// Moves one registry key to another position, including subkeys bool LRegistry::MoveRegistryKey(HKEY hkFrom, LPCTSTR szFrom, HKEY hkTo, LPCTSTR szTo) { bool success = CopyRegistryKey(hkFrom, szFrom, hkTo, szTo); if (success) DeleteRegistryKey(hkFrom, szFrom); return success; }
BOOL CRegKey2::CopyKey(HKEY hkRootFrom, const CString& sFromPath, HKEY hkRootTo, const CString& sToPath) { return CopyRegistryKey(hkRootFrom, sFromPath, hkRootTo, sToPath); }
// Copies one registry key to another position, including subkeys bool LRegistry::CopyRegistryKey(HKEY hkFromMajor, LPCTSTR szFrom, HKEY hkToMajor, LPCTSTR szTo) { HKEY hKeyFrom = NULL; HKEY hKeyTo = NULL; LONG res = RegOpenKeyEx(hkFromMajor, szFrom, 0, KEY_READ, &hKeyFrom); if (res == ERROR_SUCCESS) { // Create the new key, or open it if it already exists. DWORD dwDisp = 0; res = RegCreateKeyEx(hkToMajor, szTo, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyTo, &dwDisp); } if (res == ERROR_SUCCESS) { // First copy all the values in the current key DWORD dwIndex = 0; LONG res2 = ERROR_SUCCESS; while (ERROR_SUCCESS == res2 && res == ERROR_SUCCESS) { TCHAR szValueName[1024]; DWORD dwValueNameSize = 1024; DWORD dwType = 0; LPBYTE pData = NULL; DWORD dwDataSize = 0; // query necessary size for pData // enumerates only the values not the (possible) sub-keys res2 = RegEnumValue(hKeyFrom, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, NULL, &dwDataSize); if (res2 == ERROR_SUCCESS) { dwValueNameSize = 1024; if (dwDataSize != 0) pData = new BYTE[dwDataSize]; // actually fill pData: res2 = RegEnumValue(hKeyFrom, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, pData, &dwDataSize); } if (res2 == ERROR_SUCCESS) { res2 = RegSetValueEx(hKeyTo, szValueName, 0, dwType, pData, dwDataSize); } if (pData) delete[] pData; pData = NULL; dwIndex++; } // Then call recursively for all subkeys in the current key dwIndex = 0; res2 = ERROR_SUCCESS; while (res2 == ERROR_SUCCESS) { TCHAR szSubKey[1024]; DWORD dwSubKeySize = 1024; TCHAR szClass[1024]; DWORD dwClassSize = 1024; FILETIME fileTime; res2 = RegEnumKeyEx(hKeyFrom, dwIndex, szSubKey, &dwSubKeySize, NULL, szClass, &dwClassSize, &fileTime); if (res2 == ERROR_SUCCESS) { res = CopyRegistryKey(hKeyFrom, szSubKey, hKeyTo, szSubKey); } dwIndex++; } } if (hKeyFrom) RegCloseKey(hKeyFrom); if (hKeyTo) RegCloseKey(hKeyTo); return (res == ERROR_SUCCESS); }