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.

412 lines
13 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: registry.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. NTSTATUS
  12. PptRegGetDeviceParameterDword(
  13. IN PDEVICE_OBJECT Pdo,
  14. IN PWSTR ParameterName,
  15. IN OUT PULONG ParameterValue
  16. )
  17. /*++
  18. Routine Description:
  19. retrieve a devnode registry parameter of type dword
  20. Arguments:
  21. Pdo - ParPort PDO
  22. ParameterName - parameter name to look up
  23. ParameterValue - default parameter value
  24. Return Value:
  25. Status - if RegKeyValue does not exist or other failure occurs,
  26. then default is returned via ParameterValue
  27. --*/
  28. {
  29. NTSTATUS status;
  30. HANDLE hKey;
  31. RTL_QUERY_REGISTRY_TABLE queryTable[2];
  32. ULONG defaultValue;
  33. PAGED_CODE();
  34. status = IoOpenDeviceRegistryKey(Pdo, PLUGPLAY_REGKEY_DEVICE, KEY_READ, &hKey);
  35. if(!NT_SUCCESS(status)) {
  36. return status;
  37. }
  38. defaultValue = *ParameterValue;
  39. RtlZeroMemory(queryTable, sizeof(queryTable));
  40. queryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  41. queryTable[0].Name = ParameterName;
  42. queryTable[0].EntryContext = ParameterValue;
  43. queryTable[0].DefaultType = REG_DWORD;
  44. queryTable[0].DefaultData = &defaultValue;
  45. queryTable[0].DefaultLength = sizeof(ULONG);
  46. status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE, hKey, queryTable, NULL, NULL);
  47. if ( !NT_SUCCESS(status) ) {
  48. *ParameterValue = defaultValue;
  49. }
  50. ZwClose(hKey);
  51. return status;
  52. }
  53. NTSTATUS
  54. PptRegSetDeviceParameterDword(
  55. IN PDEVICE_OBJECT Pdo,
  56. IN PWSTR ParameterName,
  57. IN PULONG ParameterValue
  58. )
  59. /*++
  60. Routine Description:
  61. Create/set a devnode registry parameter of type dword
  62. Arguments:
  63. Pdo - ParPort PDO
  64. ParameterName - parameter name
  65. ParameterValue - parameter value
  66. Return Value:
  67. Status - status from attempt
  68. --*/
  69. {
  70. NTSTATUS status;
  71. HANDLE hKey;
  72. UNICODE_STRING valueName;
  73. PPDO_EXTENSION pdx = Pdo->DeviceExtension;
  74. PAGED_CODE();
  75. status = IoOpenDeviceRegistryKey(Pdo, PLUGPLAY_REGKEY_DEVICE, KEY_WRITE, &hKey);
  76. if( !NT_SUCCESS( status ) ) {
  77. DD((PCE)pdx,DDE,"PptRegSetDeviceParameterDword - openKey FAILED w/status=%x",status);
  78. return status;
  79. }
  80. RtlInitUnicodeString( &valueName, ParameterName );
  81. status = ZwSetValueKey( hKey, &valueName, 0, REG_DWORD, ParameterValue, sizeof(ULONG) );
  82. if( !NT_SUCCESS( status ) ) {
  83. DD((PCE)pdx,DDE,"PptRegSetDeviceParameterDword - setValue FAILED w/status=%x",status);
  84. }
  85. ZwClose(hKey);
  86. return status;
  87. }
  88. /************************************************************************/
  89. /* PptRegGetDword */
  90. /************************************************************************/
  91. //
  92. // Routine Description:
  93. //
  94. // Read a REG_DWORD from the registry. This is a wrapper for
  95. // function RtlQueryRegistryValues.
  96. //
  97. // Arguments:
  98. //
  99. // RelativeTo - starting point for the Path
  100. // Path - path to the registry key
  101. // ParameterName - name of the value to be read
  102. // ParameterValue - used to return the DWORD value read from the registry
  103. //
  104. // Return Value:
  105. //
  106. // NTSTATUS
  107. //
  108. // Notes:
  109. //
  110. // - On an ERROR or if the requested registry value does not exist,
  111. // *ParameterValue retains its original value.
  112. //
  113. // Log:
  114. //
  115. /************************************************************************/
  116. NTSTATUS
  117. PptRegGetDword(
  118. IN ULONG RelativeTo,
  119. IN PWSTR Path,
  120. IN PWSTR ParameterName,
  121. IN OUT PULONG ParameterValue
  122. )
  123. {
  124. NTSTATUS status;
  125. RTL_QUERY_REGISTRY_TABLE paramTable[2];
  126. ULONG defaultValue;
  127. if( ( NULL == Path ) || ( NULL == ParameterName ) || ( NULL == ParameterValue ) ) {
  128. return STATUS_INVALID_PARAMETER;
  129. }
  130. DD(NULL,DDT,"PptRegGetDword - RelativeTo= %x, Path=<%S>, ParameterName=<%S>\n", RelativeTo, Path, ParameterName);
  131. //
  132. // set up table entries for call to RtlQueryRegistryValues
  133. //
  134. // leave paramTable[1] as all zeros to terminate the table
  135. //
  136. // use original value as default value
  137. //
  138. // use RtlQueryRegistryValues to do the grunge work
  139. //
  140. RtlZeroMemory( paramTable, sizeof(paramTable) );
  141. defaultValue = *ParameterValue;
  142. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  143. paramTable[0].Name = ParameterName;
  144. paramTable[0].EntryContext = ParameterValue;
  145. paramTable[0].DefaultType = REG_DWORD;
  146. paramTable[0].DefaultData = &defaultValue;
  147. paramTable[0].DefaultLength = sizeof(ULONG);
  148. status = RtlQueryRegistryValues( RelativeTo | RTL_REGISTRY_OPTIONAL,
  149. Path,
  150. &paramTable[0],
  151. NULL,
  152. NULL);
  153. if( status != STATUS_SUCCESS ) {
  154. DD(NULL,DDW,"PptRegGetDword - RtlQueryRegistryValues FAILED w/status=%x\n",status);
  155. }
  156. DD(NULL,DDT,"PptRegGetDword - post-query <%S> *ParameterValue = %x\n", ParameterName, *ParameterValue);
  157. return status;
  158. }
  159. /************************************************************************/
  160. /* PptRegSetDword */
  161. /************************************************************************/
  162. //
  163. // Routine Description:
  164. //
  165. // Write a REG_DWORD to the registry. This is a wrapper for
  166. // function RtlWriteRegistryValue.
  167. //
  168. // Arguments:
  169. //
  170. // RelativeTo - starting point for the Path
  171. // Path - path to the registry key
  172. // ParameterName - name of the value to write
  173. // ParameterValue - points to the DWORD value to write to the registry
  174. //
  175. // Return Value:
  176. //
  177. // NTSTATUS
  178. //
  179. // Notes:
  180. //
  181. // Log:
  182. //
  183. /************************************************************************/
  184. NTSTATUS
  185. PptRegSetDword(
  186. IN ULONG RelativeTo,
  187. IN PWSTR Path,
  188. IN PWSTR ParameterName,
  189. IN PULONG ParameterValue
  190. )
  191. {
  192. NTSTATUS status;
  193. if( (NULL == Path) || (NULL == ParameterName) || (NULL == ParameterValue) ) {
  194. status = STATUS_INVALID_PARAMETER;
  195. } else {
  196. status = RtlWriteRegistryValue( RelativeTo,
  197. Path,
  198. ParameterName,
  199. REG_DWORD,
  200. ParameterValue,
  201. sizeof(ULONG) );
  202. }
  203. return status;
  204. }
  205. /************************************************************************/
  206. /* PptRegGetSz */
  207. /************************************************************************/
  208. //
  209. // Routine Description:
  210. //
  211. // Read a REG_SZ from the registry. This is a wrapper for
  212. // function RtlQueryRegistryValues.
  213. //
  214. // Arguments:
  215. //
  216. // RelativeTo - starting point for the Path
  217. // Path - path to the registry key
  218. // ParameterName - name of the value to be read
  219. // ParameterValue - points to a UNICODE_STRING structure used to return
  220. // the REG_SZ read from the registry
  221. //
  222. // Return Value:
  223. //
  224. // NTSTATUS
  225. //
  226. //
  227. // Notes:
  228. //
  229. // - All fields of *ParameterValue UNICODE_STRING structure must be
  230. // initialized to zero by the caller.
  231. // - On SUCCESS ParameterValue->Buffer points to an allocated buffer. The
  232. // caller is responsible for freeing this buffer when done.
  233. // - On SUCCESS ParameterValue->Buffer is UNICODE_NULL terminated and is
  234. // safe to use as a PWSTR.
  235. //
  236. // Log:
  237. //
  238. /************************************************************************/
  239. NTSTATUS
  240. PptRegGetSz(
  241. IN ULONG RelativeTo,
  242. IN PWSTR Path,
  243. IN PWSTR ParameterName,
  244. IN OUT PUNICODE_STRING ParameterValue
  245. )
  246. {
  247. NTSTATUS status;
  248. RTL_QUERY_REGISTRY_TABLE paramTable[2];
  249. //
  250. // sanity check parameters - reject NULL pointers and invalid
  251. // UNICODE_STRING field initializations
  252. //
  253. if( ( NULL == Path ) || ( NULL == ParameterName ) || ( NULL == ParameterValue ) ) {
  254. return STATUS_INVALID_PARAMETER;
  255. }
  256. if( (ParameterValue->Length != 0) || (ParameterValue->MaximumLength !=0) || (ParameterValue->Buffer != NULL) ) {
  257. return STATUS_INVALID_PARAMETER;
  258. }
  259. DD(NULL,DDT,"PptRegGetSz - RelativeTo=%x, Path=<%S>, ParameterName=<%S>\n", RelativeTo, Path, ParameterName);
  260. //
  261. // set up table entries for call to RtlQueryRegistryValues
  262. //
  263. // leave paramTable[1] as all zeros to terminate the table
  264. //
  265. // use RtlQueryRegistryValues to do the grunge work
  266. //
  267. RtlZeroMemory( paramTable, sizeof(paramTable) );
  268. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  269. paramTable[0].Name = ParameterName;
  270. paramTable[0].EntryContext = ParameterValue;
  271. paramTable[0].DefaultType = REG_SZ;
  272. paramTable[0].DefaultData = L"";
  273. paramTable[0].DefaultLength = 0;
  274. status = RtlQueryRegistryValues( RelativeTo | RTL_REGISTRY_OPTIONAL,
  275. Path,
  276. &paramTable[0],
  277. NULL,
  278. NULL);
  279. if( status != STATUS_SUCCESS ) {
  280. DD(NULL,DDW,"PptRegGetSz - RtlQueryRegistryValues FAILED w/status=%x\n",status);
  281. }
  282. //
  283. // Try to make ParameterValue->Buffer safe to use as a PWSTR parameter.
  284. // Clean up the allocation and fail this request if we are unable to do so.
  285. //
  286. if( (STATUS_SUCCESS == status) && (ParameterValue->Buffer != NULL) ) {
  287. if( ParameterValue->MaximumLength >= (ParameterValue->Length + sizeof(WCHAR)) ) {
  288. (ParameterValue->Buffer)[ ParameterValue->Length / sizeof(WCHAR) ] = UNICODE_NULL;
  289. DD(NULL,DDT,"PptRegGetSz - post-query *ParameterValue=<%S>\n", ParameterValue->Buffer);
  290. } else {
  291. ExFreePool( ParameterValue->Buffer );
  292. ParameterValue->Length = 0;
  293. ParameterValue->MaximumLength = 0;
  294. ParameterValue->Buffer = 0;
  295. status = STATUS_UNSUCCESSFUL;
  296. }
  297. }
  298. return status;
  299. }
  300. /************************************************************************/
  301. /* PptRegSetSz */
  302. /************************************************************************/
  303. //
  304. // Routine Description:
  305. //
  306. // Write a REG_SZ to the registry. This is a wrapper for
  307. // function RtlWriteRegistryValue.
  308. //
  309. // Arguments:
  310. //
  311. // RelativeTo - starting point for the Path
  312. // Path - path to the registry key
  313. // ParameterName - name of the value to write
  314. // ParameterValue - points to the PWSTR to write to the registry
  315. //
  316. // Return Value:
  317. //
  318. // NTSTATUS
  319. //
  320. // Notes:
  321. //
  322. // Log:
  323. //
  324. /************************************************************************/
  325. NTSTATUS
  326. PptRegSetSz(
  327. IN ULONG RelativeTo,
  328. IN PWSTR Path,
  329. IN PWSTR ParameterName,
  330. IN PWSTR ParameterValue
  331. )
  332. {
  333. NTSTATUS status;
  334. if( (NULL == Path) || (NULL == ParameterName) || (NULL == ParameterValue) ) {
  335. status = STATUS_INVALID_PARAMETER;
  336. } else {
  337. status = RtlWriteRegistryValue( RelativeTo,
  338. Path,
  339. ParameterName,
  340. REG_SZ,
  341. ParameterValue,
  342. ( wcslen(ParameterValue) + 1 ) * sizeof(WCHAR) );
  343. }
  344. return status;
  345. }