LONG RenameKey(HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpNewName) { LPCTSTR s; LPTSTR lpNewSubKey = NULL; LONG Ret = 0; if (!lpSubKey) return Ret; s = _tcsrchr(lpSubKey, _T('\\')); if (s) { s++; lpNewSubKey = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (s - lpSubKey + _tcslen(lpNewName) + 1) * sizeof(TCHAR)); if (lpNewSubKey != NULL) { memcpy(lpNewSubKey, lpSubKey, (s - lpSubKey) * sizeof(TCHAR)); lstrcpy(lpNewSubKey + (s - lpSubKey), lpNewName); lpNewName = lpNewSubKey; } else return ERROR_NOT_ENOUGH_MEMORY; } Ret = MoveKey(hKey, lpNewName, hKey, lpSubKey); if (lpNewSubKey) { HeapFree(GetProcessHeap(), 0, lpNewSubKey); } return Ret; }
void CLAItem::Resize(int new_len) { VERIFY((new_len>=1)); if (new_len!=iFrameCount){ if (new_len>iFrameCount){ int old_len=iFrameCount; iFrameCount = new_len; MoveKey(old_len,new_len); }else{ KeyPairIt I = Keys.upper_bound(new_len-1); if (I!=Keys.end()) Keys.erase(I, Keys.end()); iFrameCount = new_len; } } }
// Move a key, it's subkeys and all values bool RegKey::MoveKey(LPCTSTR pszSourceKey, LPCTSTR pszDestKey, HKEY hBaseKey /* = HKEY_CURRENT_USER */) { if (pszSourceKey == NULL || pszDestKey == NULL) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } HKEY hSource = NULL; HKEY hDest = NULL; // Open the source key LONG lRetValue = RegOpenKeyEx(hBaseKey, pszSourceKey, NULL, KEY_ALL_ACCESS, &hSource); if (lRetValue != ERROR_SUCCESS) { iLastErrorCode_ = GetLastError(); return false; } // Open/Create the destination key DWORD dwDisp; // Disposition lRetValue = RegCreateKeyEx(hBaseKey, pszDestKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hDest, &dwDisp); if (lRetValue != ERROR_SUCCESS) { iLastErrorCode_ = GetLastError(); return false; } // We have opened and created all appropriate keys // Enumerate all values in hSource, and recreate in hDest TCHAR szValueName[_MAX_PATH]; DWORD dwValueBuffer = _MAX_PATH; DWORD dwIndex = 0; DWORD dwValueType; DWORD dwValueLength = 0; bool bContinue = true; // Deal with the values first while (bContinue) { dwValueBuffer = _MAX_PATH; dwValueLength = 1; if (RegEnumValue(hSource, dwIndex, szValueName, &dwValueBuffer, NULL, &dwValueType, NULL, &dwValueLength) == ERROR_SUCCESS) { LPBYTE pValue = (LPBYTE) new TCHAR[dwValueLength + 1]; // Allocate memory for the data if (pValue) { DWORD dwRealDataLen = dwValueLength + 1; if (RegQueryValueEx(hSource, szValueName, NULL, NULL, pValue, &dwRealDataLen) == ERROR_SUCCESS) { if (RegSetValueEx(hDest, szValueName, NULL, dwValueType, pValue, dwValueLength) != ERROR_SUCCESS) { delete[] pValue; pValue = NULL; iLastErrorCode_ = GetLastError(); return false; } } else { delete[] pValue; pValue = NULL; iLastErrorCode_ = GetLastError(); return false; } delete[] pValue; pValue = NULL; dwIndex++; } } else bContinue = false; } // Enumerate all subkeys in hSource, and recreate in hDest bContinue = true; dwIndex = 0; while (bContinue) { if (RegEnumKey(hSource, dwIndex, szValueName, _MAX_PATH) == ERROR_SUCCESS) { TCHAR szSourceKey[_MAX_PATH] = { '\0' }; TCHAR szDestKey[_MAX_PATH] = { '\0' }; _tcscpy_s(szSourceKey, _MAX_PATH, pszSourceKey); _tcscpy_s(szDestKey, _MAX_PATH, pszDestKey); _tcscat_s(szSourceKey, _MAX_PATH, _T("\\")); _tcscat_s(szSourceKey, _MAX_PATH, szValueName); _tcscat_s(szDestKey, _MAX_PATH, _T("\\")); _tcscat_s(szDestKey, _MAX_PATH, szValueName); if (!MoveKey(szSourceKey, szDestKey, hBaseKey)) dwIndex++; } else bContinue = false; } // Now delete the original key and it's values and subkeys RegCloseKey(hSource); RegCloseKey(hDest); QuickDeleteKey(pszSourceKey, hBaseKey); iLastErrorCode_ = ERROR_SUCCESS; return true; }