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