Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. rtrestor.c
  5. Abstract:
  6. NT level registry api test program, basic non-error paths.
  7. Perform an NtRestoreKey call to load part of the registry from a file.
  8. rtrestor <KeyPath> <FileName>
  9. Example:
  10. rtrestor \registry\machine\user userfile.rd
  11. Author:
  12. Bryan Willman (bryanwi) 24-Jan-92
  13. Revision History:
  14. --*/
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include "cmp.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #define WORK_SIZE 1024
  23. void __cdecl main(int, char *);
  24. void processargs();
  25. UNICODE_STRING KeyPath;
  26. WCHAR KeyPathBuffer[WORK_SIZE];
  27. UNICODE_STRING FileName;
  28. WCHAR FileNameBuffer[WORK_SIZE];
  29. BOOLEAN HiveVolatile;
  30. void
  31. __cdecl main(
  32. int argc,
  33. char *argv[]
  34. )
  35. {
  36. NTSTATUS status;
  37. OBJECT_ATTRIBUTES ObjectAttributes;
  38. IO_STATUS_BLOCK IoStatus;
  39. HANDLE FileHandle;
  40. HANDLE KeyHandle;
  41. BOOLEAN WasEnabled;
  42. //
  43. // Process args
  44. //
  45. KeyPath.MaximumLength = WORK_SIZE;
  46. KeyPath.Length = 0L;
  47. KeyPath.Buffer = &(KeyPathBuffer[0]);
  48. FileName.MaximumLength = WORK_SIZE;
  49. FileName.Length = 0L;
  50. FileName.Buffer = &(FileNameBuffer[0]);
  51. processargs(argc, argv);
  52. //
  53. // Set up and open FileName
  54. //
  55. printf("rtrestor: starting\n");
  56. InitializeObjectAttributes(
  57. &ObjectAttributes,
  58. &FileName,
  59. 0,
  60. (HANDLE)NULL,
  61. NULL
  62. );
  63. ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
  64. status = NtCreateFile(
  65. &FileHandle,
  66. GENERIC_READ | SYNCHRONIZE,
  67. &ObjectAttributes,
  68. &IoStatus,
  69. 0, // AllocationSize
  70. FILE_ATTRIBUTE_NORMAL,
  71. 0, // ShareAccess
  72. FILE_OPEN_IF,
  73. FILE_SYNCHRONOUS_IO_NONALERT,
  74. NULL, // EaBuffer
  75. 0 // EaLength
  76. );
  77. if (!NT_SUCCESS(status)) {
  78. printf("rtsave: file open failed status = %08lx\n", status);
  79. exit(1);
  80. }
  81. InitializeObjectAttributes(
  82. &ObjectAttributes,
  83. &KeyPath,
  84. 0,
  85. (HANDLE)NULL,
  86. NULL
  87. );
  88. ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
  89. status = NtOpenKey(
  90. &KeyHandle,
  91. KEY_READ,
  92. &ObjectAttributes
  93. );
  94. if (!NT_SUCCESS(status)) {
  95. printf("rtsave: key open failed status = %08lx\n", status);
  96. exit(1);
  97. }
  98. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, TRUE, FALSE, &WasEnabled);
  99. if (HiveVolatile) {
  100. status = NtRestoreKey(KeyHandle, FileHandle, REG_WHOLE_HIVE_VOLATILE);
  101. } else {
  102. status = NtRestoreKey(KeyHandle, FileHandle, 0);
  103. }
  104. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasEnabled, FALSE, &WasEnabled);
  105. if (!NT_SUCCESS(status)) {
  106. printf("rtrestor: NtRestorKey failed status = %08lx\n", status);
  107. exit(1);
  108. }
  109. printf("rtsave: success\n");
  110. exit(0);
  111. }
  112. void
  113. processargs(
  114. int argc,
  115. char *argv[]
  116. )
  117. {
  118. ANSI_STRING temp;
  119. UNICODE_STRING DosFileName;
  120. if ( (argc < 3) || (argc > 4) )
  121. {
  122. printf("Usage: %s <KeyName> <FileName> [VOLATILE]\n",
  123. argv[0]);
  124. exit(1);
  125. }
  126. RtlInitAnsiString(
  127. &temp,
  128. argv[1]
  129. );
  130. RtlAnsiStringToUnicodeString(
  131. &KeyPath,
  132. &temp,
  133. TRUE
  134. );
  135. RtlInitAnsiString(
  136. &temp,
  137. argv[2]
  138. );
  139. RtlAnsiStringToUnicodeString(
  140. &DosFileName,
  141. &temp,
  142. TRUE
  143. );
  144. RtlDosPathNameToNtPathName_U( DosFileName.Buffer,
  145. &FileName,
  146. NULL,
  147. NULL);
  148. if ((argc==4) && (_stricmp(argv[3],"volatile")==0)) {
  149. HiveVolatile = TRUE;
  150. } else {
  151. HiveVolatile = FALSE;
  152. }
  153. return;
  154. }