MUtilities development repository
修订版 | 636e61c4842671ae20a320cf2f53d9eddd353d0e (tree) |
---|---|
时间 | 2015-05-03 01:52:35 |
作者 | ![]() |
Commiter | LoRd_MuldeR |
Added the Registry class + added ShellNotification function.
@@ -31,6 +31,7 @@ | ||
31 | 31 | <ClCompile Include="src\JobObject_Win32.cpp" /> |
32 | 32 | <ClCompile Include="src\Hash_Keccak.cpp" /> |
33 | 33 | <ClCompile Include="src\OSSupport_Win32.cpp" /> |
34 | + <ClCompile Include="src\Registry_Win32.cpp" /> | |
34 | 35 | <ClCompile Include="src\Sound_Win32.cpp" /> |
35 | 36 | <ClCompile Include="src\Startup.cpp" /> |
36 | 37 | <ClCompile Include="src\Taskbar7_Win32.cpp" /> |
@@ -50,6 +51,7 @@ | ||
50 | 51 | <ClInclude Include="include\MUtils\JobObject.h" /> |
51 | 52 | <ClInclude Include="include\MUtils\Hash_Keccak.h" /> |
52 | 53 | <ClInclude Include="include\MUtils\OSSupport.h" /> |
54 | + <ClInclude Include="include\MUtils\Registry.h" /> | |
53 | 55 | <ClInclude Include="include\MUtils\Sound.h" /> |
54 | 56 | <ClInclude Include="include\MUtils\Startup.h" /> |
55 | 57 | <ClInclude Include="include\MUtils\Taskbar7.h" /> |
@@ -60,7 +62,6 @@ | ||
60 | 62 | <ClInclude Include="src\3rd_party\keccak\include\keccak_impl.h" /> |
61 | 63 | <ClInclude Include="src\3rd_party\strnatcmp\include\strnatcmp.h" /> |
62 | 64 | <ClInclude Include="src\DirLocker.h" /> |
63 | - <ClInclude Include="src\Utils_Win32.h" /> | |
64 | 65 | <CustomBuild Include="include\Mutils\UpdateChecker.h"> |
65 | 66 | <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)"</Command> |
66 | 67 | <Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp"</Message> |
@@ -96,6 +96,9 @@ | ||
96 | 96 | <ClCompile Include="src\3rd_party\blake2\src\blake2.cpp"> |
97 | 97 | <Filter>Source Files\3rd Party</Filter> |
98 | 98 | </ClCompile> |
99 | + <ClCompile Include="src\Registry_Win32.cpp"> | |
100 | + <Filter>Source Files</Filter> | |
101 | + </ClCompile> | |
99 | 102 | </ItemGroup> |
100 | 103 | <ItemGroup> |
101 | 104 | <ClInclude Include="src\CriticalSection_Win32.h"> |
@@ -131,9 +134,6 @@ | ||
131 | 134 | <ClInclude Include="include\MUtils\GUI.h"> |
132 | 135 | <Filter>Public Headers</Filter> |
133 | 136 | </ClInclude> |
134 | - <ClInclude Include="src\Utils_Win32.h"> | |
135 | - <Filter>Header Files</Filter> | |
136 | - </ClInclude> | |
137 | 137 | <ClInclude Include="src\3rd_party\strnatcmp\include\strnatcmp.h"> |
138 | 138 | <Filter>Header Files\3rd Party</Filter> |
139 | 139 | </ClInclude> |
@@ -167,6 +167,9 @@ | ||
167 | 167 | <ClInclude Include="src\3rd_party\blake2\include\blake2.h"> |
168 | 168 | <Filter>Header Files\3rd Party</Filter> |
169 | 169 | </ClInclude> |
170 | + <ClInclude Include="include\MUtils\Registry.h"> | |
171 | + <Filter>Public Headers</Filter> | |
172 | + </ClInclude> | |
170 | 173 | </ItemGroup> |
171 | 174 | <ItemGroup> |
172 | 175 | <CustomBuild Include="include\Mutils\UpdateChecker.h"> |
@@ -167,6 +167,9 @@ namespace MUtils | ||
167 | 167 | //Keyboard support |
168 | 168 | MUTILS_API bool check_key_state_esc(void); |
169 | 169 | |
170 | + //Shell notification | |
171 | + MUTILS_API void shell_change_notification(void); | |
172 | + | |
170 | 173 | //WOW64 redirection |
171 | 174 | MUTILS_API bool wow64fsredir_disable(void *oldValue); |
172 | 175 | MUTILS_API bool wow64fsredir_revert (void *oldValue); |
@@ -0,0 +1,76 @@ | ||
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. | |
14 | +// | |
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 | |
18 | +// | |
19 | +// http://www.gnu.org/licenses/lgpl-2.1.txt | |
20 | +////////////////////////////////////////////////////////////////////////////////// | |
21 | + | |
22 | +#pragma once | |
23 | + | |
24 | +//MUtils | |
25 | +#include <MUtils/Global.h> | |
26 | + | |
27 | +/////////////////////////////////////////////////////////////////////////////// | |
28 | + | |
29 | +namespace MUtils | |
30 | +{ | |
31 | + namespace Registry | |
32 | + { | |
33 | + //Regsitry root | |
34 | + typedef enum | |
35 | + { | |
36 | + root_classes = 0, | |
37 | + root_user = 1, | |
38 | + root_machine = 2, | |
39 | + } | |
40 | + reg_root_t; | |
41 | + | |
42 | + //Forward declaration | |
43 | + namespace Internal | |
44 | + { | |
45 | + class RegistryKeyPrivate; | |
46 | + } | |
47 | + | |
48 | + //Registry key class | |
49 | + class MUTILS_API RegistryKey | |
50 | + { | |
51 | + public: | |
52 | + RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly); | |
53 | + ~RegistryKey(void); | |
54 | + | |
55 | + inline bool isOpen(void); | |
56 | + | |
57 | + bool value_write(const QString &valueName, const quint32 &value); | |
58 | + bool value_write(const QString &valueName, const QString &value); | |
59 | + | |
60 | + bool value_read(const QString &valueName, quint32 &value) const; | |
61 | + bool value_read(const QString &valueName, QString &value) const; | |
62 | + | |
63 | + private: | |
64 | + Internal::RegistryKeyPrivate *const p; | |
65 | + }; | |
66 | + | |
67 | + //Regsitry functions | |
68 | + MUTILS_API bool reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value); | |
69 | + MUTILS_API bool reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value); | |
70 | + MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value); | |
71 | + MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, QString &value); | |
72 | + MUTILS_API bool reg_key_delete (const int &rootKey, const QString &keyName); | |
73 | + } | |
74 | +} | |
75 | + | |
76 | +/////////////////////////////////////////////////////////////////////////////// |
@@ -22,12 +22,12 @@ | ||
22 | 22 | //Win32 API |
23 | 23 | #define WIN32_LEAN_AND_MEAN 1 |
24 | 24 | #include <Windows.h> |
25 | -#include <Objbase.h> | |
26 | 25 | #include <Psapi.h> |
27 | 26 | #include <Sensapi.h> |
28 | 27 | #include <Shellapi.h> |
29 | 28 | #include <PowrProf.h> |
30 | 29 | #include <Mmsystem.h> |
30 | +#include <ShlObj.h> | |
31 | 31 | |
32 | 32 | //Internal |
33 | 33 | #include <MUtils/Global.h> |
@@ -440,7 +440,6 @@ static QReadWriteLock g_known_folders_lock; | ||
440 | 440 | |
441 | 441 | const QString &MUtils::OS::known_folder(known_folder_t folder_id) |
442 | 442 | { |
443 | - static const int CSIDL_FLAG_CREATE = 0x8000; | |
444 | 443 | typedef enum { KF_FLAG_CREATE = 0x00008000 } kf_flags_t; |
445 | 444 | |
446 | 445 | struct |
@@ -1073,6 +1072,15 @@ bool MUtils::OS::check_key_state_esc(void) | ||
1073 | 1072 | } |
1074 | 1073 | |
1075 | 1074 | /////////////////////////////////////////////////////////////////////////////// |
1075 | +// SHELL CHANGE NOTIFICATION | |
1076 | +/////////////////////////////////////////////////////////////////////////////// | |
1077 | + | |
1078 | +void MUtils::OS::shell_change_notification(void) | |
1079 | +{ | |
1080 | + SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); | |
1081 | +} | |
1082 | + | |
1083 | +/////////////////////////////////////////////////////////////////////////////// | |
1076 | 1084 | // WOW64 REDIRECTION |
1077 | 1085 | /////////////////////////////////////////////////////////////////////////////// |
1078 | 1086 |
@@ -0,0 +1,214 @@ | ||
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. | |
14 | +// | |
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 | |
18 | +// | |
19 | +// http://www.gnu.org/licenses/lgpl-2.1.txt | |
20 | +////////////////////////////////////////////////////////////////////////////////// | |
21 | + | |
22 | +#pragma once | |
23 | + | |
24 | +//MUtils | |
25 | +#include <MUtils/Registry.h> | |
26 | +#include <MUtils/Exception.h> | |
27 | + | |
28 | +//Win32 | |
29 | +#define WIN32_LEAN_AND_MEAN | |
30 | +#include <Windows.h> | |
31 | +#include <Shlwapi.h> | |
32 | + | |
33 | +/////////////////////////////////////////////////////////////////////////////// | |
34 | + | |
35 | +static HKEY registry_root(const int &rootKey) | |
36 | +{ | |
37 | + switch(rootKey) | |
38 | + { | |
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!"); | |
43 | + } | |
44 | +} | |
45 | + | |
46 | +/////////////////////////////////////////////////////////////////////////////// | |
47 | +// RegistryKeyPrivate Key Class | |
48 | +/////////////////////////////////////////////////////////////////////////////// | |
49 | + | |
50 | +namespace MUtils | |
51 | +{ | |
52 | + namespace Registry | |
53 | + { | |
54 | + namespace Internal | |
55 | + { | |
56 | + class RegistryKeyPrivate | |
57 | + { | |
58 | + friend class MUtils::Registry::RegistryKey; | |
59 | + | |
60 | + private: | |
61 | + HKEY m_hKey; | |
62 | + bool m_readOnly; | |
63 | + bool m_isOpen; | |
64 | + }; | |
65 | + } | |
66 | + } | |
67 | +} | |
68 | + | |
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) | |
81 | + | |
82 | +/////////////////////////////////////////////////////////////////////////////// | |
83 | +// Registry Key Class | |
84 | +/////////////////////////////////////////////////////////////////////////////// | |
85 | + | |
86 | +MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly) | |
87 | +: | |
88 | + p(new Internal::RegistryKeyPrivate()) | |
89 | +{ | |
90 | + p->m_hKey = NULL; | |
91 | + p->m_readOnly = readOnly; | |
92 | + p->m_isOpen = false; | |
93 | + | |
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) | |
96 | + { | |
97 | + qWarning("Failed to open registry key!"); | |
98 | + } | |
99 | +} | |
100 | + | |
101 | +MUtils::Registry::RegistryKey::~RegistryKey(void) | |
102 | +{ | |
103 | + if(p->m_isOpen) | |
104 | + { | |
105 | + CloseHandle(p->m_hKey); | |
106 | + p->m_hKey = NULL; | |
107 | + p->m_isOpen = false; | |
108 | + } | |
109 | +} | |
110 | + | |
111 | +inline bool MUtils::Registry::RegistryKey::isOpen(void) | |
112 | +{ | |
113 | + return p->m_isOpen; | |
114 | +} | |
115 | + | |
116 | +bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const quint32 &value) | |
117 | +{ | |
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); | |
120 | +} | |
121 | + | |
122 | +bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const QString &value) | |
123 | +{ | |
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); | |
126 | +} | |
127 | + | |
128 | +bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const | |
129 | +{ | |
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); | |
133 | +} | |
134 | + | |
135 | +bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString &value) const | |
136 | +{ | |
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))) | |
141 | + { | |
142 | + value = QString::fromUtf16(reinterpret_cast<const ushort*>(buffer)); | |
143 | + return true; | |
144 | + } | |
145 | + return false; | |
146 | +} | |
147 | + | |
148 | +/////////////////////////////////////////////////////////////////////////////// | |
149 | +// HELPER FUNCTIONS | |
150 | +/////////////////////////////////////////////////////////////////////////////// | |
151 | + | |
152 | +/* | |
153 | + * Write registry value | |
154 | + */ | |
155 | +bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value) | |
156 | +{ | |
157 | + bool success = false; | |
158 | + RegistryKey regKey(rootKey, keyName, false); | |
159 | + if(regKey.isOpen()) | |
160 | + { | |
161 | + success = regKey.value_write(valueName, value); | |
162 | + } | |
163 | + return success; | |
164 | +} | |
165 | + | |
166 | +/* | |
167 | + * Write registry value | |
168 | + */ | |
169 | +bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value) | |
170 | +{ | |
171 | + bool success = false; | |
172 | + RegistryKey regKey(rootKey, keyName, false); | |
173 | + if(regKey.isOpen()) | |
174 | + { | |
175 | + success = regKey.value_write(valueName, value); | |
176 | + } | |
177 | + return success; | |
178 | +} | |
179 | + | |
180 | +/* | |
181 | + * Read registry value | |
182 | + */ | |
183 | +bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value) | |
184 | +{ | |
185 | + bool success = false; | |
186 | + RegistryKey regKey(rootKey, keyName, true); | |
187 | + if(regKey.isOpen()) | |
188 | + { | |
189 | + success = regKey.value_read(valueName, value); | |
190 | + } | |
191 | + return success; | |
192 | +} | |
193 | + | |
194 | +/* | |
195 | + * Read registry value | |
196 | + */ | |
197 | +bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value) | |
198 | +{ | |
199 | + bool success = false; | |
200 | + RegistryKey regKey(rootKey, keyName, true); | |
201 | + if(regKey.isOpen()) | |
202 | + { | |
203 | + success = regKey.value_read(valueName, value); | |
204 | + } | |
205 | + return success; | |
206 | +} | |
207 | + | |
208 | +/* | |
209 | + * Delete registry key | |
210 | + */ | |
211 | +bool MUtils::Registry::reg_key_delete(const int &rootKey, const QString &keyName) | |
212 | +{ | |
213 | + return (SHDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS); | |
214 | +} |