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