BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s) { TCHAR filename[MAX_PATH]; TCHAR reg_key_name[KEY_MAX_LEN]; switch (action) { case ACTION_ADD: get_file_name(&s, filename, MAX_PATH); if (!filename[0]) { printf("No file name is specified\n%s", usage); return FALSE; //exit(1); } while (filename[0]) { if (!import_registry_file(filename)) { perror(""); printf("Can't open file \"%s\"\n", filename); return FALSE; //exit(1); } get_file_name(&s, filename, MAX_PATH); } break; case ACTION_DELETE: get_file_name(&s, reg_key_name, KEY_MAX_LEN); if (!reg_key_name[0]) { printf("No registry key is specified for removal\n%s", usage); return FALSE; //exit(1); } delete_registry_key(reg_key_name); break; case ACTION_EXPORT: filename[0] = '\0'; get_file_name(&s, filename, MAX_PATH); if (!filename[0]) { printf("No file name is specified\n%s", usage); return FALSE; //exit(1); } if (s[0]) { get_file_name(&s, reg_key_name, KEY_MAX_LEN); export_registry_key(filename, reg_key_name); } else { export_registry_key(filename, NULL); } break; default: printf("Unhandled action!\n"); return FALSE; } 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; }
static BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s) { switch (action) { case ACTION_ADD: { CHAR filename[MAX_PATH]; FILE *reg_file; get_file_name(&s, filename); if (!filename[0]) { fprintf(stderr,"%s: No file name was specified\n", getAppName()); fprintf(stderr,usage); exit(1); } while(filename[0]) { char* realname = NULL; if (strcmp(filename, "-") == 0) { reg_file=stdin; } else { int size; size=SearchPath(NULL, filename, NULL,0, NULL, NULL); if (size>0) { realname=HeapAlloc(GetProcessHeap(), 0, size); size=SearchPath(NULL, filename, NULL, size, realname, NULL); } if (size==0) { fprintf(stderr, "%s: File not found \"%s\" (%d)\n", getAppName(), filename, GetLastError()); exit(1); } reg_file = fopen(realname, "r"); if (reg_file==NULL) { perror(""); fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), filename); exit(1); } } import_registry_file(reg_file); if (realname) { HeapFree(GetProcessHeap(),0,realname); fclose(reg_file); } get_file_name(&s, filename); } break; } case ACTION_DELETE: { CHAR reg_key_name[KEY_MAX_LEN]; get_file_name(&s, reg_key_name); if (!reg_key_name[0]) { fprintf(stderr,"%s: No registry key was specified for removal\n", getAppName()); fprintf(stderr,usage); exit(1); } else { WCHAR* reg_key_nameW = GetWideString(reg_key_name); delete_registry_key(reg_key_nameW); HeapFree(GetProcessHeap(), 0, reg_key_nameW); } break; } case ACTION_EXPORT: { CHAR filename[MAX_PATH]; WCHAR* filenameW; filename[0] = '\0'; get_file_name(&s, filename); if (!filename[0]) { fprintf(stderr,"%s: No file name was specified\n", getAppName()); fprintf(stderr,usage); exit(1); } filenameW = GetWideString(filename); if (s[0]) { CHAR reg_key_name[KEY_MAX_LEN]; WCHAR* reg_key_nameW; get_file_name(&s, reg_key_name); reg_key_nameW = GetWideString(reg_key_name); export_registry_key(filenameW, reg_key_nameW, REG_FORMAT_4); HeapFree(GetProcessHeap(), 0, reg_key_nameW); } else { export_registry_key(filenameW, NULL, REG_FORMAT_4); } HeapFree(GetProcessHeap(), 0, filenameW); break; } default: fprintf(stderr,"%s: Unhandled action!\n", getAppName()); exit(1); break; } return TRUE; }
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; }
BOOL PerformRegAction(REGEDIT_ACTION action, LPWSTR s, BOOL silent) { switch (action) { case ACTION_ADD: { WCHAR szTitle[512], szText[512]; WCHAR filename[MAX_PATH]; FILE *fp; get_file_name(&s, filename); if (!filename[0]) { InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, L"No file name is specified."); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, usage); exit(4); } LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle)); while (filename[0]) { /* Request import confirmation */ if (!silent) { int choice; LoadStringW(hInst, IDS_IMPORT_PROMPT, szText, COUNT_OF(szText)); choice = InfoMessageBox(NULL, MB_YESNOCANCEL | MB_ICONWARNING, szTitle, szText, filename); switch (choice) { case IDNO: goto cont; case IDCANCEL: /* The cancel case is useful if the user is importing more than one registry file at a time, and wants to back out anytime during the import process. This way, the user doesn't have to resort to ending the regedit process abruptly just to cancel the operation. */ return TRUE; default: break; } } /* Open the file */ fp = _wfopen(filename, L"r"); /* Import it */ if (fp == NULL || !import_registry_file(fp)) { /* Error opening the file */ if (!silent) { LoadStringW(hInst, IDS_IMPORT_ERROR, szText, COUNT_OF(szText)); InfoMessageBox(NULL, MB_OK | MB_ICONERROR, szTitle, szText, filename); } } else { /* Show successful import */ if (!silent) { LoadStringW(hInst, IDS_IMPORT_OK, szText, COUNT_OF(szText)); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, szTitle, szText, filename); } } /* Close the file */ if (fp) fclose(fp); cont: get_file_name(&s, filename); } break; } case ACTION_DELETE: { WCHAR reg_key_name[KEY_MAX_LEN]; get_file_name(&s, reg_key_name); if (!reg_key_name[0]) { InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, L"No registry key is specified for removal."); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, usage); exit(6); } delete_registry_key(reg_key_name); break; } case ACTION_EXPORT: { WCHAR filename[MAX_PATH]; filename[0] = L'\0'; get_file_name(&s, filename); if (!filename[0]) { InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, L"No file name is specified."); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, usage); exit(7); } if (s[0]) { WCHAR reg_key_name[KEY_MAX_LEN]; get_file_name(&s, reg_key_name); export_registry_key(filename, reg_key_name, REG_FORMAT_4); } else { export_registry_key(filename, NULL, REG_FORMAT_4); } break; } default: fwprintf(stderr, L"%s: Unhandled action!\n", getAppName()); exit(8); break; } return TRUE; }