1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
25 #include <MUtils/Registry.h>
26 #include <MUtils/Exception.h>
29 #define WIN32_LEAN_AND_MEAN
33 ///////////////////////////////////////////////////////////////////////////////
35 static HKEY
registry_root(const int &rootKey
)
39 case MUtils::Registry::root_classes
: return HKEY_CLASSES_ROOT
; break;
40 case MUtils::Registry::root_user
: return HKEY_CURRENT_USER
; break;
41 case MUtils::Registry::root_machine
: return HKEY_LOCAL_MACHINE
; break;
42 default: MUTILS_THROW("Unknown root reg value was specified!");
46 ///////////////////////////////////////////////////////////////////////////////
47 // RegistryKeyPrivate Key Class
48 ///////////////////////////////////////////////////////////////////////////////
56 class RegistryKeyPrivate
58 friend class MUtils::Registry::RegistryKey
;
69 #define CHECK_STATUS(X) do \
73 MUTILS_THROW("Cannot read from or write to a key is not currently open!"); \
75 if(p->m_readOnly != (X)) \
77 MUTILS_THROW("Cannot write to read-only key or read from write-only key!"); \
82 ///////////////////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////////////////
86 MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey
, const QString
&keyName
, const bool &readOnly
)
88 p(new Internal::RegistryKeyPrivate())
91 p
->m_readOnly
= readOnly
;
94 p
->m_isOpen
= (RegCreateKeyEx(registry_root(rootKey
), MUTILS_WCHR(keyName
), 0, NULL
, 0, p
->m_readOnly
? KEY_READ
: KEY_WRITE
, NULL
, &p
->m_hKey
, NULL
) == ERROR_SUCCESS
);
97 qWarning("Failed to open registry key!");
101 MUtils::Registry::RegistryKey::~RegistryKey(void)
105 CloseHandle(p
->m_hKey
);
111 inline bool MUtils::Registry::RegistryKey::isOpen(void)
116 bool MUtils::Registry::RegistryKey::value_write(const QString
&valueName
, const quint32
&value
)
119 return (RegSetValueEx(p
->m_hKey
, valueName
.isEmpty() ? NULL
: MUTILS_WCHR(valueName
), 0, REG_DWORD
, reinterpret_cast<const BYTE
*>(&value
), sizeof(quint32
)) == ERROR_SUCCESS
);
122 bool MUtils::Registry::RegistryKey::value_write(const QString
&valueName
, const QString
&value
)
125 return (RegSetValueEx(p
->m_hKey
, valueName
.isEmpty() ? NULL
: MUTILS_WCHR(valueName
), 0, REG_SZ
, reinterpret_cast<const BYTE
*>(value
.utf16()), (value
.length() + 1) * sizeof(wchar_t)) == ERROR_SUCCESS
);
128 bool MUtils::Registry::RegistryKey::value_read(const QString
&valueName
, quint32
&value
) const
130 DWORD size
= sizeof(quint32
), type
= -1;
132 return (RegQueryValueEx(p
->m_hKey
, valueName
.isEmpty() ? NULL
: MUTILS_WCHR(valueName
), 0, &type
, reinterpret_cast<BYTE
*>(&value
), &size
) == ERROR_SUCCESS
) && (type
== REG_DWORD
);
135 bool MUtils::Registry::RegistryKey::value_read(const QString
&valueName
, QString
&value
) const
137 wchar_t buffer
[2048];
138 DWORD size
= sizeof(wchar_t) * 2048, type
= -1;
140 if((RegQueryValueEx(p
->m_hKey
, valueName
.isEmpty() ? NULL
: MUTILS_WCHR(valueName
), 0, &type
, reinterpret_cast<BYTE
*>(&value
), &size
) == ERROR_SUCCESS
) && ((type
== REG_SZ
) || (type
== REG_EXPAND_SZ
)))
142 value
= QString::fromUtf16(reinterpret_cast<const ushort
*>(buffer
));
148 ///////////////////////////////////////////////////////////////////////////////
150 ///////////////////////////////////////////////////////////////////////////////
153 * Write registry value
155 bool MUtils::Registry::reg_value_write(const int &rootKey
, const QString
&keyName
, const QString
&valueName
, const quint32
&value
)
157 bool success
= false;
158 RegistryKey
regKey(rootKey
, keyName
, false);
161 success
= regKey
.value_write(valueName
, value
);
167 * Write registry value
169 bool MUtils::Registry::reg_value_write(const int &rootKey
, const QString
&keyName
, const QString
&valueName
, const QString
&value
)
171 bool success
= false;
172 RegistryKey
regKey(rootKey
, keyName
, false);
175 success
= regKey
.value_write(valueName
, value
);
181 * Read registry value
183 bool MUtils::Registry::reg_value_read(const int &rootKey
, const QString
&keyName
, const QString
&valueName
, quint32
&value
)
185 bool success
= false;
186 RegistryKey
regKey(rootKey
, keyName
, true);
189 success
= regKey
.value_read(valueName
, value
);
195 * Read registry value
197 bool MUtils::Registry::reg_value_read(const int &rootKey
, const QString
&keyName
, const QString
&valueName
, QString
&value
)
199 bool success
= false;
200 RegistryKey
regKey(rootKey
, keyName
, true);
203 success
= regKey
.value_read(valueName
, value
);
209 * Delete registry key
211 bool MUtils::Registry::reg_key_delete(const int &rootKey
, const QString
&keyName
)
213 return (SHDeleteKey(registry_root(rootKey
), MUTILS_WCHR(keyName
)) == ERROR_SUCCESS
);