registry.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: msw/registry.h
  3. // Purpose: interface of wxRegKey
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxRegKey
  9. wxRegKey is a class representing the Windows registry (it is only available
  10. under Windows). One can create, query and delete registry keys using this
  11. class.
  12. The Windows registry is easy to understand. There are five registry keys,
  13. namely:
  14. @li @c HKEY_CLASSES_ROOT (HKCR)
  15. @li @c HKEY_CURRENT_USER (HKCU)
  16. @li @c HKEY_LOCAL_MACHINE (HKLM)
  17. @li @c HKEY_CURRENT_CONFIG (HKCC)
  18. @li @c HKEY_USERS (HKU)
  19. After creating a key, it can hold a value. The values can be:
  20. @li String Value
  21. @li Binary Value
  22. @li DWORD Value
  23. @li Multi String Value
  24. @li Expandable String Value
  25. @onlyfor{wxmsw}
  26. @b Example:
  27. @code
  28. // This assume that the key already exists, use HasSubKey() to check
  29. // for the key existence if necessary.
  30. wxRegKey key(wxRegKey::HKLM, "Software\\MyKey");
  31. // Create a new value "MyValue" and set it to 12.
  32. key.SetValue("MyValue", 12);
  33. // Read the value back.
  34. long value;
  35. key.QueryValue("MyValue", &value);
  36. wxMessageBox(wxString::Format("%d", value), "Registry Value", wxOK);
  37. // Get the number of subkeys and enumerate them.
  38. size_t subkeys;
  39. key.GetKeyInfo(&subkeys, NULL, NULL, NULL);
  40. wxString key_name;
  41. key.GetFirstKey(key_name, 1);
  42. for(int i = 0; i < subkeys; i++)
  43. {
  44. wxMessageBox(key_name, "Subkey Name", wxOK);
  45. key.GetNextKey(key_name, 1);
  46. }
  47. @endcode
  48. @library{wxbase}
  49. @category{cfg}
  50. */
  51. class wxRegKey
  52. {
  53. public:
  54. /**
  55. Default constructor, initializes to @c HKEY_CLASSES_ROOT.
  56. The @a viewMode parameter is new since wxWidgets 2.9.2.
  57. */
  58. wxRegKey(WOW64ViewMode viewMode = WOW64ViewMode_Default);
  59. /**
  60. The constructor to set the full name of the key.
  61. The @a viewMode parameter is new since wxWidgets 2.9.2.
  62. */
  63. wxRegKey(const wxString& strKey,
  64. WOW64ViewMode viewMode = WOW64ViewMode_Default);
  65. /**
  66. The constructor to set the full name of the key using one of the
  67. standard keys, that is, HKCR, HKCU, HKLM, HKUSR, HKPD, HKCC or HKDD.
  68. The @a viewMode parameter is new since wxWidgets 2.9.2.
  69. */
  70. wxRegKey(StdKey keyParent, const wxString& strKey,
  71. WOW64ViewMode viewMode = WOW64ViewMode_Default);
  72. /**
  73. The constructor to set the full name of the key under a previously
  74. created parent. The registry view is inherited from the parent.
  75. */
  76. wxRegKey(const wxRegKey& keyParent, const wxString& strKey);
  77. /**
  78. Access modes for wxRegKey.
  79. */
  80. enum AccessMode
  81. {
  82. Read, ///< Read-only
  83. Write ///< Read and Write
  84. };
  85. /**
  86. The standard registry key enumerator.
  87. */
  88. enum StdKey
  89. {
  90. HKCR, ///< HKEY_CLASSES_ROOT
  91. HKCU, ///< HKEY_CURRENT_USER
  92. HKLM, ///< HKEY_LOCAL_MACHINE
  93. HKUSR, ///< HKEY_USERS
  94. HKPD, ///< HKEY_PERFORMANCE_DATA (Windows NT and 2K only)
  95. HKCC, ///< HKEY_CURRENT_CONFIG
  96. HKDD, ///< HKEY_DYN_DATA (Windows 95 and 98 only)
  97. HKMAX
  98. };
  99. /**
  100. The value type enumerator.
  101. */
  102. enum ValueType
  103. {
  104. Type_None, ///< No value type
  105. Type_String, ///< Unicode null-terminated string
  106. Type_Expand_String, ///< Unicode null-terminated string
  107. ///< (with environment variable references)
  108. Type_Binary, ///< Free form binary
  109. Type_Dword, ///< 32-bit number
  110. Type_Dword_little_endian, ///< 32-bit number (same as Type_Dword)
  111. Type_Dword_big_endian, ///< 32-bit number
  112. Type_Link, ///< Symbolic Link (Unicode)
  113. Type_Multi_String, ///< Multiple Unicode strings
  114. Type_Resource_list, ///< Resource list in the resource map
  115. Type_Full_resource_descriptor, ///< Resource list in the hardware description
  116. Type_Resource_requirements_list ///<
  117. };
  118. /**
  119. Used to determine how the registry will be viewed, either as
  120. 32-bit or 64-bit.
  121. @since 2.9.2
  122. */
  123. enum WOW64ViewMode
  124. {
  125. /**
  126. Uses 32-bit registry for 32-bit applications and
  127. 64-bit registry for 64-bit ones.
  128. */
  129. WOW64ViewMode_Default,
  130. /**
  131. Can be used in 64-bit apps to access the 32-bit registry,
  132. has no effect (i.e. treated as default) in 32-bit apps.
  133. */
  134. WOW64ViewMode_32,
  135. /**
  136. Can be used in 32-bit apps to access the 64-bit registry,
  137. has no effect (i.e. treated as default) in 64-bit apps.
  138. */
  139. WOW64ViewMode_64
  140. };
  141. /**
  142. Closes the key.
  143. */
  144. void Close();
  145. /**
  146. Copy the entire contents of the key recursively to another location
  147. using the name. Returns @true if successful.
  148. */
  149. bool Copy(const wxString& szNewName);
  150. /**
  151. Copy the entire contents of the key recursively to another location
  152. using the key. Returns @true if successful.
  153. */
  154. bool Copy(wxRegKey& keyDst);
  155. /**
  156. Copy the value to another key, possibly changing its name. By default
  157. it will remain the same. Returns @true if successful.
  158. */
  159. bool CopyValue(const wxString& szValue, wxRegKey& keyDst,
  160. const wxString& szNewName = wxEmptyString);
  161. /**
  162. Creates the key. Will fail if the key already exists and @a bOkIfExists
  163. is @false. Returns @true if successful.
  164. */
  165. bool Create(bool bOkIfExists = true);
  166. /**
  167. Deletes the subkey with all its subkeys and values recursively.
  168. */
  169. void DeleteKey(const wxString& szKey);
  170. /**
  171. Deletes this key and all its subkeys and values recursively.
  172. */
  173. void DeleteSelf();
  174. /**
  175. Deletes the named value or use an empty string argument to remove the
  176. default value of the key.
  177. */
  178. void DeleteValue(const wxString& szKey);
  179. /**
  180. Returns @true if the key exists.
  181. */
  182. bool Exists() const;
  183. /**
  184. Write the contents of this key and all its subkeys to the given file.
  185. (The file will not be overwritten; it's an error if it already exists.)
  186. Note that we export the key in REGEDIT4 format, not RegSaveKey() binary
  187. format nor the newer REGEDIT5. Returns @true if successful.
  188. */
  189. bool Export(const wxString& filename) const;
  190. /**
  191. Write the contents of this key and all its subkeys to the opened stream.
  192. Returns @true if successful.
  193. */
  194. bool Export(wxOutputStream& ostr) const;
  195. /**
  196. Gets the first key. Returns @true if successful.
  197. */
  198. bool GetFirstKey(wxString& strKeyName, long& lIndex);
  199. /**
  200. Gets the first value of this key. Returns @true if successful.
  201. */
  202. bool GetFirstValue(wxString& strValueName, long& lIndex);
  203. /**
  204. Gets information about the key. Returns @true if successful.
  205. @param pnSubKeys
  206. The number of subkeys.
  207. @param pnMaxKeyLen
  208. The maximum length of the subkey name.
  209. @param pnValues
  210. The number of values.
  211. @param pnMaxValueLen
  212. The maximum length of a value.
  213. */
  214. bool GetKeyInfo(size_t* pnSubKeys, size_t* pnMaxKeyLen,
  215. size_t* pnValues, size_t* pnMaxValueLen) const;
  216. /**
  217. Gets the name of the registry key.
  218. */
  219. wxString GetName(bool bShortPrefix = true) const;
  220. /**
  221. Retrieves the registry view used by this key.
  222. @since 2.9.2
  223. @return The registry view given at the object's construction.
  224. */
  225. WOW64ViewMode GetView() const { return m_viewMode; }
  226. /**
  227. Gets the next key. Returns @true if successful.
  228. */
  229. bool GetNextKey(wxString& strKeyName, long& lIndex) const;
  230. /**
  231. Gets the next key value for this key. Returns @true if successful.
  232. */
  233. bool GetNextValue(wxString& strValueName, long& lIndex) const;
  234. /**
  235. Gets the value type.
  236. */
  237. ValueType GetValueType(const wxString& szValue) const;
  238. /**
  239. Returns @true if given subkey exists.
  240. */
  241. bool HasSubKey(const wxString& szKey) const;
  242. /**
  243. Returns @true if any subkeys exist.
  244. */
  245. bool HasSubkeys() const;
  246. /**
  247. Returns @true if the value exists.
  248. */
  249. bool HasValue(const wxString& szValue) const;
  250. /**
  251. Returns @true if any values exist.
  252. */
  253. bool HasValues() const;
  254. /**
  255. Returns @true if this key is empty, nothing under this key.
  256. */
  257. bool IsEmpty() const;
  258. /**
  259. Returns @true if the value contains a number.
  260. */
  261. bool IsNumericValue(const wxString& szValue) const;
  262. /**
  263. Returns @true if the key is opened.
  264. */
  265. bool IsOpened() const;
  266. /**
  267. Explicitly opens the key. This method also allows the key to be opened
  268. in read-only mode by passing wxRegKey::Read instead of default
  269. wxRegKey::Write parameter. Returns @true if successful.
  270. */
  271. bool Open(AccessMode mode = Write);
  272. /**
  273. Assignment operator to set the default value of the key.
  274. */
  275. wxRegKey& operator=(const wxString& strValue);
  276. /**
  277. Return the default value of the key.
  278. */
  279. wxString QueryDefaultValue() const;
  280. /**
  281. Retrieves the raw string value. Returns @true if successful.
  282. An empty @a szValue queries the default/unnamed key value.
  283. */
  284. bool QueryRawValue(const wxString& szValue, wxString& strValue) const;
  285. /**
  286. Retrieves the raw or expanded string value. Returns @true if successful.
  287. An empty @a szValue queries the default/unnamed key value.
  288. */
  289. bool QueryValue(const wxString& szValue, wxString& strValue, bool raw) const;
  290. /**
  291. Retrieves the numeric value. Returns @true if successful.
  292. An empty @a szValue queries the default/unnamed key value.
  293. */
  294. bool QueryValue(const wxString& szValue, long* plValue) const;
  295. /**
  296. Retrieves the binary structure. Returns @true if successful.
  297. An empty @a szValue queries the default/unnamed key value.
  298. */
  299. bool QueryValue(const wxString& szValue, wxMemoryBuffer& buf) const;
  300. /**
  301. Renames the key. Returns @true if successful.
  302. */
  303. bool Rename(const wxString& szNewName);
  304. /**
  305. Renames a value. Returns @true if successful.
  306. */
  307. bool RenameValue(const wxString& szValueOld,
  308. const wxString& szValueNew);
  309. /**
  310. Preallocate some memory for the name. For wxRegConfig usage only.
  311. */
  312. void ReserveMemoryForName(size_t bytes);
  313. /**
  314. Set or change the HKEY handle.
  315. */
  316. void SetHkey(WXHKEY hKey);
  317. /**
  318. Set the full key name. The name is absolute. It should start with
  319. HKEY_xxx.
  320. */
  321. void SetName(const wxString& strKey);
  322. /**
  323. Set the name relative to the parent key
  324. */
  325. void SetName(StdKey keyParent, const wxString& strKey);
  326. /**
  327. Set the name relative to the parent key
  328. */
  329. void SetName(const wxRegKey& keyParent, const wxString& strKey);
  330. /**
  331. Sets the given @a szValue which must be numeric. If the value doesn't
  332. exist, it is created. Returns @true if successful.
  333. An empty @a szValue sets the default/unnamed key value.
  334. */
  335. bool SetValue(const wxString& szValue, long lValue);
  336. /**
  337. Sets the given @a szValue which must be string. If the value doesn't
  338. exist, it is created. Returns @true if successful.
  339. An empty @a szValue sets the default/unnamed key value.
  340. */
  341. bool SetValue(const wxString& szValue, const wxString& strValue);
  342. /**
  343. Sets the given @a szValue which must be binary. If the value doesn't
  344. exist, it is created. Returns @true if successful.
  345. An empty @a szValue sets the default/unnamed key value.
  346. */
  347. bool SetValue(const wxString& szValue, const wxMemoryBuffer& buf);
  348. };