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.

215 lines
4.6 KiB

  1. /*++
  2. Copyright(c) 1995 Microsoft Corporation
  3. MODULE NAME
  4. reg.c
  5. ABSTRACT
  6. Registry routines for the automatic connection service.
  7. AUTHOR
  8. Anthony Discolo (adiscolo) 20-Mar-1995
  9. REVISION HISTORY
  10. Original version from Gurdeep
  11. --*/
  12. #define UNICODE
  13. #define _UNICODE
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <stdlib.h>
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <npapi.h>
  21. #include <acd.h>
  22. #include <debug.h>
  23. #include "reg.h"
  24. #include "misc.h"
  25. //
  26. // The maximum size of TOKEN_USER information.
  27. //
  28. #define TOKEN_INFORMATION_SIZE (sizeof (TOKEN_USER) + sizeof (SID) + (sizeof (ULONG) * SID_MAX_SUB_AUTHORITIES))
  29. HKEY
  30. GetHkeyCurrentUser(
  31. HANDLE hToken
  32. )
  33. {
  34. BOOLEAN fSuccess;
  35. HKEY hkey = NULL;
  36. UCHAR TokenInformation[TOKEN_INFORMATION_SIZE];
  37. DWORD dwReturnLength;
  38. UNICODE_STRING sidString, keyString;
  39. OBJECT_ATTRIBUTES objectAttributes;
  40. if (!GetTokenInformation(
  41. hToken,
  42. TokenUser,
  43. TokenInformation,
  44. sizeof (TokenInformation),
  45. &dwReturnLength))
  46. {
  47. RASAUTO_TRACE1(
  48. "GetHkeyCurrentUser: GetTokenInformation failed (error=%d)",
  49. GetLastError());
  50. return NULL;
  51. }
  52. if (RtlConvertSidToUnicodeString(
  53. &sidString,
  54. ((PTOKEN_USER)TokenInformation)->User.Sid,
  55. TRUE) != STATUS_SUCCESS)
  56. {
  57. RASAUTO_TRACE1(
  58. "GetHkeyCurrentUser: RtlConvertSidToUnicodeString failed (error=%d)",
  59. GetLastError());
  60. return NULL;
  61. }
  62. keyString.Length = 0;
  63. keyString.MaximumLength =
  64. sidString.Length + sizeof (L"\\REGISTRY\\USER\\") + sizeof (L"\0");
  65. keyString.Buffer = LocalAlloc(LPTR, keyString.MaximumLength);
  66. if (keyString.Buffer == NULL) {
  67. RASAUTO_TRACE("GetHkeyCurrentUser: LocalAlloc failed");
  68. RtlFreeUnicodeString(&sidString);
  69. return NULL;
  70. }
  71. //
  72. // Copy \REGISTRY\USER to keyString.
  73. //
  74. RtlAppendUnicodeToString(&keyString, L"\\REGISTRY\\USER\\");
  75. //
  76. // Append the user's SID to keyString.
  77. //
  78. if (RtlAppendUnicodeStringToString(
  79. &keyString,
  80. &sidString) != STATUS_SUCCESS)
  81. {
  82. RASAUTO_TRACE1(
  83. "GetHkeyCurrentUser: RtlAppendUnicodeToString failed (error=%d)",
  84. GetLastError());
  85. RtlFreeUnicodeString(&sidString);
  86. LocalFree(keyString.Buffer);
  87. return NULL;
  88. }
  89. RtlFreeUnicodeString(&sidString);
  90. RASAUTO_TRACE1(
  91. "GetHkeyCurrentUser: HKEY_CURRENT_USER is %S",
  92. keyString.Buffer);
  93. //
  94. // Initialize the object attributes.
  95. //
  96. InitializeObjectAttributes(
  97. &objectAttributes,
  98. &keyString,
  99. OBJ_CASE_INSENSITIVE,
  100. NULL,
  101. NULL);
  102. //
  103. // Open the registry key.
  104. //
  105. if (NtOpenKey(
  106. &hkey,
  107. MAXIMUM_ALLOWED,
  108. &objectAttributes) != STATUS_SUCCESS)
  109. {
  110. RASAUTO_TRACE1(
  111. "GetHkeyCurrentUser: NtOpenKey failed (error=%d)",
  112. GetLastError());
  113. LocalFree(keyString.Buffer);
  114. return NULL;
  115. }
  116. LocalFree(keyString.Buffer);
  117. return hkey;
  118. } // GetHkeyCurrentUser
  119. BOOLEAN
  120. RegGetValue(
  121. IN HKEY hkey,
  122. IN LPTSTR pszKey,
  123. OUT PVOID *ppvData,
  124. OUT LPDWORD pdwcbData,
  125. OUT LPDWORD pdwType
  126. )
  127. {
  128. DWORD dwError, dwType, dwSize;
  129. PVOID pvData;
  130. //
  131. // Get the length of the string.
  132. //
  133. dwError = RegQueryValueEx(
  134. hkey,
  135. pszKey,
  136. NULL,
  137. &dwType,
  138. NULL,
  139. &dwSize);
  140. if (dwError != ERROR_SUCCESS)
  141. return FALSE;
  142. pvData = LocalAlloc(LPTR, dwSize);
  143. if (pvData == NULL) {
  144. RASAUTO_TRACE("RegGetValue: LocalAlloc failed");
  145. return FALSE;
  146. }
  147. //
  148. // Read the value for real this time.
  149. //
  150. dwError = RegQueryValueEx(
  151. hkey,
  152. pszKey,
  153. NULL,
  154. NULL,
  155. (LPBYTE)pvData,
  156. &dwSize);
  157. if (dwError != ERROR_SUCCESS) {
  158. LocalFree(pvData);
  159. return FALSE;
  160. }
  161. if(NULL != pdwType)
  162. {
  163. *pdwType = dwType;
  164. }
  165. *ppvData = pvData;
  166. if (pdwcbData != NULL)
  167. *pdwcbData = dwSize;
  168. return TRUE;
  169. } // RegGetValue
  170. BOOLEAN
  171. RegGetDword(
  172. IN HKEY hkey,
  173. IN LPTSTR pszKey,
  174. OUT LPDWORD pdwValue
  175. )
  176. {
  177. DWORD dwError, dwType, dwSize = sizeof (DWORD);
  178. dwError = RegQueryValueEx(
  179. hkey,
  180. pszKey,
  181. NULL,
  182. &dwType,
  183. (LPBYTE)pdwValue,
  184. &dwSize);
  185. if (dwError != ERROR_SUCCESS || dwType != REG_DWORD)
  186. return FALSE;
  187. return TRUE;
  188. } // RegGetDword