BOOL My_RegSaveKeyW() { HKEY hKey=NULL; LPCWSTR lpFile=NULL; LPSECURITY_ATTRIBUTES lpSecurityAttributes=NULL; LONG returnVal_Real = NULL; LONG returnVal_Intercepted = NULL; DWORD error_Real = 0; DWORD error_Intercepted = 0; disableInterception(); returnVal_Real = RegSaveKeyW (hKey,lpFile,lpSecurityAttributes); error_Real = GetLastError(); enableInterception(); returnVal_Intercepted = RegSaveKeyW (hKey,lpFile,lpSecurityAttributes); error_Intercepted = GetLastError(); return ((returnVal_Real == returnVal_Intercepted) && (error_Real == error_Intercepted)); }
BOOL RegistryOp::SaveKey(LPWSTR lpFileName) { assert(m_hKey); assert(lpFileName); long lReturn = RegSaveKeyW(m_hKey, lpFileName, NULL); if (lReturn == ERROR_SUCCESS) return TRUE; DOLOG("RegSaveKeyW ERROR return:" + lReturn); return FALSE; }
LONG Win32U_RegSaveKey(HKEY keyName, // IN: LPCSTR keyFile, // IN: LPSECURITY_ATTRIBUTES attr) // IN: { LONG ret; utf16_t *keyFileW = Unicode_GetAllocUTF16(keyFile); ret = RegSaveKeyW(keyName, keyFileW, attr); free(keyFileW); return ret; }
BOOL ExportRegistryFile(HWND hWnd) { BOOL bRet = FALSE; OPENFILENAME ofn; WCHAR ExportKeyPath[_MAX_PATH] = {0}; WCHAR Caption[128], szTitle[512], szText[512]; HKEY hKeyRoot; LPCWSTR pszKeyPath; /* Figure out which key path we are exporting */ pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot); GetKeyName(ExportKeyPath, COUNT_OF(ExportKeyPath), hKeyRoot, pszKeyPath); InitOpenFileName(hWnd, &ofn); LoadStringW(hInst, IDS_EXPORT_REG_FILE, Caption, COUNT_OF(Caption)); ofn.lpstrTitle = Caption; /* Only set the path if a key (not the root node) is selected */ if (hKeyRoot != 0) { ofn.lCustData = (LPARAM) ExportKeyPath; } ofn.Flags = OFN_ENABLETEMPLATE | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_OVERWRITEPROMPT; ofn.lpfnHook = ExportRegistryFile_OFNHookProc; ofn.lpTemplateName = MAKEINTRESOURCEW(IDD_EXPORTRANGE); if (GetSaveFileName(&ofn)) { switch (ofn.nFilterIndex) { case 2: /* Registry Hive Files */ { LONG lResult; HKEY hSubKey; /* Open the subkey */ lResult = RegOpenKeyExW(hKeyRoot, pszKeyPath, 0, KEY_READ, &hSubKey); if (lResult == ERROR_SUCCESS) { /* Enable the 'backup' privilege, save the hive then disable the privilege */ EnablePrivilege(SE_BACKUP_NAME, NULL, TRUE); lResult = RegSaveKeyW(hSubKey, ofn.lpstrFile, NULL); if (lResult == ERROR_ALREADY_EXISTS) { /* * We are here, that means that we already said "yes" to the confirmation dialog. * So we absolutely want to replace the hive file. */ if (DeleteFileW(ofn.lpstrFile)) { /* Try again */ lResult = RegSaveKeyW(hSubKey, ofn.lpstrFile, NULL); } } EnablePrivilege(SE_BACKUP_NAME, NULL, FALSE); if (lResult != ERROR_SUCCESS) { /* * If we are here, it's because RegSaveKeyW has failed for any reason. * The problem is that even if it has failed, it has created or * replaced the exported hive file with a new empty file. We don't * want to keep this file, so we delete it. */ DeleteFileW(ofn.lpstrFile); } /* Close the subkey */ RegCloseKey(hSubKey); } /* Set the return value */ bRet = (lResult == ERROR_SUCCESS); /* Display error, if any */ if (!bRet) ErrorMessageBox(hWnd, Caption, lResult); break; } case 1: /* Windows Registry Editor Version 5.00 */ case 3: /* REGEDIT4 */ default: /* All files ==> use Windows Registry Editor Version 5.00 */ { if (!export_registry_key(ofn.lpstrFile, ExportKeyPath, (ofn.nFilterIndex == 3 ? REG_FORMAT_4 : REG_FORMAT_5))) { /* Error creating the file */ LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle)); LoadStringW(hInst, IDS_EXPORT_ERROR, szText, COUNT_OF(szText)); InfoMessageBox(hWnd, MB_OK | MB_ICONERROR, szTitle, szText, ofn.lpstrFile); bRet = FALSE; } else { bRet = TRUE; } break; } } } else { CheckCommDlgError(hWnd); } return bRet; }