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.

441 lines
13 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright � Microsoft Corporation. All rights reserved.
  4. //
  5. // cregcls.h
  6. //
  7. // Purpose: registry wrapper class
  8. //
  9. //***************************************************************************
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif
  13. #ifndef _CREGCLS_H_
  14. #define _CREGCLS_H_
  15. #include <Polarity.h>
  16. #include <CHString.h>
  17. #include <chstrarr.h>
  18. #include <chptrarr.h>
  19. #define MAX_VALUE_NAME (1024)
  20. #define NULL_DWORD ((DWORD)0L)
  21. #define MAX_SUBKEY_BUFFERSIZE (255+1) // Per spec
  22. #define QUOTE L"\""
  23. #define CSTRING_PTR (1)
  24. class POLARITY CRegistry
  25. {
  26. public:
  27. CRegistry (); // Constructor
  28. ~CRegistry (); // Destructor
  29. // Opens the key and subkey using the desired access mask
  30. LONG Open (
  31. HKEY hRootKey, // handle of open key
  32. LPCWSTR lpszSubKey, // address of name of subkey to open
  33. REGSAM samDesired // Access mask
  34. );
  35. // Version that properly opens the user key appropriate
  36. // to the current thread
  37. DWORD OpenCurrentUser(
  38. LPCWSTR lpszSubKey, // address of name of subkey to open
  39. REGSAM samDesired); // Access mask
  40. // Generalized RegCreateKeyEx form
  41. LONG CreateOpen (
  42. HKEY hInRootKey,
  43. LPCWSTR lpszSubKey,
  44. LPWSTR lpClass = NULL,
  45. DWORD dwOptions = REG_OPTION_NON_VOLATILE,
  46. REGSAM samDesired = KEY_ALL_ACCESS,
  47. LPSECURITY_ATTRIBUTES lpSecurityAttrib = NULL,
  48. LPDWORD pdwDisposition = NULL
  49. );
  50. // Deletes the specified subkey or the opened root
  51. LONG DeleteKey (
  52. CHString *pchsSubKeyPath = NULL
  53. );
  54. // Deletes the specified value within the createopened portion of the registry
  55. LONG DeleteValue (
  56. LPCWSTR pValueName
  57. );
  58. // Opens the key but forces the enumation of subkeys flag
  59. //=======================================================
  60. LONG OpenAndEnumerateSubKeys (
  61. HKEY hInRootKey,
  62. LPCWSTR lpszSubKey,
  63. REGSAM samDesired
  64. );
  65. LONG EnumerateAndGetValues (
  66. DWORD &dwIndexOfValue,
  67. WCHAR *&pValueName,
  68. BYTE *&pValueData
  69. );
  70. void Close ( void ) ;
  71. // Information Functions
  72. // Having a key, but no class name is legal so just return a null string
  73. // if there has been no class name set
  74. //======================================================================
  75. HKEY GethKey ( void ) { return hKey; }
  76. WCHAR *GetClassName ( void ) { return ( ClassName ) ; }
  77. DWORD GetCurrentSubKeyCount ( void ) { return ( dwcSubKeys ) ; }
  78. DWORD GetLongestSubKeySize ( void ) { return ( dwcMaxSubKey ) ; }
  79. DWORD GetLongestClassStringSize ( void ) { return ( dwcMaxClass ) ; }
  80. DWORD GetValueCount ( void ) { return ( dwcValues ) ; }
  81. DWORD GetLongestValueName ( void ) { return ( dwcMaxValueName ) ; }
  82. DWORD GetLongestValueData ( void ) { return ( dwcMaxValueData ) ; }
  83. DWORD GetCurrentKeyValue ( LPCWSTR pValueName , CHString &DestValue ) ;
  84. DWORD GetCurrentKeyValue ( LPCWSTR pValueName , DWORD &DestValue ) ;
  85. DWORD GetCurrentKeyValue ( LPCWSTR pValueName , CHStringArray &DestValue ) ;
  86. DWORD SetCurrentKeyValue ( LPCWSTR pValueName , CHString &DestValue ) ;
  87. DWORD SetCurrentKeyValue ( LPCWSTR pValueName , DWORD &DestValue ) ;
  88. DWORD SetCurrentKeyValue ( LPCWSTR pValueName , CHStringArray &DestValue ) ;
  89. DWORD GetCurrentBinaryKeyValue ( LPCWSTR pValueName , CHString &chsDest ) ;
  90. DWORD GetCurrentBinaryKeyValue ( LPCWSTR pValueName , LPBYTE pbDest , LPDWORD pSizeOfDestValue ) ;
  91. DWORD GetCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName , CHString &DestValue ) ;
  92. DWORD GetCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName , DWORD &DestValue ) ;
  93. DWORD GetCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName , CHStringArray &DestValue ) ;
  94. DWORD SetCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName , CHString &DestValue ) ;
  95. DWORD SetCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName , DWORD &DestValue ) ;
  96. DWORD SetCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName , CHStringArray &DestValue ) ;
  97. DWORD SetCurrentKeyValueExpand ( HKEY UseKey , LPCWSTR pValueName , CHString &DestValue ) ;
  98. DWORD GetCurrentBinaryKeyValue ( HKEY UseKey , LPCWSTR pValueName , LPBYTE pbDest , LPDWORD pSizeOfDestValue ) ;
  99. DWORD DeleteCurrentKeyValue ( LPCWSTR pValueName ) ;
  100. DWORD DeleteCurrentKeyValue ( HKEY UseKey , LPCWSTR pValueName ) ;
  101. // Subkey functions
  102. //=================
  103. void RewindSubKeys ( void ) ;
  104. DWORD GetCurrentSubKeyName ( CHString &DestSubKeyName ) ;
  105. DWORD GetCurrentSubKeyValue ( LPCWSTR pValueName, void *pDestValue , LPDWORD pSizeOfDestValue ) ;
  106. DWORD GetCurrentSubKeyValue ( LPCWSTR pValueName, CHString &DestValue ) ;
  107. DWORD GetCurrentSubKeyValue ( LPCWSTR pValueName, DWORD &DestValue ) ;
  108. DWORD NextSubKey ( void ) ;
  109. DWORD GetCurrentSubKeyPath ( CHString &DestSubKeyPath ) ;
  110. LONG OpenLocalMachineKeyAndReadValue (
  111. LPCWSTR lpszSubKey ,
  112. LPCWSTR pValueName,
  113. CHString &DestValue
  114. );
  115. private:
  116. // Private functions
  117. //==================
  118. // Set the member variables to their default state
  119. //================================================
  120. void SetDefaultValues ( void ) ;
  121. // Open and close the subkey
  122. // =========================
  123. DWORD OpenSubKey ( void ) ;
  124. void CloseSubKey ( void ) ;
  125. // Given a good key gets the value
  126. // ===============================
  127. DWORD GetCurrentRawKeyValue (
  128. HKEY UseKey,
  129. LPCWSTR pValueName,
  130. void *pDestValue,
  131. LPDWORD pValueType,
  132. LPDWORD pSizeOfDestValue
  133. ) ;
  134. DWORD GetCurrentRawSubKeyValue (
  135. LPCWSTR pValueName,
  136. void *pDestValue,
  137. LPDWORD pValueType,
  138. LPDWORD pSizeOfDestValue
  139. ) ;
  140. // Init static vars
  141. // ================
  142. static DWORD WINAPI GetPlatformID ( void ) ;
  143. // MultiPlatform support
  144. // =====================
  145. LONG myRegCreateKeyEx (
  146. HKEY hKey,
  147. LPCWSTR lpwcsSubKey,
  148. DWORD Reserved,
  149. LPWSTR lpwcsClass,
  150. DWORD dwOptions,
  151. REGSAM samDesired,
  152. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  153. PHKEY phkResult,
  154. LPDWORD lpdwDisposition
  155. );
  156. LONG myRegSetValueEx (
  157. HKEY hKey,
  158. LPCWSTR lpwcsSubKey,
  159. DWORD Reserved,
  160. DWORD dwType,
  161. CONST BYTE *lpData,
  162. DWORD cbData
  163. );
  164. LONG myRegQueryValueEx (
  165. HKEY hKey,
  166. LPCWSTR lpwcsSubKey,
  167. LPDWORD Reserved,
  168. LPDWORD dwType,
  169. LPBYTE lpData,
  170. LPDWORD cbData
  171. );
  172. LONG myRegEnumKey (
  173. HKEY hKey,
  174. DWORD dwIndex,
  175. LPWSTR lpwcsName,
  176. DWORD cbData
  177. );
  178. LONG myRegDeleteValue (
  179. HKEY hKey,
  180. LPCWSTR lpwcsName
  181. ) ;
  182. LONG myRegDeleteKey (
  183. HKEY hKey,
  184. LPCWSTR lpwcsName
  185. );
  186. LONG myRegOpenKeyEx (
  187. HKEY hKey,
  188. LPCWSTR lpwcsSubKey,
  189. DWORD ulOptions,
  190. REGSAM samDesired,
  191. PHKEY phkResult
  192. );
  193. LONG myRegQueryInfoKey (
  194. HKEY hKey,
  195. LPWSTR lpwstrClass,
  196. LPDWORD lpcbClass,
  197. LPDWORD lpReserved,
  198. LPDWORD lpcSubKeys,
  199. LPDWORD lpcbMaxSubKeyLen,
  200. LPDWORD lpcbMaxClassLen,
  201. LPDWORD lpcValues,
  202. LPDWORD lpcbMaxValueNameLen,
  203. LPDWORD lpcbMaxValueLen,
  204. LPDWORD lpcbSecurityDescriptor,
  205. PFILETIME lpftLastWriteTime
  206. );
  207. LONG myRegEnumValue (
  208. HKEY hKey,
  209. DWORD dwIndex,
  210. LPWSTR lpValueName,
  211. LPDWORD lpcbValueName,
  212. LPDWORD lpReserved,
  213. LPDWORD lpType,
  214. LPBYTE lpData,
  215. LPDWORD lpcbData
  216. );
  217. // In the event the caller is REUSING this instance,
  218. // close the existing key and reset values to the default
  219. // in preparation to REOPEN this instance
  220. //=======================================================
  221. void PrepareToReOpen ( void ) ;
  222. // Private data
  223. //=============
  224. HKEY hRootKey; // Current root key for cla
  225. HKEY hKey; // Current active key
  226. HKEY hSubKey; // Current active subkey
  227. static DWORD s_dwPlatform; // Currently running OS
  228. CHString RootKeyPath; // Current path to root assigned by open
  229. DWORD CurrentSubKeyIndex; // Current subkey being indexed
  230. bool m_fFromCurrentUser; // allows check on whether to free
  231. // hRootKey member based on whether
  232. // its value was populated via a call
  233. // to ::RegOpenCurrentUser.
  234. // Information about this class
  235. //=============================
  236. WCHAR ClassName[MAX_PATH]; // Buffer for class name.
  237. DWORD dwcClassLen; // Length of class string.
  238. DWORD dwcSubKeys; // Number of sub keys.
  239. DWORD dwcMaxSubKey; // Longest sub key size.
  240. DWORD dwcMaxClass; // Longest class string.
  241. DWORD dwcValues; // Number of values for this key.
  242. DWORD dwcMaxValueName; // Longest Value name.
  243. DWORD dwcMaxValueData; // Longest Value data.
  244. DWORD dwcSecDesc; // Security descriptor.
  245. FILETIME ftLastWriteTime; // Last write time.
  246. };
  247. //*********************************************************************
  248. //
  249. // CLASS: CRegistrySearch
  250. //
  251. // Description: This class uses the CRegistry Class to search
  252. // through the registry to build a list of keys
  253. // for the requested value, or requested full key
  254. // name, or requested partial key name. This class
  255. // allocates CHString objects and puts them in the
  256. // users CHPtrArray. The user is responsible for
  257. // deleting the memory allocated, the FreeSearchList
  258. // function can accomplish this, or the user must
  259. // remember to delete every object in the array
  260. // before deallocating the array.
  261. //
  262. //
  263. //=====================================================================
  264. //
  265. // Note: Private functions are documented in the .CPP file
  266. //
  267. //=====================================================================
  268. //
  269. // Public functions
  270. //
  271. //=====================================================================
  272. //
  273. // BOOL SearchAndBuildList( CHString chsRootKey,
  274. // CHPtrArray & cpaList,
  275. // CHString chsSearchString,
  276. // CHString chsValueString,
  277. // int nSearchType );
  278. //
  279. // Parameters:
  280. // chsRootKey - The root key to start the search from.
  281. // Note: At this point in time, we just
  282. // search thru HKEY_LOCAL_MACHINE, this
  283. // can be changed when needed.
  284. // cpaList - The reference to the CHPtrArray to put
  285. // the list of keys that matched the search
  286. // criteria.
  287. // chsSearchString - The string to search for
  288. // chsValueString - The value to open and see if it matches what is
  289. // chsSearchString
  290. // nSearchType - The type of search, the following are
  291. // supported:
  292. // KEY_FULL_MATCH_SEARCH
  293. // Only keys that match the chsSearchString
  294. // KEY_PARTIAL_MATCH_SEARCH
  295. // Keys that have chsSearchString anywhere in them
  296. // VALUE_SEARCH
  297. // Values that match chsSearchString
  298. //*********************************************************************
  299. #define KEY_FULL_MATCH_SEARCH 1
  300. #define KEY_PARTIAL_MATCH_SEARCH 2
  301. #define VALUE_SEARCH 3
  302. class POLARITY CRegistrySearch
  303. {
  304. private:
  305. void CheckAndAddToList (
  306. CRegistry * pReg,
  307. CHString chsSubKey,
  308. CHString chsFullKey,
  309. CHPtrArray & chpaList,
  310. CHString chsSearchString,
  311. CHString chsValueString,
  312. int nSearchType
  313. );
  314. int m_nSearchType ;
  315. CHString m_chsSearchString ;
  316. CHPtrArray m_cpaList ;
  317. public:
  318. CRegistrySearch () ;
  319. ~CRegistrySearch () ;
  320. BOOL SearchAndBuildList (
  321. CHString chsRootKey,
  322. CHPtrArray & cpaList,
  323. CHString chsSearchString,
  324. CHString chsValueString,
  325. int nSearchType,
  326. HKEY hkDefault = HKEY_LOCAL_MACHINE
  327. );
  328. BOOL FreeSearchList (
  329. int nType,
  330. CHPtrArray & cpaList
  331. ) ;
  332. BOOL LocateKeyByNameOrValueName (
  333. HKEY hKeyParent,
  334. LPCWSTR pszKeyName,
  335. LPCWSTR pszSubKeyName,
  336. LPCWSTR *ppszValueNames,
  337. DWORD dwNumValueNames,
  338. CHString &strFoundKeyName,
  339. CHString &strFoundKeyPath
  340. ) ;
  341. } ;
  342. #endif