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.

202 lines
6.6 KiB

  1. //==========================================================================;
  2. //
  3. // Registry.CPP
  4. // WDM MiniDrivers development.
  5. // Global space.
  6. // Registry data management.
  7. // Copyright (c) 1996 - 1997 ATI Technologies Inc. All Rights Reserved.
  8. //
  9. // $Date: 15 Apr 1999 11:08:06 $
  10. // $Revision: 1.6 $
  11. // $Author: Tashjian $
  12. //
  13. //==========================================================================;
  14. #include "common.h"
  15. #include "registry.h"
  16. ULONG g_DebugLevel;
  17. PCHAR g_DebugComponent = WDM_DRIVER_NAME " not set in registry: ";
  18. /*^^*
  19. * OpenRegistryFolder()
  20. * Purpose : Gets the specified Registry folder handle ( opens the folder) to be used later on
  21. *
  22. * Inputs : PDEVICE_OBJECT pDeviceObject : pointer to DeviceObject
  23. * PWCHAR pwchFolderName : name of the Registry folder to open, might be NULL
  24. * Outputs : HANDLE, NULL if the Registry folder has not been opened
  25. * Author : IKLEBANOV
  26. *^^*/
  27. HANDLE OpenRegistryFolder( PDEVICE_OBJECT pDeviceObject, PWCHAR pwchFolderName)
  28. {
  29. HANDLE hFolder, hDevice;
  30. NTSTATUS ntStatus;
  31. UNICODE_STRING FolderName;
  32. OBJECT_ATTRIBUTES attr;
  33. hFolder = hDevice = NULL;
  34. ENSURE
  35. {
  36. ntStatus = ::IoOpenDeviceRegistryKey( pDeviceObject,
  37. PLUGPLAY_REGKEY_DRIVER, STANDARD_RIGHTS_ALL,
  38. &hDevice);
  39. if( !NT_SUCCESS( ntStatus) || ( hDevice == NULL))
  40. FAIL;
  41. if( pwchFolderName != NULL)
  42. {
  43. ::RtlInitUnicodeString( &FolderName, pwchFolderName);
  44. InitializeObjectAttributes( &attr, &FolderName, OBJ_INHERIT|OBJ_KERNEL_HANDLE, hDevice, NULL);
  45. ntStatus = ::ZwOpenKey( &hFolder, KEY_QUERY_VALUE, &attr);
  46. ::ZwClose( hDevice);
  47. if( !NT_SUCCESS( ntStatus))
  48. FAIL;
  49. }
  50. else
  51. hFolder = hDevice;
  52. return( hFolder);
  53. } END_ENSURE;
  54. return( NULL);
  55. }
  56. /*^^*
  57. * SetMiniDriverDebugLevel()
  58. * Purpose : Sets the Debugging level required by user
  59. *
  60. * Inputs : PUNICODE_STRING pRegistryPath : MiniDriver's private Registry path
  61. * Outputs : none
  62. *
  63. * Author : IKLEBANOV
  64. *^^*/
  65. extern "C"
  66. void SetMiniDriverDebugLevel( PUNICODE_STRING pRegistryPath)
  67. {
  68. OBJECT_ATTRIBUTES objectAttributes;
  69. HANDLE hFolder;
  70. ULONG ulValue;
  71. WCHAR wcDriverName[20];
  72. // Set the default value as no Debug
  73. g_DebugLevel = 0;
  74. InitializeObjectAttributes( &objectAttributes,
  75. pRegistryPath,
  76. OBJ_CASE_INSENSITIVE,
  77. NULL,
  78. NULL);
  79. if( NT_SUCCESS( ZwOpenKey( &hFolder, KEY_READ, &objectAttributes)))
  80. {
  81. ulValue = 0;
  82. if( NT_SUCCESS( ReadStringFromRegistryFolder( hFolder,
  83. UNICODE_WDM_DEBUGLEVEL_INFO,
  84. ( PWCHAR)&ulValue,
  85. sizeof( ULONG))))
  86. g_DebugLevel = ulValue;
  87. // fetch the driver name from the registry
  88. if( NT_SUCCESS( ReadStringFromRegistryFolder( hFolder,
  89. UNICODE_WDM_DRIVER_NAME,
  90. wcDriverName,
  91. sizeof(wcDriverName)))) {
  92. // set g_DebugComponent by using driver name
  93. ANSI_STRING stringDriverName;
  94. UNICODE_STRING unicodeDriverName;
  95. // convert unicode driver name to ansi
  96. RtlInitAnsiString(&stringDriverName, g_DebugComponent);
  97. RtlInitUnicodeString(&unicodeDriverName, wcDriverName);
  98. RtlUnicodeStringToAnsiString(&stringDriverName, &unicodeDriverName, FALSE);
  99. // remove extension and put a colon
  100. PCHAR pExt = strchr(g_DebugComponent, '.');
  101. if (pExt) {
  102. *pExt++ = ':';
  103. *pExt++ = ' ';
  104. *pExt = 0;
  105. }
  106. // convert to upper case (or lower case... whatever your fancy)
  107. //_strupr(g_DebugComponent);
  108. //_strlwr(g_DebugComponent);
  109. }
  110. ZwClose( hFolder);
  111. }
  112. }
  113. /*^^*
  114. * ReadStringFromRegistryFolder
  115. * Purpose : Read ASCII string from the Registry folder
  116. *
  117. * Inputs : HANDLE hFolder : Registry folder handle to read the values from
  118. * PWCHAR pwcKeyNameString : pointer to the StringValue to read
  119. * PWCHAR pwchBuffer : pointer to the buffer to read into
  120. * ULONG ulDataLength : length of the data to be expected to read
  121. *
  122. * Outputs : NTSTATUS of the registry read operation
  123. * Author : IKLEBANOV
  124. *^^*/
  125. NTSTATUS ReadStringFromRegistryFolder( HANDLE hFolder, PWCHAR pwcKeyNameString, PWCHAR pwchBuffer, ULONG ulDataLength)
  126. {
  127. NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
  128. UNICODE_STRING unicodeKeyName;
  129. ULONG ulLength;
  130. PKEY_VALUE_FULL_INFORMATION FullInfo;
  131. ENSURE
  132. {
  133. ::RtlInitUnicodeString( &unicodeKeyName, pwcKeyNameString);
  134. ulLength = sizeof( KEY_VALUE_FULL_INFORMATION) + unicodeKeyName.MaximumLength + ulDataLength;
  135. FullInfo = ( PKEY_VALUE_FULL_INFORMATION)::ExAllocatePool( PagedPool, ulLength);
  136. if( FullInfo)
  137. {
  138. ntStatus = ::ZwQueryValueKey( hFolder,
  139. &unicodeKeyName,
  140. KeyValueFullInformation,
  141. FullInfo,
  142. ulLength,
  143. &ulLength);
  144. if( NT_SUCCESS( ntStatus))
  145. {
  146. if( ulDataLength >= FullInfo->DataLength)
  147. RtlCopyMemory( pwchBuffer, (( PUCHAR)FullInfo) + FullInfo->DataOffset, FullInfo->DataLength);
  148. else
  149. {
  150. TRAP;
  151. ntStatus = STATUS_BUFFER_TOO_SMALL;
  152. } // buffer right length
  153. } // if success
  154. ::ExFreePool( FullInfo);
  155. }
  156. else
  157. {
  158. ntStatus = STATUS_NO_MEMORY;
  159. }
  160. } END_ENSURE;
  161. return( ntStatus);
  162. }