Leaked source code of windows server 2003
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.

177 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. rtrest2.c
  5. Abstract:
  6. NT level registry api test program, basic error path
  7. Creates a key "Key1" and a subkey, "Key2"
  8. Calls NtSaveKey on Key1, then NtRestoreKey while a handle to Key2 is
  9. still open.
  10. rtrest2 <KeyPath> <FileName>
  11. Example:
  12. rtrest2 \registry\machine\system\test tempfile
  13. Author:
  14. John Vert (jvert) 13-Jun-1992
  15. Revision History:
  16. --*/
  17. #include "cmp.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #define WORK_SIZE 1024
  22. void __cdecl main(int, char *);
  23. void processargs();
  24. UNICODE_STRING KeyPath;
  25. WCHAR KeyPathBuffer[WORK_SIZE];
  26. UNICODE_STRING FileName;
  27. WCHAR FileNameBuffer[WORK_SIZE];
  28. void
  29. __cdecl main(
  30. int argc,
  31. char *argv[]
  32. )
  33. {
  34. NTSTATUS status;
  35. OBJECT_ATTRIBUTES ObjectAttributes;
  36. IO_STATUS_BLOCK IoStatus;
  37. HANDLE FileHandle;
  38. HANDLE KeyHandle;
  39. //
  40. // Process args
  41. //
  42. KeyPath.MaximumLength = WORK_SIZE;
  43. KeyPath.Length = 0L;
  44. KeyPath.Buffer = &(KeyPathBuffer[0]);
  45. FileName.MaximumLength = WORK_SIZE;
  46. FileName.Length = 0L;
  47. FileName.Buffer = &(FileNameBuffer[0]);
  48. processargs(argc, argv);
  49. //
  50. // Set up and open FileName
  51. //
  52. printf("rtrestor: starting\n");
  53. InitializeObjectAttributes(
  54. &ObjectAttributes,
  55. &FileName,
  56. 0,
  57. (HANDLE)NULL,
  58. NULL
  59. );
  60. ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
  61. status = NtCreateFile(
  62. &FileHandle,
  63. GENERIC_READ | SYNCHRONIZE,
  64. &ObjectAttributes,
  65. &IoStatus,
  66. 0, // AllocationSize
  67. FILE_ATTRIBUTE_NORMAL,
  68. 0, // ShareAccess
  69. FILE_OPEN_IF,
  70. FILE_SYNCHRONOUS_IO_NONALERT,
  71. NULL, // EaBuffer
  72. 0 // EaLength
  73. );
  74. if (!NT_SUCCESS(status)) {
  75. printf("rtsave: file open failed status = %08lx\n", status);
  76. exit(1);
  77. }
  78. InitializeObjectAttributes(
  79. &ObjectAttributes,
  80. &KeyPath,
  81. 0,
  82. (HANDLE)NULL,
  83. NULL
  84. );
  85. ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
  86. status = NtOpenKey(
  87. &KeyHandle,
  88. MAXIMUM_ALLOWED,
  89. &ObjectAttributes
  90. );
  91. if (!NT_SUCCESS(status)) {
  92. printf("rtsave: key open failed status = %08lx\n", status);
  93. exit(1);
  94. }
  95. status = NtRestoreKey(KeyHandle, FileHandle);
  96. if (!NT_SUCCESS(status)) {
  97. printf("rtrestor: NtRestorKey failed status = %08lx\n", status);
  98. exit(1);
  99. }
  100. printf("rtsave: success\n");
  101. exit(0);
  102. }
  103. void
  104. processargs(
  105. int argc,
  106. char *argv[]
  107. )
  108. {
  109. ANSI_STRING temp;
  110. if ( (argc != 3) )
  111. {
  112. printf("Usage: %s <KeyName> <FileName>\n",
  113. argv[0]);
  114. exit(1);
  115. }
  116. RtlInitAnsiString(
  117. &temp,
  118. argv[1]
  119. );
  120. RtlAnsiStringToUnicodeString(
  121. &KeyPath,
  122. &temp,
  123. TRUE
  124. );
  125. RtlInitAnsiString(
  126. &temp,
  127. argv[2]
  128. );
  129. RtlAnsiStringToUnicodeString(
  130. &FileName,
  131. &temp,
  132. TRUE
  133. );
  134. return;
  135. }