1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/registry.h
3 // Purpose: Registry classes and functions
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_MSW_REGISTRY_H_
12 #define _WX_MSW_REGISTRY_H_
18 class WXDLLIMPEXP_FWD_BASE wxOutputStream
;
20 // ----------------------------------------------------------------------------
21 // class wxRegKey encapsulates window HKEY handle
22 // ----------------------------------------------------------------------------
24 class WXDLLIMPEXP_BASE wxRegKey
27 // NB: do _not_ change the values of elements in these enumerations!
29 // registry value types (with comments from winnt.h)
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
// ???
48 // predefined registry keys
53 HKLM
, // local machine
55 HKPD
, // performance data (WinNT/2K only)
56 HKCC
, // current config
57 HKDD
, // dynamic data (Win95/98 only)
61 // access mode for the key
65 Write
// read and write
68 // Different registry views supported under WOW64.
71 // 32 bit registry for 32 bit applications, 64 bit registry
73 WOW64ViewMode_Default
,
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.
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.
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
);
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
);
99 // root key is set to HKCR (the only root key under Win16)
100 wxRegKey(WOW64ViewMode viewMode
= WOW64ViewMode_Default
);
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
);
106 // strKey is the name of key under (standard key) keyParent
107 wxRegKey(StdKey keyParent
,
108 const wxString
& strKey
,
109 WOW64ViewMode viewMode
= WOW64ViewMode_Default
);
111 // strKey is the name of key under (previously created) keyParent
112 wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
);
113 // dtor closes the key
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
);
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
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; }
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
);
152 bool Rename(const wxString
& szNewName
);
153 // copy value to another key possibly changing its name (by default it will
155 bool CopyValue(const wxString
& szValue
, wxRegKey
& keyDst
,
156 const wxString
& szNewName
= wxEmptyString
);
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)
165 // deleting keys/values
166 // deletes this key and all of it's subkeys/values
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
);
173 // access to values and subkeys
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;
179 // assignment operators set the default value of the key
180 wxRegKey
& operator=(const wxString
& strValue
)
181 { SetValue(wxEmptyString
, strValue
); return *this; }
183 // query the default value of the key: implicitly or explicitly
184 wxString
QueryDefaultValue() const;
185 operator wxString() const { return QueryDefaultValue(); }
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;
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;
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(); }
221 // enumerate values and subkeys
222 bool GetFirstValue(wxString
& strValueName
, long& lIndex
);
223 bool GetNextValue (wxString
& strValueName
, long& lIndex
) const;
225 bool GetFirstKey (wxString
& strKeyName
, long& lIndex
);
226 bool GetNextKey (wxString
& strKeyName
, long& lIndex
) const;
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)
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;
235 // same as above but write to the given (opened) stream
236 bool Export(wxOutputStream
& ostr
) const;
239 // for wxRegConfig usage only: preallocate some memory for the name
240 void ReserveMemoryForName(size_t bytes
) { m_strKey
.reserve(bytes
); }
243 // common part of all ctors
246 m_hKey
= (WXHKEY
) NULL
;
250 // recursive helper for Export()
251 bool DoExport(wxOutputStream
& ostr
) const;
253 // export a single value
254 bool DoExportValue(wxOutputStream
& ostr
, const wxString
& name
) const;
256 // return the text representation (in REGEDIT4 format) of the value with the
258 wxString
FormatValue(const wxString
& name
) const;
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)
269 wxDECLARE_NO_COPY_CLASS(wxRegKey
);
272 #endif // wxUSE_REGKEY
274 #endif // _WX_MSW_REGISTRY_H_