LONG DeleteSubKey(HKEY hKey,LPCSTR szKey) { HKEY hSubKey; DWORD cb; char szSubKey[200]; FILETIME ft; LONG lRet=RegOpenKeyEx(hKey,szKey,0, KEY_ENUMERATE_SUB_KEYS|KEY_SET_VALUE,&hSubKey); if (lRet!=ERROR_SUCCESS) return lRet; for (;;) { cb=200; if (RegEnumKeyEx(hSubKey,0,szSubKey,&cb,NULL, NULL,NULL,&ft)==ERROR_NO_MORE_ITEMS) break; DeleteSubKey(hSubKey,szSubKey); } RegCloseKey(hSubKey); RegDeleteKey(hKey,szKey); return ERROR_SUCCESS; }
void THierarchicalStorage::RecursiveDeleteSubKey(const UnicodeString & Key) { if (OpenSubKey(Key, false)) { ClearSubKeys(); CloseSubKey(); } DeleteSubKey(Key); }
//RecurseDeleteKey is necessary because on NT RegDeleteKey doesn't work if the //specified key has subkeys void CRegKey::RecurseDeleteKey(LPCTSTR lpszKey) { CRegKey key; if (!key.Open(m_hKey, lpszKey)) return; FILETIME time; TCHAR szBuffer[256]; DWORD dwSize = 256; while (RegEnumKeyEx(key.m_hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time)==ERROR_SUCCESS) { key.RecurseDeleteKey(szBuffer); dwSize = 256; } key.Close(); DeleteSubKey(lpszKey); }
LONG CPropertyArchiveRegistry::RecurseDeleteKey(LPCTSTR lpszKey) { CPropertyArchiveRegistry key; LONG lRes = key.Open(GetKey(), lpszKey, KEY_READ | KEY_WRITE); if (lRes != ERROR_SUCCESS) return lRes; FILETIME time; DWORD dwSize = 256; TCHAR szBuffer[256]; while (RegEnumKeyEx(key.GetKey(), 0, szBuffer, &dwSize, NULL, NULL, NULL, &time)==ERROR_SUCCESS) { lRes = key.RecurseDeleteKey(szBuffer); if (lRes != ERROR_SUCCESS) return lRes; dwSize = 256; } key.Close(); return DeleteSubKey(lpszKey); }
LONG CKey::RecurseDeleteKey(LPCTSTR subKeyName) throw() { CKey key; LONG res = key.Open(_object, subKeyName, KEY_READ | KEY_WRITE); if (res != ERROR_SUCCESS) return res; FILETIME fileTime; const UInt32 kBufferSize = MAX_PATH + 1; // 256 in ATL DWORD size = kBufferSize; TCHAR buffer[kBufferSize]; while (RegEnumKeyEx(key._object, 0, buffer, &size, NULL, NULL, NULL, &fileTime) == ERROR_SUCCESS) { res = key.RecurseDeleteKey(buffer); if (res != ERROR_SUCCESS) return res; size = kBufferSize; } key.Close(); return DeleteSubKey(subKeyName); }
void RemoveSettings(HWND hWnd) { char szTitle[100]; char szMsg[200]; LoadString(hInst,IDS_REMOVESETTINGS,szTitle,100); LoadString(hInst,IDS_WANTBACKUP,szMsg,200); int nRet=MessageBox(hWnd,szMsg,szTitle,MB_ICONQUESTION|MB_YESNOCANCEL); if (nRet==IDCANCEL) return; if (nRet==IDYES) { if (!SaveSettings(hWnd)) return; } HKEY hRegKey; LONG lRet=RegOpenKeyEx(HKEY_CURRENT_USER,"SOFTWARE", 0,KEY_ALL_ACCESS|DELETE,&hRegKey); if (lRet!=ERROR_SUCCESS) { ShowError(hWnd,IDS_ERRORCANNOTOPENKEY2,lRet); return; } lRet=DeleteSubKey(hRegKey,"Update"); RegCloseKey(hRegKey); if (lRet!=ERROR_SUCCESS) { ShowError(hWnd,IDS_ERRORCANNOTDELETEKEY,lRet); return; } }
BOOL RestoreSettings(HWND hWnd) { // Check whether locate32 is running HWND hLocateSTWindow=FindWindow("LOCATEAPPST",NULL); if (hLocateSTWindow!=NULL) { char szText[100]; LoadString(hInst,IDS_LOCATE32RUNNING,szText,100); if (MessageBox(hWnd,szText,NULL,MB_OKCANCEL|MB_ICONINFORMATION)) return FALSE; } char szPath[MAX_PATH]=""; char szTitle[100],szFilter[200]; OSVERSIONINFO ve; ve.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); if (GetVersionEx(&ve)) { if (ve.dwPlatformId==VER_PLATFORM_WIN32_NT) LoadString(hInst,IDS_RESTOREFILTER,szFilter,200); else LoadString(hInst,IDS_RESTOREFILTER9x,szFilter,200); } else LoadString(hInst,IDS_RESTOREFILTER,szFilter,200); LoadString(hInst,IDS_RESTORESETTINGS,szTitle,100); for (int i=0;szFilter[i]!='\0';i++) { if (szFilter[i]=='|') szFilter[i]='\0'; } OPENFILENAME ofn; ZeroMemory(&ofn,sizeof(OPENFILENAME)); ofn.lStructSize=OPENFILENAME_SIZE_VERSION_400; ofn.hwndOwner=hWnd; ofn.hInstance=hInst; ofn.lpstrFilter=szFilter; ofn.lpstrFile=szPath; ofn.nMaxFile=MAX_PATH; ofn.lpstrTitle=szTitle; ofn.Flags=OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_LONGNAMES|OFN_HIDEREADONLY; ofn.lpstrDefExt="*.reg"; if (!GetOpenFileName(&ofn)) return FALSE; int nDotIndex; for (nDotIndex=(int)strlen(szPath)-1;nDotIndex>=0 && szPath[nDotIndex]!='.';nDotIndex--); if (nDotIndex>=0 && _stricmp(szPath+nDotIndex+1,"reg")==0) { char szBackup[MAX_PATH]; CopyMemory(szBackup,szPath,nDotIndex+1); strcpy_s(szBackup+nDotIndex+1,MAX_PATH-nDotIndex-1,"old.reg"); // Backing up char szCommand[2000]; sprintf_s(szCommand,2000,"regedit /ea \"%s\" HKEY_CURRENT_USER\\Software\\Update",szBackup); PROCESS_INFORMATION pi; STARTUPINFO si; // Ansi and Unicode versions are same ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO); if (CreateProcess(NULL,szCommand,NULL, NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS, NULL,NULL,&si,&pi)) { WaitForSingleObject(pi.hProcess,2000); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } else ShowError(hWnd,IDS_ERRORCANNOTRUNREGEDIT,GetLastError()); // Restore key DeleteSubKey(HKEY_CURRENT_USER,"SOFTWARE\\Update"); sprintf_s(szCommand,2000,"regedit /s \"%s\"",szPath); ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO); if (CreateProcess(NULL,szCommand,NULL, NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS, NULL,NULL,&si,&pi)) { CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } else ShowError(hWnd,IDS_ERRORCANNOTRUNREGEDIT,GetLastError()); return TRUE; } // First, check that we can restore key HKEY hRegKey; LONG lRet=RegCreateKeyEx(HKEY_CURRENT_USER,"SOFTWARE\\Update_tmpTmp", 0,NULL,REG_OPTION_BACKUP_RESTORE,KEY_ALL_ACCESS,NULL,&hRegKey,NULL); if (lRet!=ERROR_SUCCESS) { ShowError(hWnd,IDS_ERRORCANNOTCREATEKEY,lRet); return FALSE; } lRet=RegRestoreKey(hRegKey,szPath,0); RegCloseKey(hRegKey); DeleteSubKey(HKEY_CURRENT_USER,"SOFTWARE\\Update_tmpTmp"); if (lRet!=ERROR_SUCCESS) { ShowError(hWnd,IDS_ERRORCANNOTRESTOREKEY,lRet); return FALSE; } // Clear existing key DeleteSubKey(HKEY_CURRENT_USER,"SOFTWARE\\Update"); // Restore key lRet=RegCreateKeyEx(HKEY_CURRENT_USER,"SOFTWARE\\Update", 0,NULL,REG_OPTION_BACKUP_RESTORE,KEY_ALL_ACCESS,NULL,&hRegKey,NULL); if (lRet!=ERROR_SUCCESS) { ShowError(hWnd,IDS_ERRORCANNOTCREATEKEY,lRet); return FALSE; } lRet=RegRestoreKey(hRegKey,szPath,0); RegCloseKey(hRegKey); if (lRet!=ERROR_SUCCESS) { ShowError(hWnd,IDS_ERRORCANNOTRESTOREKEY,lRet); return FALSE; } return TRUE; }