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.

212 lines
4.5 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #define VALUE_BUFFER_SIZE 1024
  8. PWSTR
  9. SfcQueryRegString(
  10. LPWSTR KeyNameStr,
  11. LPWSTR ValueNameStr
  12. )
  13. {
  14. NTSTATUS Status;
  15. UNICODE_STRING KeyName;
  16. UNICODE_STRING ValueName;
  17. OBJECT_ATTRIBUTES ObjectAttributes;
  18. HANDLE Key;
  19. WCHAR ValueBuffer[VALUE_BUFFER_SIZE];
  20. PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo;
  21. ULONG ValueLength;
  22. PWSTR s;
  23. //
  24. // Open the registry key.
  25. //
  26. RtlZeroMemory( (PVOID)ValueBuffer, VALUE_BUFFER_SIZE );
  27. KeyValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
  28. RtlInitUnicodeString( &KeyName, KeyNameStr );
  29. InitializeObjectAttributes(
  30. &ObjectAttributes,
  31. &KeyName,
  32. OBJ_CASE_INSENSITIVE,
  33. NULL,
  34. NULL
  35. );
  36. Status = NtOpenKey(&Key, KEY_READ, &ObjectAttributes);
  37. if (!NT_SUCCESS(Status)) {
  38. return NULL;
  39. }
  40. //
  41. // Query the key value.
  42. //
  43. RtlInitUnicodeString( &ValueName, ValueNameStr );
  44. Status = NtQueryValueKey(
  45. Key,
  46. &ValueName,
  47. KeyValuePartialInformation,
  48. (PVOID)KeyValueInfo,
  49. VALUE_BUFFER_SIZE,
  50. &ValueLength
  51. );
  52. NtClose(Key);
  53. if (!NT_SUCCESS(Status)) {
  54. return 0;
  55. }
  56. s = (PWSTR) malloc( KeyValueInfo->DataLength + 16 );
  57. if (s == NULL) {
  58. return NULL;
  59. }
  60. CopyMemory( s, KeyValueInfo->Data, KeyValueInfo->DataLength );
  61. return s;
  62. }
  63. ULONG
  64. SfcQueryRegDword(
  65. LPWSTR KeyNameStr,
  66. LPWSTR ValueNameStr
  67. )
  68. {
  69. NTSTATUS Status;
  70. UNICODE_STRING KeyName;
  71. UNICODE_STRING ValueName;
  72. OBJECT_ATTRIBUTES ObjectAttributes;
  73. HANDLE Key;
  74. WCHAR ValueBuffer[VALUE_BUFFER_SIZE];
  75. PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo;
  76. ULONG ValueLength;
  77. //
  78. // Open the registry key.
  79. //
  80. KeyValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
  81. RtlInitUnicodeString( &KeyName, KeyNameStr );
  82. InitializeObjectAttributes(
  83. &ObjectAttributes,
  84. &KeyName,
  85. OBJ_CASE_INSENSITIVE,
  86. NULL,
  87. NULL
  88. );
  89. Status = NtOpenKey(&Key, KEY_READ, &ObjectAttributes);
  90. if (!NT_SUCCESS(Status)) {
  91. return 0;
  92. }
  93. //
  94. // Query the key value.
  95. //
  96. RtlInitUnicodeString( &ValueName, ValueNameStr );
  97. Status = NtQueryValueKey(
  98. Key,
  99. &ValueName,
  100. KeyValuePartialInformation,
  101. (PVOID)KeyValueInfo,
  102. VALUE_BUFFER_SIZE,
  103. &ValueLength
  104. );
  105. NtClose(Key);
  106. if (!NT_SUCCESS(Status)) {
  107. return 0;
  108. }
  109. return *((PULONG)&KeyValueInfo->Data);
  110. }
  111. ULONG
  112. ExpandPathString(
  113. IN PWSTR PathString,
  114. IN ULONG PathStringLength,
  115. OUT PUNICODE_STRING FileName,
  116. OUT PUNICODE_STRING PathName
  117. )
  118. {
  119. NTSTATUS Status;
  120. UNICODE_STRING NewPath;
  121. UNICODE_STRING SrcPath;
  122. PWSTR FilePart;
  123. SrcPath.Length = (USHORT)PathStringLength;
  124. SrcPath.MaximumLength = SrcPath.Length;
  125. SrcPath.Buffer = PathString;
  126. NewPath.Length = 0;
  127. NewPath.MaximumLength = (MAX_PATH*2) * sizeof(WCHAR);
  128. NewPath.Buffer = (PWSTR) malloc( NewPath.MaximumLength );
  129. if (NewPath.Buffer == NULL) {
  130. return STATUS_NO_MEMORY;
  131. }
  132. Status = RtlExpandEnvironmentStrings_U(
  133. NULL,
  134. &SrcPath,
  135. &NewPath,
  136. NULL
  137. );
  138. if (!NT_SUCCESS(Status)) {
  139. goto exit;
  140. }
  141. if (FileName == NULL) {
  142. PathName->Length = NewPath.Length;
  143. PathName->MaximumLength = NewPath.MaximumLength;
  144. PathName->Buffer = NewPath.Buffer;
  145. return STATUS_SUCCESS;
  146. }
  147. FilePart = wcsrchr( NewPath.Buffer, L'\\' );
  148. if (FilePart == NULL) {
  149. Status = STATUS_NO_MEMORY;
  150. goto exit;
  151. }
  152. *FilePart = 0;
  153. FilePart += 1;
  154. PathName->Length = wcslen(NewPath.Buffer) * sizeof(WCHAR);
  155. PathName->MaximumLength = PathName->Length + 4;
  156. PathName->Buffer = (PWSTR) malloc( PathName->MaximumLength );
  157. if (PathName->Buffer == NULL) {
  158. Status = STATUS_NO_MEMORY;
  159. goto exit;
  160. }
  161. wcscpy( PathName->Buffer, NewPath.Buffer );
  162. FileName->Length = wcslen(FilePart) * sizeof(WCHAR);
  163. FileName->MaximumLength = FileName->Length + 4;
  164. FileName->Buffer = (PWSTR) malloc( FileName->MaximumLength );
  165. if (FileName->Buffer == NULL) {
  166. Status = STATUS_NO_MEMORY;
  167. free( PathName->Buffer );
  168. goto exit;
  169. }
  170. wcscpy( FileName->Buffer, FilePart );
  171. Status = STATUS_SUCCESS;
  172. exit:
  173. free( NewPath.Buffer );
  174. return Status;
  175. }