1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/registry.h
3 // Purpose: Registry classes and functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MSW_REGISTRY_H_
13 #define _WX_MSW_REGISTRY_H_
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "registry.h"
19 class WXDLLIMPEXP_BASE wxOutputStream
;
21 // ----------------------------------------------------------------------------
22 // class wxRegKey encapsulates window HKEY handle
23 // ----------------------------------------------------------------------------
25 class WXDLLIMPEXP_BASE wxRegKey
28 // NB: do _not_ change the values of elements in these enumerations!
30 // registry value types (with comments from winnt.h)
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
// ???
49 // predefined registry keys
54 HKLM
, // local machine
58 HKPD
// performance data (WinNT/2K only)
62 HKCC
, // current config (starting from Win95/NT 4.0)
63 HKDD
// dynamic data (Win95/98 only)
67 // access mode for the key
71 Write
// read and write
74 // information about standard (predefined) registry keys
75 // number of standard keys
76 static const size_t nStdKeys
;
77 // get the name of a standard key
78 static const wxChar
*GetStdKeyName(size_t key
);
79 // get the short name of a standard key
80 static const wxChar
*GetStdKeyShortName(size_t key
);
81 // get StdKey from root HKEY
82 static StdKey
GetStdKeyFromHkey(WXHKEY hkey
);
84 // extacts the std key prefix from the string (return value) and
85 // leaves only the part after it (i.e. modifies the string passed!)
86 static StdKey
ExtractKeyName(wxString
& str
);
89 // root key is set to HKCR (the only root key under Win16)
91 // strKey is the full name of the key (i.e. starting with HKEY_xxx...)
92 wxRegKey(const wxString
& strKey
);
93 // strKey is the name of key under (standard key) keyParent
94 wxRegKey(StdKey keyParent
, const wxString
& strKey
);
95 // strKey is the name of key under (previously created) keyParent
96 wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
);
97 // dtor closes the key
100 // change key (closes the previously opened key if any)
101 // the name is absolute, i.e. should start with HKEY_xxx
102 void SetName(const wxString
& strKey
);
103 // the name is relative to the parent key
104 void SetName(StdKey keyParent
, const wxString
& strKey
);
105 // the name is relative to the parent key
106 void SetName(const wxRegKey
& keyParent
, const wxString
& strKey
);
107 // hKey should be opened and will be closed in wxRegKey dtor
108 void SetHkey(WXHKEY hKey
);
110 // get infomation about the key
111 // get the (full) key name. Abbreviate std root keys if bShortPrefix.
112 wxString
GetName(bool bShortPrefix
= true) const;
113 // return true if the key exists
115 // get the info about key (any number of these pointers may be NULL)
116 bool GetKeyInfo(size_t *pnSubKeys
, // number of subkeys
117 size_t *pnMaxKeyLen
, // max len of subkey name
118 size_t *pnValues
, // number of values
119 size_t *pnMaxValueLen
) const;
120 // return true if the key is opened
121 bool IsOpened() const { return m_hKey
!= 0; }
122 // for "if ( !key ) wxLogError(...)" kind of expressions
123 operator bool() const { return m_dwLastError
== 0; }
125 // operations on the key itself
126 // explicitly open the key (will be automatically done by all functions
127 // which need the key to be opened if the key is not opened yet)
128 bool Open(AccessMode mode
= Write
);
129 // create the key: will fail if the key already exists and !bOkIfExists
130 bool Create(bool bOkIfExists
= true);
131 // rename a value from old name to new one
132 bool RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
);
134 bool Rename(const wxChar
*szNewName
);
135 // copy value to another key possibly changing its name (by default it will
137 bool CopyValue(const wxChar
*szValue
, wxRegKey
& keyDst
,
138 const wxChar
*szNewName
= NULL
);
139 // copy the entire contents of the key recursively to another location
140 bool Copy(const wxChar
*szNewName
);
141 // same as Copy() but using a key and not the name
142 bool Copy(wxRegKey
& keyDst
);
143 // close the key (will be automatically done in dtor)
146 // deleting keys/values
147 // deletes this key and all of it's subkeys/values
149 // deletes the subkey with all of it's subkeys/values recursively
150 bool DeleteKey(const wxChar
*szKey
);
151 // deletes the named value (may be NULL to remove the default value)
152 bool DeleteValue(const wxChar
*szValue
);
154 // access to values and subkeys
156 ValueType
GetValueType(const wxChar
*szValue
) const;
157 // returns true if the value contains a number (else it's some string)
158 bool IsNumericValue(const wxChar
*szValue
) const;
160 // assignment operators set the default value of the key
161 wxRegKey
& operator=(const wxString
& strValue
)
162 { SetValue(NULL
, strValue
); return *this; }
163 wxRegKey
& operator=(long lValue
)
164 { SetValue(NULL
, lValue
); return *this; }
166 // query the default value of the key: implicitly or explicitly
167 wxString
QueryDefaultValue() const;
168 operator wxString() const { return QueryDefaultValue(); }
172 // set the string value
173 bool SetValue(const wxChar
*szValue
, const wxString
& strValue
);
174 // retrieve the string value
175 bool QueryValue(const wxChar
*szValue
, wxString
& strValue
) const
176 { return QueryValue(szValue
, strValue
, false); }
177 // retrieve raw string value
178 bool QueryRawValue(const wxChar
*szValue
, wxString
& strValue
) const
179 { return QueryValue(szValue
, strValue
, true); }
180 // retrieve either raw or expanded string value
181 bool QueryValue(const wxChar
*szValue
, wxString
& strValue
, bool raw
) const;
183 // set the numeric value
184 bool SetValue(const wxChar
*szValue
, long lValue
);
185 // return the numeric value
186 bool QueryValue(const wxChar
*szValue
, long *plValue
) const;
187 // set the binary value
188 bool SetValue(const wxChar
*szValue
, const wxMemoryBuffer
& buf
);
189 // return the binary value
190 bool QueryValue(const wxChar
*szValue
, wxMemoryBuffer
& buf
) const;
192 // query existence of a key/value
193 // return true if value exists
194 bool HasValue(const wxChar
*szKey
) const;
195 // return true if given subkey exists
196 bool HasSubKey(const wxChar
*szKey
) const;
197 // return true if any subkeys exist
198 bool HasSubkeys() const;
199 // return true if any values exist
200 bool HasValues() const;
201 // return true if the key is empty (nothing under this key)
202 bool IsEmpty() const { return !HasSubkeys() && !HasValues(); }
204 // enumerate values and subkeys
205 bool GetFirstValue(wxString
& strValueName
, long& lIndex
);
206 bool GetNextValue (wxString
& strValueName
, long& lIndex
) const;
208 bool GetFirstKey (wxString
& strKeyName
, long& lIndex
);
209 bool GetNextKey (wxString
& strKeyName
, long& lIndex
) const;
211 // export the contents of this key and all its subkeys to the given file
212 // (which won't be overwritten, it's an error if it already exists)
214 // note that we export the key in REGEDIT4 format, not RegSaveKey() binary
215 // format nor newer REGEDIT5 one
216 bool Export(const wxString
& filename
) const;
218 // same as above but write to the given (opened) stream
219 bool Export(wxOutputStream
& ostr
) const;
222 // for wxRegConfig usage only: preallocate some memory for the name
223 void ReserveMemoryForName(size_t bytes
) { m_strKey
.reserve(bytes
); }
226 // common part of all ctors
229 m_hKey
= (WXHKEY
) NULL
;
233 // recursive helper for Export()
234 bool DoExport(wxOutputStream
& ostr
) const;
236 // export a single value
237 bool DoExportValue(wxOutputStream
& ostr
, const wxString
& name
) const;
239 // return the text representation (in REGEDIT4 format) of the value with the
241 wxString
FormatValue(const wxString
& name
) const;
244 WXHKEY m_hKey
, // our handle
245 m_hRootKey
; // handle of the top key (i.e. StdKey)
246 wxString m_strKey
; // key name (relative to m_hRootKey)
248 long m_dwLastError
; // last error (0 if none)
251 DECLARE_NO_COPY_CLASS(wxRegKey
)
254 #endif // _WX_MSW_REGISTRY_H_