[80] | 1 | // |
---|
| 2 | // WinRegistryKey.h |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Util/include/Poco/Util/WinRegistryKey.h#3 $ |
---|
| 5 | // |
---|
| 6 | // Library: Util |
---|
| 7 | // Package: Windows |
---|
| 8 | // Module: WinRegistryKey |
---|
| 9 | // |
---|
| 10 | // Definition of the WinRegistryKey class. |
---|
| 11 | // |
---|
| 12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
| 13 | // and Contributors. |
---|
| 14 | // |
---|
| 15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
| 16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
| 17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
| 20 | // do so, all subject to the following: |
---|
| 21 | // |
---|
| 22 | // The copyright notices in the Software and this entire statement, including |
---|
| 23 | // the above license grant, this restriction and the following disclaimer, |
---|
| 24 | // must be included in all copies of the Software, in whole or in part, and |
---|
| 25 | // all derivative works of the Software, unless such copies or derivative |
---|
| 26 | // works are solely in the form of machine-executable object code generated by |
---|
| 27 | // a source language processor. |
---|
| 28 | // |
---|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 35 | // DEALINGS IN THE SOFTWARE. |
---|
| 36 | // |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | #ifndef Util_WinRegistryKey_INCLUDED |
---|
| 40 | #define Util_WinRegistryKey_INCLUDED |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #include "Poco/Util/Util.h" |
---|
| 44 | #include "Poco/UnWindows.h" |
---|
| 45 | #include <vector> |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | namespace Poco { |
---|
| 49 | namespace Util { |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | class Util_API WinRegistryKey |
---|
| 53 | /// This class implements a convenient interface to the |
---|
| 54 | /// Windows Registry. |
---|
| 55 | /// |
---|
| 56 | /// This class is only available on Windows platforms. |
---|
| 57 | { |
---|
| 58 | public: |
---|
| 59 | typedef std::vector<std::string> Keys; |
---|
| 60 | typedef std::vector<std::string> Values; |
---|
| 61 | |
---|
| 62 | enum Type |
---|
| 63 | { |
---|
| 64 | REGT_NONE = 0, |
---|
| 65 | REGT_STRING = 1, |
---|
| 66 | REGT_STRING_EXPAND = 2, |
---|
| 67 | REGT_DWORD = 4 |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | WinRegistryKey(const std::string& key, bool readOnly = false); |
---|
| 71 | /// Creates the WinRegistryKey. |
---|
| 72 | /// |
---|
| 73 | /// The key must start with one of the root key names |
---|
| 74 | /// like HKEY_CLASSES_ROOT, e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. |
---|
| 75 | /// |
---|
| 76 | /// If readOnly is true, then only read access to the registry |
---|
| 77 | /// is available and any attempt to write to the registry will |
---|
| 78 | /// result in an exception. |
---|
| 79 | |
---|
| 80 | WinRegistryKey(HKEY hRootKey, const std::string& subKey, bool readOnly = false); |
---|
| 81 | /// Creates the WinRegistryKey. |
---|
| 82 | /// |
---|
| 83 | /// If readOnly is true, then only read access to the registry |
---|
| 84 | /// is available and any attempt to write to the registry will |
---|
| 85 | /// result in an exception. |
---|
| 86 | |
---|
| 87 | ~WinRegistryKey(); |
---|
| 88 | /// Destroys the WinRegistryKey. |
---|
| 89 | |
---|
| 90 | void setString(const std::string& name, const std::string& value); |
---|
| 91 | /// Sets the string value (REG_SZ) with the given name. |
---|
| 92 | /// An empty name denotes the default value. |
---|
| 93 | |
---|
| 94 | std::string getString(const std::string& name); |
---|
| 95 | /// Returns the string value (REG_SZ) with the given name. |
---|
| 96 | /// An empty name denotes the default value. |
---|
| 97 | /// |
---|
| 98 | /// Throws a NotFoundException if the value does not exist. |
---|
| 99 | |
---|
| 100 | void setStringExpand(const std::string& name, const std::string& value); |
---|
| 101 | /// Sets the expandable string value (REG_EXPAND_SZ) with the given name. |
---|
| 102 | /// An empty name denotes the default value. |
---|
| 103 | |
---|
| 104 | std::string getStringExpand(const std::string& name); |
---|
| 105 | /// Returns the string value (REG_EXPAND_SZ) with the given name. |
---|
| 106 | /// An empty name denotes the default value. |
---|
| 107 | /// All references to environment variables (%VAR%) in the string |
---|
| 108 | /// are expanded. |
---|
| 109 | /// |
---|
| 110 | /// Throws a NotFoundException if the value does not exist. |
---|
| 111 | |
---|
| 112 | void setInt(const std::string& name, int value); |
---|
| 113 | /// Sets the numeric (REG_DWORD) value with the given name. |
---|
| 114 | /// An empty name denotes the default value. |
---|
| 115 | |
---|
| 116 | int getInt(const std::string& name); |
---|
| 117 | /// Returns the numeric value (REG_DWORD) with the given name. |
---|
| 118 | /// An empty name denotes the default value. |
---|
| 119 | /// |
---|
| 120 | /// Throws a NotFoundException if the value does not exist. |
---|
| 121 | |
---|
| 122 | void deleteValue(const std::string& name); |
---|
| 123 | /// Deletes the value with the given name. |
---|
| 124 | /// |
---|
| 125 | /// Throws a NotFoundException if the value does not exist. |
---|
| 126 | |
---|
| 127 | void deleteKey(); |
---|
| 128 | /// Recursively deletes the key and all subkeys. |
---|
| 129 | |
---|
| 130 | bool exists(); |
---|
| 131 | /// Returns true iff the key exists. |
---|
| 132 | |
---|
| 133 | Type type(const std::string& name); |
---|
| 134 | /// Returns the type of the key value. |
---|
| 135 | |
---|
| 136 | bool exists(const std::string& name); |
---|
| 137 | /// Returns true iff the given value exists under that key. |
---|
| 138 | |
---|
| 139 | void subKeys(Keys& keys); |
---|
| 140 | /// Appends all subKey names to keys. |
---|
| 141 | |
---|
| 142 | void values(Values& vals); |
---|
| 143 | /// Appends all value names to vals; |
---|
| 144 | |
---|
| 145 | bool isReadOnly() const; |
---|
| 146 | /// Returns true iff the key has been opened for read-only access only. |
---|
| 147 | |
---|
| 148 | protected: |
---|
| 149 | void open(); |
---|
| 150 | void close(); |
---|
| 151 | std::string key() const; |
---|
| 152 | std::string key(const std::string& valueName) const; |
---|
| 153 | void handleSetError(const std::string& name); |
---|
| 154 | static HKEY handleFor(const std::string& rootKey); |
---|
| 155 | |
---|
| 156 | private: |
---|
| 157 | WinRegistryKey(); |
---|
| 158 | WinRegistryKey(const WinRegistryKey&); |
---|
| 159 | WinRegistryKey& operator = (const WinRegistryKey&); |
---|
| 160 | |
---|
| 161 | HKEY _hRootKey; |
---|
| 162 | std::string _subKey; |
---|
| 163 | HKEY _hKey; |
---|
| 164 | bool _readOnly; |
---|
| 165 | }; |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | // |
---|
| 169 | // inlines |
---|
| 170 | // |
---|
| 171 | inline bool WinRegistryKey::isReadOnly() const |
---|
| 172 | { |
---|
| 173 | return _readOnly; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | } } // namespace Poco::Util |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | #endif // Util_WinRegistryKey_INCLUDED |
---|