static BOOL LoadHive(HWND hWnd) { OPENFILENAME ofn; WCHAR Caption[128]; LPCWSTR pszKeyPath; WCHAR xPath[LOADHIVE_KEYNAMELENGTH]; HKEY hRootKey; WCHAR Filter[1024]; FILTERPAIR filter; /* get the item key to load the hive in */ pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey); /* initialize the "open file" dialog */ InitOpenFileName(hWnd, &ofn); /* build the "All Files" filter up */ filter.DisplayID = IDS_FLT_ALLFILES; filter.FilterID = IDS_FLT_ALLFILES_FLT; BuildFilterStrings(Filter, &filter, 1); ofn.lpstrFilter = Filter; /* load and set the caption and flags for dialog */ LoadStringW(hInst, IDS_LOAD_HIVE, Caption, COUNT_OF(Caption)); ofn.lpstrTitle = Caption; ofn.Flags |= OFN_ENABLESIZING; /* ofn.lCustData = ;*/ /* now load the hive */ if (GetOpenFileName(&ofn)) { if (DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_LOADHIVE), hWnd, &LoadHive_KeyNameInHookProc, (LPARAM)xPath)) { LONG regLoadResult; /* Enable the 'restore' privilege, load the hive, disable the privilege */ EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE); regLoadResult = RegLoadKeyW(hRootKey, xPath, ofn.lpstrFile); EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE); if(regLoadResult == ERROR_SUCCESS) { /* refresh tree and list views */ RefreshTreeView(g_pChildWnd->hTreeWnd); pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey); RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath); } else { ErrorMessageBox(hWnd, Caption, regLoadResult); return FALSE; } } } else { CheckCommDlgError(hWnd); } return TRUE; }
BOOL ExportRegistryFile(HWND hWnd) { OPENFILENAME ofn; TCHAR ExportKeyPath[_MAX_PATH]; TCHAR Caption[128]; HKEY hKeyRoot; LPCTSTR 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); LoadString(hInst, IDS_EXPORT_REG_FILE, Caption, sizeof(Caption)/sizeof(TCHAR)); 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 = MAKEINTRESOURCE(IDD_EXPORTRANGE); if (GetSaveFileName(&ofn)) { BOOL result; DWORD format; if (ofn.nFilterIndex == 1) format = REG_FORMAT_5; else format = REG_FORMAT_4; result = export_registry_key(ofn.lpstrFile, ExportKeyPath, format); if (!result) { LPSTR p = GetMultiByteString(ofn.lpstrFile); fprintf(stderr, "Can't open file \"%s\"\n", p); HeapFree(GetProcessHeap(), 0, p); return FALSE; } } else { CheckCommDlgError(hWnd); } return TRUE; }
static BOOL ImportRegistryFile(HWND hWnd) { OPENFILENAME ofn; TCHAR Caption[128], szTitle[256], szText[256]; LPCTSTR pszKeyPath; HKEY hRootKey; InitOpenFileName(hWnd, &ofn); LoadString(hInst, IDS_IMPORT_REG_FILE, Caption, COUNT_OF(Caption)); ofn.lpstrTitle = Caption; ofn.Flags |= OFN_ENABLESIZING; /* ofn.lCustData = ;*/ if (GetOpenFileName(&ofn)) { FILE *fp = _wfopen(ofn.lpstrFile, L"r"); if (fp == NULL || !import_registry_file(fp)) { LPSTR p = GetMultiByteString(ofn.lpstrFile); fprintf(stderr, "Can't open file \"%s\"\n", p); HeapFree(GetProcessHeap(), 0, p); if (fp != NULL) fclose(fp); return FALSE; } LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle)); LoadString(hInst, IDS_IMPORTED_OK, szText, sizeof(szTitle)); /* show successful import */ MessageBox(NULL, szText, szTitle, MB_OK); fclose(fp); } else { CheckCommDlgError(hWnd); } RefreshTreeView(g_pChildWnd->hTreeWnd); pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey); RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath); return TRUE; }
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; }
static BOOL ImportRegistryFile(HWND hWnd) { BOOL bRet = FALSE; OPENFILENAME ofn; WCHAR Caption[128], szTitle[512], szText[512]; HKEY hKeyRoot; LPCWSTR pszKeyPath; /* Figure out in which key path we are importing */ pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot); InitOpenFileName(hWnd, &ofn); LoadStringW(hInst, IDS_IMPORT_REG_FILE, Caption, COUNT_OF(Caption)); ofn.lpstrTitle = Caption; ofn.Flags |= OFN_ENABLESIZING; /* ofn.lCustData = ;*/ if (GetOpenFileName(&ofn)) { /* Look at the extension of the file to determine its type */ if (ofn.nFileExtension >= 1 && _wcsicmp(ofn.lpstrFile + ofn.nFileExtension, L"reg") == 0) /* REGEDIT4 or Windows Registry Editor Version 5.00 */ { /* Open the file */ FILE* fp = _wfopen(ofn.lpstrFile, L"r"); /* Import it */ if (fp == NULL || !import_registry_file(fp)) { /* Error opening the file */ LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle)); LoadStringW(hInst, IDS_IMPORT_ERROR, szText, COUNT_OF(szText)); InfoMessageBox(hWnd, MB_OK | MB_ICONERROR, szTitle, szText, ofn.lpstrFile); bRet = FALSE; } else { /* Show successful import */ LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle)); LoadStringW(hInst, IDS_IMPORT_OK, szText, COUNT_OF(szText)); InfoMessageBox(hWnd, MB_OK | MB_ICONINFORMATION, szTitle, szText, ofn.lpstrFile); bRet = TRUE; } /* Close the file */ if (fp) fclose(fp); } else /* Registry Hive Files */ { LoadStringW(hInst, IDS_QUERY_IMPORT_HIVE_CAPTION, szTitle, COUNT_OF(szTitle)); LoadStringW(hInst, IDS_QUERY_IMPORT_HIVE_MSG, szText, COUNT_OF(szText)); /* Display a confirmation message */ if (MessageBoxW(g_pChildWnd->hWnd, szText, szTitle, MB_ICONWARNING | MB_YESNO) == IDYES) { LONG lResult; HKEY hSubKey; /* Open the subkey */ lResult = RegOpenKeyExW(hKeyRoot, pszKeyPath, 0, KEY_WRITE, &hSubKey); if (lResult == ERROR_SUCCESS) { /* Enable the 'restore' privilege, restore the hive then disable the privilege */ EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE); lResult = RegRestoreKey(hSubKey, ofn.lpstrFile, REG_FORCE_RESTORE); EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE); /* Flush the subkey and close it */ RegFlushKey(hSubKey); RegCloseKey(hSubKey); } /* Set the return value */ bRet = (lResult == ERROR_SUCCESS); /* Display error, if any */ if (!bRet) ErrorMessageBox(hWnd, Caption, lResult); } } } else { CheckCommDlgError(hWnd); } /* refresh tree and list views */ RefreshTreeView(g_pChildWnd->hTreeWnd); pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot); RefreshListView(g_pChildWnd->hListWnd, hKeyRoot, pszKeyPath); return bRet; }