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.

220 lines
7.2 KiB

  1. /******************************************************************************
  2. *
  3. * $RCSfile: DrvReg.h $
  4. * $Source: u:/si/VXP/Wdm/Classes/DrvReg.h $
  5. * $Author: Max $
  6. * $Date: 1998/09/15 23:39:30 $
  7. * $Revision: 1.3 $
  8. *
  9. * Written by: Max Paklin
  10. * Purpose: Declaration of of registry class
  11. *
  12. *******************************************************************************
  13. *
  14. * Copyright 1996-98, AuraVision Corporation. All rights reserved.
  15. *
  16. * AuraVision Corporation makes no warranty of any kind, express or implied,
  17. * with regard to this software. In no event shall AuraVision Corporation
  18. * be liable for incidental or consequential damages in connection with or
  19. * arising from the furnishing, performance, or use of this software.
  20. *
  21. *******************************************************************************/
  22. #ifndef __DRVREG_H__
  23. #define __DRVREG_H__
  24. #define MAX_KEYLENGTH 512
  25. #define MAX_GUIDLENGTH 48
  26. class CKsRegKey
  27. {
  28. PWCHAR m_pwszKey; // If we are dealing with absolute registry path then
  29. // this is where the string is stored
  30. PVOID m_hHandle; // We probably already have handle to base key opened
  31. // and we are going to use it as a base for all the
  32. // data we will access. Store it here
  33. PDEVICE_OBJECT m_pDeviceObject; // Or we may put the data under device key
  34. // (recommended) and use it as a base. Here is device
  35. // object handle
  36. // Was object initialized correctly, i.e. is it "not empty"?
  37. BOOL IsKey()
  38. {
  39. return (BOOL)(m_hHandle != NULL || m_pDeviceObject != NULL ||
  40. (m_pwszKey && wcslen( m_pwszKey ) > 0));
  41. }
  42. // Helper function to be called from constructors
  43. void Create( LPCWSTR pwszKey, LPCWSTR pwszSubKey,
  44. PVOID hHandle = NULL, PDEVICE_OBJECT pDeviceObject = NULL );
  45. PWSTR GetInputData( PULONG puRelativeTo, PHANDLE phHandleToDelete,
  46. ACCESS_MASK amDesiredAccess, BOOL bCreateHandle = FALSE );
  47. public:
  48. // Constructors
  49. CKsRegKey( LPCWSTR pwszKey, LPCWSTR pwszSubKey = NULL );
  50. CKsRegKey( CKsRegKey& keyFrom, LPCWSTR pwszSubKey = NULL );
  51. CKsRegKey( LPCWSTR pwszKey, GUID guidSubKey );
  52. CKsRegKey( CKsRegKey& keyFrom, GUID guidSubKey );
  53. CKsRegKey( LPCWSTR pwszSubKey, PDEVICE_OBJECT pDeviceObj, PVOID hHandle );
  54. ~CKsRegKey()
  55. {
  56. if( m_pwszKey )
  57. ExFreePool( m_pwszKey );
  58. }
  59. // If we are dealing with absolute key, this is useful method to get that key string
  60. LPWSTR GetKey() { return m_pwszKey; }
  61. // Put the data into registry
  62. BOOL SetValue( LPCWSTR pwszValue, LPCWSTR pwszSetTo );
  63. BOOL SetValue( LPCWSTR pwszValue, int nSetTo );
  64. BOOL SetValue( LPCWSTR pwszValue, GUID guidValue );
  65. // Get the data from registry
  66. BOOL GetValue( LPCWSTR pwszGetFrom, LPWSTR pwszValue, USHORT ushSize, LPCWSTR pwszDefault = L"" );
  67. BOOL GetValue( LPCWSTR pwszGetFrom, PDWORD pdwValue, ULONG ulSize );
  68. BOOL GetValue( LPCWSTR pwszGetFrom, long& nValue, long lDefault = 0 );
  69. BOOL GetValue( LPCWSTR pwszGetFrom, int& nValue, int nDefault = 0 );
  70. BOOL GetValue( LPCWSTR pwszValue, GUID& guidValue );
  71. // Check if the data entry is present under current key
  72. BOOL IsValue( LPCWSTR pwszValue );
  73. // Helper functions to convert wide characters and GUIDs
  74. void GuidToWChar( const GUID guid, LPWSTR pwszString );
  75. NTSTATUS WCharToGuid( LPCWSTR pszString, GUID guid );
  76. // These are for "object oriented people" :-)
  77. const CKsRegKey& operator << ( GUID guidValue )
  78. {
  79. SetValue( NULL, guidValue );
  80. return *this;
  81. }
  82. const CKsRegKey& operator << ( LPCWSTR pwszValue )
  83. {
  84. SetValue( NULL, pwszValue );
  85. return *this;
  86. }
  87. const CKsRegKey& operator << ( long lValue )
  88. {
  89. SetValue( NULL, lValue );
  90. return *this;
  91. }
  92. const CKsRegKey& operator << ( int nValue )
  93. {
  94. SetValue( NULL, (long)nValue );
  95. return *this;
  96. }
  97. friend CKsRegKey& operator >> ( CKsRegKey& keyReg, LPWSTR pwszValue );
  98. };
  99. // Constructors.
  100. // Call Create to initialize object
  101. inline CKsRegKey::CKsRegKey( LPCWSTR pwszKey, LPCWSTR pwszSubKey )
  102. {
  103. Create( pwszKey, pwszSubKey );
  104. }
  105. inline CKsRegKey::CKsRegKey( CKsRegKey& keyFrom, LPCWSTR pwszSubKey )
  106. {
  107. Create( keyFrom.m_pwszKey, pwszSubKey );
  108. }
  109. inline CKsRegKey::CKsRegKey( LPCWSTR pwszKey, GUID guidSubKey )
  110. {
  111. WCHAR wszKeyName[MAX_GUIDLENGTH];
  112. GuidToWChar( guidSubKey, wszKeyName );
  113. Create( pwszKey, wszKeyName );
  114. }
  115. inline CKsRegKey::CKsRegKey( CKsRegKey& keyFrom, GUID guidSubKey )
  116. {
  117. WCHAR wszKeyName[MAX_GUIDLENGTH];
  118. GuidToWChar( guidSubKey, wszKeyName );
  119. Create( keyFrom.m_pwszKey, wszKeyName );
  120. }
  121. inline CKsRegKey::CKsRegKey( LPCWSTR pwszSubKey, PDEVICE_OBJECT pDeviceObject, PVOID hHandle )
  122. {
  123. Create( NULL, pwszSubKey, hHandle, pDeviceObject );
  124. }
  125. // C++ madness. Create method for every possible type of arguments to avoid compiler errors.
  126. // All the functions, of course, will just delegate the call to one of them to do the real
  127. // job.
  128. // These numerous set and get functions exist here for exactly that reason.
  129. inline BOOL CKsRegKey::SetValue( LPCWSTR pwszValue, GUID guidValue )
  130. {
  131. WCHAR wszKeyName[MAX_GUIDLENGTH];
  132. GuidToWChar( guidValue, wszKeyName );
  133. return SetValue( pwszValue, wszKeyName );
  134. }
  135. inline BOOL CKsRegKey::GetValue( LPCWSTR pwszGetFrom, int& nValue, int nDefault )
  136. {
  137. long lValue;
  138. if( GetValue( pwszGetFrom, lValue, nDefault ) )
  139. {
  140. nValue = (int)lValue;
  141. return TRUE;
  142. }
  143. return FALSE;
  144. }
  145. inline BOOL CKsRegKey::GetValue( LPCWSTR pwszGetFrom, GUID& guidValue )
  146. {
  147. WCHAR wszKeyName[MAX_GUIDLENGTH];
  148. BOOL bReturnCode = GetValue( pwszGetFrom, wszKeyName, SIZEOF_ARRAY( wszKeyName ) );
  149. WCharToGuid( wszKeyName, guidValue );
  150. return bReturnCode;
  151. }
  152. // Check for presence of value is pretty simple. Just try to read from it. If the value
  153. // exists, the read operation will be successful
  154. inline BOOL CKsRegKey::IsValue( LPCWSTR pwszValue )
  155. {
  156. WCHAR wTempString[MAX_GUIDLENGTH];
  157. return GetValue( pwszValue, wTempString, SIZEOF_ARRAY( wTempString ) );
  158. }
  159. // Again C++ joy. This time for >> and << operators
  160. inline CKsRegKey& operator >> ( CKsRegKey& keyReg, LPWSTR pwszValue )
  161. {
  162. // This is a little trick. Caller is supposed to take care of buffer, i.e. it should be
  163. // big enough to hold the string from registry. There is no way to specify the size
  164. // of buffer for operator, so we assume that the string is not longer than 512 characters
  165. // and that user's buffer is capable of storing all the data that system will put there
  166. USHORT ushSize = 512;
  167. keyReg.GetValue( NULL, pwszValue, ushSize );
  168. return keyReg;
  169. }
  170. inline CKsRegKey& operator >> ( CKsRegKey& keyReg, long& lValue )
  171. {
  172. USHORT ushSize = sizeof( lValue );
  173. keyReg.GetValue( NULL, (LPWSTR)&lValue, ushSize );
  174. return keyReg;
  175. }
  176. inline CKsRegKey& operator >> ( CKsRegKey& keyReg, int& nValue )
  177. {
  178. keyReg.GetValue( NULL, nValue );
  179. return keyReg;
  180. }
  181. // This is the last set of similar functions to do the same job of deleting subkey. They
  182. // exist just for convinience
  183. extern void DeleteSubKey( LPWSTR pwszKey, LPCWSTR pwszSubKey );
  184. inline void DeleteSubKey( CKsRegKey& keyReg, LPCWSTR pwszSubKey )
  185. {
  186. DeleteSubKey( keyReg.GetKey(), pwszSubKey );
  187. }
  188. inline void DeleteSubKey( LPWSTR pwszKey, GUID guidSubKey )
  189. {
  190. WCHAR wszKeyName[MAX_GUIDLENGTH];
  191. ((CKsRegKey*)NULL)->GuidToWChar( guidSubKey, wszKeyName );
  192. DeleteSubKey( pwszKey, wszKeyName );
  193. }
  194. inline void DeleteSubKey( CKsRegKey& keyReg, GUID guidSubKey )
  195. {
  196. WCHAR wszKeyName[MAX_GUIDLENGTH];
  197. ((CKsRegKey*)NULL)->GuidToWChar( guidSubKey, wszKeyName );
  198. DeleteSubKey( keyReg.GetKey(), wszKeyName );
  199. }
  200. #endif // #ifndef __DRVREG_H__