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.

209 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. reg.hxx
  5. Abstract:
  6. contains class definition for registry access.
  7. Author:
  8. Madan Appiah (madana) 19-Dec-1994
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. #ifndef CACHE_REG
  14. #define CACHE_REG
  15. #define DEFAULT_KEY_ACCESS ( KEY_QUERY_VALUE | \
  16. KEY_SET_VALUE | \
  17. KEY_CREATE_SUB_KEY | \
  18. KEY_ENUMERATE_SUB_KEYS )
  19. #define BASIC_ACCESS ( KEY_QUERY_VALUE | \
  20. KEY_ENUMERATE_SUB_KEYS )
  21. #define DEFAULT_CLASS TEXT("DefaultClass")
  22. #define DEFAULT_CLASS_SIZE sizeof(DEFAULT_CLASS)
  23. #define MAX_KEY_SIZE 64 + 1
  24. #define SERVICES_KEY \
  25. TEXT("System\\CurrentControlSet\\Services\\")
  26. #define FAIL_IF_KEY_NOT_EXISTS 0
  27. #define CREATE_KEY_IF_NOT_EXISTS 1
  28. typedef struct _KEY_QUERY_INFO {
  29. TCHAR Class[DEFAULT_CLASS_SIZE];
  30. DWORD ClassSize;
  31. DWORD NumSubKeys;
  32. DWORD MaxSubKeyLen;
  33. DWORD MaxClassLen;
  34. DWORD NumValues;
  35. DWORD MaxValueNameLen;
  36. DWORD MaxValueLen;
  37. DWORD SecurityDescriptorLen;
  38. FILETIME LastWriteTime;
  39. } KEY_QUERY_INFO, *LPKEY_QUERY_INFO;
  40. /*++
  41. Class Description:
  42. Defines a REGISTRY class that manipulates the registry keys.
  43. Public Member functions:
  44. Create : is a overloaded function that creates a subkey.
  45. GetValue : is a overloaded function that retrieves REG_DWORD,
  46. REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ AND REG_BINARY data values.
  47. SetValue : is a overloaded function that sets REG_DWORD,
  48. REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ AND REG_BINARY data values.
  49. GetNumSubKeys : returns number of subkeys under this key object.
  50. DeleteKey : deletes a subkey node.
  51. FindFirstKey : returns the first subkey of this key.
  52. FindNextKey : returns the next subkey of this key.
  53. --*/
  54. class REGISTRY_OBJ {
  55. private:
  56. HKEY _RegHandle;
  57. DWORD _Status;
  58. DWORD _Index;
  59. DWORD _ValIndex;
  60. DWORD _dwAccess;
  61. public:
  62. REGISTRY_OBJ( HKEY Handle = NULL, DWORD Error = ERROR_SUCCESS );
  63. REGISTRY_OBJ( HKEY ParentHandle, LPTSTR KeyName, DWORD dwFlags = FAIL_IF_KEY_NOT_EXISTS )
  64. {
  65. _dwAccess = DEFAULT_KEY_ACCESS;
  66. _RegHandle = NULL;
  67. WorkWith(ParentHandle, KeyName, dwFlags);
  68. }
  69. REGISTRY_OBJ( REGISTRY_OBJ *ParentObj, LPTSTR KeyName, DWORD dwFlags = FAIL_IF_KEY_NOT_EXISTS)
  70. {
  71. _dwAccess = DEFAULT_KEY_ACCESS;
  72. _RegHandle = NULL;
  73. WorkWith(ParentObj, KeyName, dwFlags);
  74. }
  75. ~REGISTRY_OBJ( VOID ) {
  76. if( _RegHandle != NULL ) {
  77. REGCLOSEKEY( _RegHandle );
  78. }
  79. return;
  80. };
  81. DWORD WorkWith( HKEY ParentHandle, LPTSTR KeyName, DWORD dwFlags = FAIL_IF_KEY_NOT_EXISTS, DWORD dwAccess = DEFAULT_KEY_ACCESS);
  82. DWORD WorkWith( REGISTRY_OBJ *ParentObj, LPTSTR KeyName, DWORD dwFlags = FAIL_IF_KEY_NOT_EXISTS );
  83. DWORD GetStatus( VOID ) {
  84. return( _Status );
  85. };
  86. DWORD GetAccessFlags( VOID ) {
  87. return( _dwAccess );
  88. };
  89. DWORD Create( LPTSTR ChildName, HKEY *pChildHandle = NULL );
  90. DWORD Create( LPTSTR ChildName, REGISTRY_OBJ **ChildObj );
  91. DWORD Create( LPTSTR ChildName, REGISTRY_OBJ **ChildObj, DWORD *KeyDisposition );
  92. DWORD GetValue( LPTSTR ValueName, DWORD *Data );
  93. DWORD GetValue( LPTSTR ValueName, LPTSTR *Data, DWORD *NumStrings );
  94. DWORD GetValue( LPTSTR ValueName, LPBYTE *Data, DWORD *DataLen );
  95. DWORD GetValue( LPTSTR ValueName, LPBYTE Data, DWORD *DataLen );
  96. DWORD SetValue( LPTSTR ValueName, LPDWORD Data );
  97. DWORD SetValue( LPTSTR ValueName, LPTSTR Data, DWORD StringType );
  98. DWORD SetValue( LPSTR ValueName, LPSTR Data, DWORD DataLen, DWORD StringType );
  99. DWORD SetValue( LPTSTR ValueName, LPBYTE Data, DWORD DataLen );
  100. DWORD GetKeyInfo( LPKEY_QUERY_INFO QueryInfo ) {
  101. DWORD Error;
  102. QueryInfo->ClassSize = DEFAULT_CLASS_SIZE;
  103. Error = RegQueryInfoKey(
  104. _RegHandle,
  105. QueryInfo->Class,
  106. &QueryInfo->ClassSize,
  107. NULL,
  108. &QueryInfo->NumSubKeys,
  109. &QueryInfo->MaxSubKeyLen,
  110. &QueryInfo->MaxClassLen,
  111. &QueryInfo->NumValues,
  112. &QueryInfo->MaxValueNameLen,
  113. &QueryInfo->MaxValueLen,
  114. &QueryInfo->SecurityDescriptorLen,
  115. &QueryInfo->LastWriteTime
  116. );
  117. return( Error );
  118. }
  119. DWORD GetNumSubKeys(DWORD *NumSubKeys ) {
  120. DWORD Error;
  121. KEY_QUERY_INFO QueryInfo;
  122. Error = GetKeyInfo( &QueryInfo );
  123. if( Error == ERROR_SUCCESS ) {
  124. *NumSubKeys = QueryInfo.NumSubKeys;
  125. }
  126. return( Error );
  127. }
  128. DWORD DeleteKey( LPTSTR ChildKeyName );
  129. DWORD DeleteValue( LPTSTR ValueName );
  130. DWORD FindNextKey( LPTSTR Key, DWORD KeySize );
  131. DWORD FindFirstKey( LPTSTR Key, DWORD KeySize ) {
  132. _Index = 0;
  133. return( FindNextKey(Key, KeySize) );
  134. };
  135. DWORD FindNextValue( LPSTR ValueName, DWORD ValueSize,
  136. LPBYTE Data, DWORD *DataLen );
  137. DWORD FindFirstValue( LPSTR ValueName, DWORD ValueSize, LPBYTE Data,
  138. DWORD *DataLen ) {
  139. _ValIndex = 0;
  140. return( FindNextValue(ValueName, ValueSize, Data, DataLen ) );
  141. };
  142. DWORD GetValueSizeAndType(
  143. LPTSTR ValueName,
  144. LPDWORD ValueSize,
  145. LPDWORD ValueType )
  146. {
  147. *ValueSize = 0; // we are passing null buffer to get the size
  148. return RegQueryValueEx(
  149. _RegHandle, ValueName, 0, ValueType, NULL, ValueSize);
  150. }
  151. };
  152. #endif