Added the Registry class + added ShellNotification function.
[MUtilities.git] / src / Registry_Win32.cpp
blobb1b67d42307baff03bde82b5ae23006a3b4874b3
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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 //////////////////////////////////////////////////////////////////////////////////
22 #pragma once
24 //MUtils
25 #include <MUtils/Registry.h>
26 #include <MUtils/Exception.h>
28 //Win32
29 #define WIN32_LEAN_AND_MEAN
30 #include <Windows.h>
31 #include <Shlwapi.h>
33 ///////////////////////////////////////////////////////////////////////////////
35 static HKEY registry_root(const int &rootKey)
37 switch(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 ///////////////////////////////////////////////////////////////////////////////
50 namespace MUtils
52 namespace Registry
54 namespace Internal
56 class RegistryKeyPrivate
58 friend class MUtils::Registry::RegistryKey;
60 private:
61 HKEY m_hKey;
62 bool m_readOnly;
63 bool m_isOpen;
69 #define CHECK_STATUS(X) do \
70 { \
71 if(!p->m_isOpen) \
72 { \
73 MUTILS_THROW("Cannot read from or write to a key is not currently open!"); \
74 } \
75 if(p->m_readOnly != (X)) \
76 { \
77 MUTILS_THROW("Cannot write to read-only key or read from write-only key!"); \
78 } \
79 } \
80 while(0)
82 ///////////////////////////////////////////////////////////////////////////////
83 // Registry Key Class
84 ///////////////////////////////////////////////////////////////////////////////
86 MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly)
88 p(new Internal::RegistryKeyPrivate())
90 p->m_hKey = NULL;
91 p->m_readOnly = readOnly;
92 p->m_isOpen = false;
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);
95 if(!p->m_isOpen)
97 qWarning("Failed to open registry key!");
101 MUtils::Registry::RegistryKey::~RegistryKey(void)
103 if(p->m_isOpen)
105 CloseHandle(p->m_hKey);
106 p->m_hKey = NULL;
107 p->m_isOpen = false;
111 inline bool MUtils::Registry::RegistryKey::isOpen(void)
113 return p->m_isOpen;
116 bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const quint32 &value)
118 CHECK_STATUS(false);
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)
124 CHECK_STATUS(false);
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;
131 CHECK_STATUS(false);
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;
139 CHECK_STATUS(false);
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));
143 return true;
145 return false;
148 ///////////////////////////////////////////////////////////////////////////////
149 // HELPER FUNCTIONS
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);
159 if(regKey.isOpen())
161 success = regKey.value_write(valueName, value);
163 return success;
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);
173 if(regKey.isOpen())
175 success = regKey.value_write(valueName, value);
177 return success;
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);
187 if(regKey.isOpen())
189 success = regKey.value_read(valueName, value);
191 return success;
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);
201 if(regKey.isOpen())
203 success = regKey.value_read(valueName, value);
205 return success;
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);