registry.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/registry.h
  3. // Purpose: Registry classes and functions
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 03.04.1998
  7. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSW_REGISTRY_H_
  11. #define _WX_MSW_REGISTRY_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_REGKEY
  14. class WXDLLIMPEXP_FWD_BASE wxOutputStream;
  15. // ----------------------------------------------------------------------------
  16. // class wxRegKey encapsulates window HKEY handle
  17. // ----------------------------------------------------------------------------
  18. class WXDLLIMPEXP_BASE wxRegKey
  19. {
  20. public:
  21. // NB: do _not_ change the values of elements in these enumerations!
  22. // registry value types (with comments from winnt.h)
  23. enum ValueType
  24. {
  25. Type_None, // No value type
  26. Type_String, // Unicode nul terminated string
  27. Type_Expand_String, // Unicode nul terminated string
  28. // (with environment variable references)
  29. Type_Binary, // Free form binary
  30. Type_Dword, // 32-bit number
  31. Type_Dword_little_endian // 32-bit number
  32. = Type_Dword, // (same as Type_DWORD)
  33. Type_Dword_big_endian, // 32-bit number
  34. Type_Link, // Symbolic Link (unicode)
  35. Type_Multi_String, // Multiple Unicode strings
  36. Type_Resource_list, // Resource list in the resource map
  37. Type_Full_resource_descriptor, // Resource list in the hardware description
  38. Type_Resource_requirements_list // ???
  39. };
  40. // predefined registry keys
  41. enum StdKey
  42. {
  43. HKCR, // classes root
  44. HKCU, // current user
  45. HKLM, // local machine
  46. HKUSR, // users
  47. HKPD, // performance data (WinNT/2K only)
  48. HKCC, // current config
  49. HKDD, // dynamic data (Win95/98 only)
  50. HKMAX
  51. };
  52. // access mode for the key
  53. enum AccessMode
  54. {
  55. Read, // read-only
  56. Write // read and write
  57. };
  58. // Different registry views supported under WOW64.
  59. enum WOW64ViewMode
  60. {
  61. // 32 bit registry for 32 bit applications, 64 bit registry
  62. // for 64 bit ones.
  63. WOW64ViewMode_Default,
  64. // Can be used in 64 bit apps to access 32 bit registry,
  65. // has no effect (i.e. treated as default) in 32 bit apps.
  66. WOW64ViewMode_32,
  67. // Can be used in 32 bit apps to access 64 bit registry,
  68. // has no effect (i.e. treated as default) in 64 bit apps.
  69. WOW64ViewMode_64
  70. };
  71. // information about standard (predefined) registry keys
  72. // number of standard keys
  73. static const size_t nStdKeys;
  74. // get the name of a standard key
  75. static const wxChar *GetStdKeyName(size_t key);
  76. // get the short name of a standard key
  77. static const wxChar *GetStdKeyShortName(size_t key);
  78. // get StdKey from root HKEY
  79. static StdKey GetStdKeyFromHkey(WXHKEY hkey);
  80. // extracts the std key prefix from the string (return value) and
  81. // leaves only the part after it (i.e. modifies the string passed!)
  82. static StdKey ExtractKeyName(wxString& str);
  83. // ctors
  84. // root key is set to HKCR (the only root key under Win16)
  85. wxRegKey(WOW64ViewMode viewMode = WOW64ViewMode_Default);
  86. // strKey is the full name of the key (i.e. starting with HKEY_xxx...)
  87. wxRegKey(const wxString& strKey,
  88. WOW64ViewMode viewMode = WOW64ViewMode_Default);
  89. // strKey is the name of key under (standard key) keyParent
  90. wxRegKey(StdKey keyParent,
  91. const wxString& strKey,
  92. WOW64ViewMode viewMode = WOW64ViewMode_Default);
  93. // strKey is the name of key under (previously created) keyParent
  94. wxRegKey(const wxRegKey& keyParent, const wxString& strKey);
  95. // dtor closes the key
  96. ~wxRegKey();
  97. // change key (closes the previously opened key if any)
  98. // the name is absolute, i.e. should start with HKEY_xxx
  99. void SetName(const wxString& strKey);
  100. // the name is relative to the parent key
  101. void SetName(StdKey keyParent, const wxString& strKey);
  102. // the name is relative to the parent key
  103. void SetName(const wxRegKey& keyParent, const wxString& strKey);
  104. // hKey should be opened and will be closed in wxRegKey dtor
  105. void SetHkey(WXHKEY hKey);
  106. // get infomation about the key
  107. // get the (full) key name. Abbreviate std root keys if bShortPrefix.
  108. wxString GetName(bool bShortPrefix = true) const;
  109. // Retrieves the registry view used by this key.
  110. WOW64ViewMode GetView() const { return m_viewMode; }
  111. // return true if the key exists
  112. bool Exists() const;
  113. // get the info about key (any number of these pointers may be NULL)
  114. bool GetKeyInfo(size_t *pnSubKeys, // number of subkeys
  115. size_t *pnMaxKeyLen, // max len of subkey name
  116. size_t *pnValues, // number of values
  117. size_t *pnMaxValueLen) const;
  118. // return true if the key is opened
  119. bool IsOpened() const { return m_hKey != 0; }
  120. // for "if ( !key ) wxLogError(...)" kind of expressions
  121. operator bool() const { return m_dwLastError == 0; }
  122. // operations on the key itself
  123. // explicitly open the key (will be automatically done by all functions
  124. // which need the key to be opened if the key is not opened yet)
  125. bool Open(AccessMode mode = Write);
  126. // create the key: will fail if the key already exists and !bOkIfExists
  127. bool Create(bool bOkIfExists = true);
  128. // rename a value from old name to new one
  129. bool RenameValue(const wxString& szValueOld, const wxString& szValueNew);
  130. // rename the key
  131. bool Rename(const wxString& szNewName);
  132. // copy value to another key possibly changing its name (by default it will
  133. // remain the same)
  134. bool CopyValue(const wxString& szValue, wxRegKey& keyDst,
  135. const wxString& szNewName = wxEmptyString);
  136. // copy the entire contents of the key recursively to another location
  137. bool Copy(const wxString& szNewName);
  138. // same as Copy() but using a key and not the name
  139. bool Copy(wxRegKey& keyDst);
  140. // close the key (will be automatically done in dtor)
  141. bool Close();
  142. // deleting keys/values
  143. // deletes this key and all of it's subkeys/values
  144. bool DeleteSelf();
  145. // deletes the subkey with all of it's subkeys/values recursively
  146. bool DeleteKey(const wxString& szKey);
  147. // deletes the named value (may be empty string to remove the default value)
  148. bool DeleteValue(const wxString& szValue);
  149. // access to values and subkeys
  150. // get value type
  151. ValueType GetValueType(const wxString& szValue) const;
  152. // returns true if the value contains a number (else it's some string)
  153. bool IsNumericValue(const wxString& szValue) const;
  154. // assignment operators set the default value of the key
  155. wxRegKey& operator=(const wxString& strValue)
  156. { SetValue(wxEmptyString, strValue); return *this; }
  157. // query the default value of the key: implicitly or explicitly
  158. wxString QueryDefaultValue() const;
  159. operator wxString() const { return QueryDefaultValue(); }
  160. // named values
  161. // set the string value
  162. bool SetValue(const wxString& szValue, const wxString& strValue);
  163. // retrieve the string value
  164. bool QueryValue(const wxString& szValue, wxString& strValue) const
  165. { return QueryValue(szValue, strValue, false); }
  166. // retrieve raw string value
  167. bool QueryRawValue(const wxString& szValue, wxString& strValue) const
  168. { return QueryValue(szValue, strValue, true); }
  169. // retrieve either raw or expanded string value
  170. bool QueryValue(const wxString& szValue, wxString& strValue, bool raw) const;
  171. // set the numeric value
  172. bool SetValue(const wxString& szValue, long lValue);
  173. // return the numeric value
  174. bool QueryValue(const wxString& szValue, long *plValue) const;
  175. // set the binary value
  176. bool SetValue(const wxString& szValue, const wxMemoryBuffer& buf);
  177. // return the binary value
  178. bool QueryValue(const wxString& szValue, wxMemoryBuffer& buf) const;
  179. // query existence of a key/value
  180. // return true if value exists
  181. bool HasValue(const wxString& szKey) const;
  182. // return true if given subkey exists
  183. bool HasSubKey(const wxString& szKey) const;
  184. // return true if any subkeys exist
  185. bool HasSubkeys() const;
  186. // return true if any values exist
  187. bool HasValues() const;
  188. // return true if the key is empty (nothing under this key)
  189. bool IsEmpty() const { return !HasSubkeys() && !HasValues(); }
  190. // enumerate values and subkeys
  191. bool GetFirstValue(wxString& strValueName, long& lIndex);
  192. bool GetNextValue (wxString& strValueName, long& lIndex) const;
  193. bool GetFirstKey (wxString& strKeyName , long& lIndex);
  194. bool GetNextKey (wxString& strKeyName , long& lIndex) const;
  195. // export the contents of this key and all its subkeys to the given file
  196. // (which won't be overwritten, it's an error if it already exists)
  197. //
  198. // note that we export the key in REGEDIT4 format, not RegSaveKey() binary
  199. // format nor newer REGEDIT5 one
  200. bool Export(const wxString& filename) const;
  201. // same as above but write to the given (opened) stream
  202. bool Export(wxOutputStream& ostr) const;
  203. // for wxRegConfig usage only: preallocate some memory for the name
  204. void ReserveMemoryForName(size_t bytes) { m_strKey.reserve(bytes); }
  205. private:
  206. // common part of all ctors
  207. void Init()
  208. {
  209. m_hKey = (WXHKEY) NULL;
  210. m_dwLastError = 0;
  211. }
  212. // recursive helper for Export()
  213. bool DoExport(wxOutputStream& ostr) const;
  214. // export a single value
  215. bool DoExportValue(wxOutputStream& ostr, const wxString& name) const;
  216. // return the text representation (in REGEDIT4 format) of the value with the
  217. // given name
  218. wxString FormatValue(const wxString& name) const;
  219. WXHKEY m_hKey, // our handle
  220. m_hRootKey; // handle of the top key (i.e. StdKey)
  221. wxString m_strKey; // key name (relative to m_hRootKey)
  222. WOW64ViewMode m_viewMode; // which view to select under WOW64
  223. AccessMode m_mode; // valid only if key is opened
  224. long m_dwLastError; // last error (0 if none)
  225. wxDECLARE_NO_COPY_CLASS(wxRegKey);
  226. };
  227. #endif // wxUSE_REGKEY
  228. #endif // _WX_MSW_REGISTRY_H_