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.

198 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. rtreplac.c
  5. Abstract:
  6. NT level registry api test program, basic non-error paths.
  7. Perform an NtReplaceKey call to replace a registry hive.
  8. rtreplac <KeyPath> <NewHiveName> <BackupOldHiveName>
  9. Example:
  10. rtreplac \registry\machine\user newuser olduser
  11. Author:
  12. John Vert (jvert) 8-May-1992
  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 NewName;
  28. WCHAR NewNameBuffer[WORK_SIZE];
  29. UNICODE_STRING OldName;
  30. WCHAR OldNameBuffer[WORK_SIZE];
  31. void
  32. __cdecl main(
  33. int argc,
  34. char *argv[]
  35. )
  36. {
  37. NTSTATUS status;
  38. OBJECT_ATTRIBUTES NewAttributes;
  39. OBJECT_ATTRIBUTES OldAttributes;
  40. OBJECT_ATTRIBUTES ObjectAttributes;
  41. IO_STATUS_BLOCK IoStatus;
  42. HANDLE FileHandle;
  43. HANDLE KeyHandle;
  44. BOOLEAN WasEnabled;
  45. //
  46. // Process args
  47. //
  48. KeyPath.MaximumLength = WORK_SIZE;
  49. KeyPath.Length = 0L;
  50. KeyPath.Buffer = &(KeyPathBuffer[0]);
  51. NewName.MaximumLength = WORK_SIZE;
  52. NewName.Length = 0L;
  53. NewName.Buffer = &(NewNameBuffer[0]);
  54. OldName.MaximumLength = WORK_SIZE;
  55. OldName.Length = 0L;
  56. OldName.Buffer = &(OldNameBuffer[0]);
  57. processargs(argc, argv);
  58. //
  59. // Set up and open FileName
  60. //
  61. printf("rtreplac: starting\n");
  62. InitializeObjectAttributes(
  63. &NewAttributes,
  64. &NewName,
  65. OBJ_CASE_INSENSITIVE,
  66. (HANDLE)NULL,
  67. NULL
  68. );
  69. InitializeObjectAttributes(
  70. &OldAttributes,
  71. &OldName,
  72. OBJ_CASE_INSENSITIVE,
  73. (HANDLE)NULL,
  74. NULL
  75. );
  76. InitializeObjectAttributes(
  77. &ObjectAttributes,
  78. &KeyPath,
  79. OBJ_CASE_INSENSITIVE,
  80. (HANDLE)NULL,
  81. NULL
  82. );
  83. status = NtOpenKey(
  84. &KeyHandle,
  85. MAXIMUM_ALLOWED,
  86. &ObjectAttributes
  87. );
  88. if (!NT_SUCCESS(status)) {
  89. printf("rtreplac: key open failed status = %08lx\n", status);
  90. exit(1);
  91. }
  92. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, TRUE, FALSE, &WasEnabled);
  93. status = NtReplaceKey(&NewAttributes,
  94. KeyHandle,
  95. &OldAttributes);
  96. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasEnabled, FALSE, &WasEnabled);
  97. if (!NT_SUCCESS(status)) {
  98. printf("rtreplac: NtReplaceKey failed status = %08lx\n", status);
  99. exit(1);
  100. }
  101. printf("rtreplac: success\n");
  102. exit(0);
  103. }
  104. void
  105. processargs(
  106. int argc,
  107. char *argv[]
  108. )
  109. {
  110. ANSI_STRING temp;
  111. UNICODE_STRING DosFileName;
  112. if ( (argc != 4) )
  113. {
  114. printf("Usage: %s <KeyName> <NewFileName> <OldFileName>\n",
  115. argv[0]);
  116. exit(1);
  117. }
  118. RtlInitAnsiString(
  119. &temp,
  120. argv[1]
  121. );
  122. RtlAnsiStringToUnicodeString(
  123. &KeyPath,
  124. &temp,
  125. TRUE
  126. );
  127. RtlInitAnsiString(
  128. &temp,
  129. argv[2]
  130. );
  131. RtlAnsiStringToUnicodeString(
  132. &DosFileName,
  133. &temp,
  134. TRUE
  135. );
  136. RtlDosPathNameToNtPathName_U( DosFileName.Buffer,
  137. &NewName,
  138. NULL,
  139. NULL );
  140. RtlInitAnsiString(
  141. &temp,
  142. argv[3]
  143. );
  144. RtlAnsiStringToUnicodeString(
  145. &DosFileName,
  146. &temp,
  147. TRUE
  148. );
  149. RtlDosPathNameToNtPathName_U( DosFileName.Buffer,
  150. &OldName,
  151. NULL,
  152. NULL );
  153. return;
  154. }