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.

141 lines
5.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Copyright (C) Silicon Prairie Software, 1996
  4. //
  5. // File: simreg.h
  6. //
  7. // Contents: CRegFile
  8. //
  9. // Simple Win32/Win16 registry manipulation class
  10. // Copyright (c)1996, Shaun Ivory
  11. // All rights reserved
  12. // Used with permission
  13. //
  14. // History: Sep-26-96 Davepl Created
  15. //
  16. //--------------------------------------------------------------------------
  17. #pragma once
  18. #include "shtl.h"
  19. #include "tstring.h"
  20. #include <winreg.h>
  21. #if defined(WIN32) || defined(_WIN32)
  22. #define SIMREG_WIN32
  23. #else
  24. #define SIMREG_WIN16
  25. #endif
  26. #define INVALID_HKEY ((HKEY)-1)
  27. class CSimpleReg
  28. {
  29. private:
  30. tstring m_SubKeyName;
  31. HKEY m_hKeyRoot;
  32. HKEY m_hSubKey;
  33. unsigned char m_bOpenSuccess;
  34. unsigned char m_bCreate;
  35. #if defined(SIMREG_WIN32)
  36. LPSECURITY_ATTRIBUTES m_lpsaSecurityAttributes;
  37. #endif
  38. private:
  39. // Some of the Reg... functions inexplicably require non const strings
  40. static LPTSTR Totstring( const tstring &s ) { return (LPTSTR)(LPCTSTR)s; }
  41. unsigned char Assign( const CSimpleReg &other );
  42. public:
  43. enum
  44. {
  45. PreOrder=0,
  46. PostOrder=1
  47. };
  48. struct CKeyEnumInfo
  49. {
  50. tstring Name;
  51. HKEY Root;
  52. int Level;
  53. void *ExtraData;
  54. };
  55. struct CValueEnumInfo
  56. {
  57. tstring Name;
  58. DWORD Type;
  59. DWORD Size;
  60. void *ExtraData;
  61. };
  62. typedef int (*SimRegKeyEnumProc)( CKeyEnumInfo &enumInfo );
  63. typedef int (*SimRegValueEnumProc)( CValueEnumInfo &enumInfo );
  64. // Constructors, destructor
  65. #if defined(SIMREG_WIN32)
  66. CSimpleReg( HKEY, const tstring&, unsigned char forceCreate=0, LPSECURITY_ATTRIBUTES lpsa=NULL );
  67. #else
  68. CSimpleReg( HKEY, const tstring&, unsigned char forceCreate=0 );
  69. #endif
  70. CSimpleReg(void);
  71. CSimpleReg(const CSimpleReg &other);
  72. ~CSimpleReg(void);
  73. CSimpleReg &operator=(const CSimpleReg& );
  74. // Query functions
  75. unsigned long Size( const tstring &key );
  76. unsigned long Type( const tstring &key );
  77. unsigned char Query( const tstring &key, LPTSTR szValue, unsigned short maxLen );
  78. unsigned char Query( const tstring &key, tstring &value, unsigned short maxLen=0 );
  79. unsigned char Query( const tstring &key, DWORD &value );
  80. unsigned char Query( const tstring &key, int &value );
  81. unsigned char Query( const tstring &key, LONG &value );
  82. unsigned char Query( const tstring &key, BYTE &value );
  83. unsigned char Query( const tstring &key, WORD &value );
  84. unsigned char QueryBin( const tstring &key, void *value, DWORD size );
  85. void Query( const tstring &key, DWORD &value, const DWORD &defaultvalue );
  86. template<class _TYPE> _TYPE Query( const tstring &key, const _TYPE &defaultvalue )
  87. {
  88. _TYPE result;
  89. if (Query(key, result))
  90. return result;
  91. else
  92. return defaultvalue;
  93. }
  94. // Set functions
  95. unsigned char SetBin( const tstring &key, void *value, DWORD size );
  96. unsigned char Set( const tstring &key, const tstring &value );
  97. unsigned char Set( const tstring &key, DWORD value );
  98. unsigned char Open(void);
  99. unsigned char Close(void);
  100. unsigned char ForceCreate( unsigned char create = TRUE );
  101. unsigned char SetRoot( HKEY keyClass, const tstring &newRoot );
  102. #if defined(SIMREG_WIN32)
  103. void SecurityAttributes( const LPSECURITY_ATTRIBUTES lpsa ) { m_lpsaSecurityAttributes = lpsa; }
  104. const LPSECURITY_ATTRIBUTES SecurityAttributes(void) const { return m_lpsaSecurityAttributes; }
  105. #endif
  106. DWORD CountKeys(void);
  107. static HKEY GetHkeyFromName( const tstring &Name );
  108. static unsigned char Delete( HKEY, const tstring &key );
  109. static int DeleteRecursively( HKEY root, const tstring &name );
  110. // Inlines
  111. tstring SubKeyName(void) const {return (m_SubKeyName);}
  112. unsigned char ForceCreate(void) const {return (m_bCreate);}
  113. unsigned char OK(void) const {return (m_bOpenSuccess);}
  114. HKEY GetRootKey(void) const {return (m_hKeyRoot);}
  115. HKEY GetSubKey(void) const {return (m_hSubKey);}
  116. int operator!(void) const {return (!m_bOpenSuccess);}
  117. static HKEY GetWin16HKey( HKEY key );
  118. int EnumValues( SimRegValueEnumProc enumProc, void *extraInfo = NULL );
  119. int RecurseKeys( SimRegKeyEnumProc enumProc, void *extraInfo = NULL, int recurseOrder = CSimpleReg::PostOrder, int failOnOpenError = 1 );
  120. int EnumKeys( SimRegKeyEnumProc enumProc, void *extraInfo = NULL, int failOnOpenError = 1 );
  121. private:
  122. static int DoRecurseKeys( HKEY key, const tstring &root, SimRegKeyEnumProc enumProc, void *extraInfo, int level, int recurseOrder, int failOnOpenError );
  123. static int DoEnumKeys( HKEY key, const tstring &root, SimRegKeyEnumProc enumProc, void *extraInfo, int failOnOpenError );
  124. static int DeleteEnumKeyProc( CSimpleReg::CKeyEnumInfo &enumInfo );
  125. };