• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版636e61c4842671ae20a320cf2f53d9eddd353d0e (tree)
时间2015-05-03 01:52:35
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Added the Registry class + added ShellNotification function.

更改概述

差异

--- a/MUtilities_VS2013.vcxproj
+++ b/MUtilities_VS2013.vcxproj
@@ -31,6 +31,7 @@
3131 <ClCompile Include="src\JobObject_Win32.cpp" />
3232 <ClCompile Include="src\Hash_Keccak.cpp" />
3333 <ClCompile Include="src\OSSupport_Win32.cpp" />
34+ <ClCompile Include="src\Registry_Win32.cpp" />
3435 <ClCompile Include="src\Sound_Win32.cpp" />
3536 <ClCompile Include="src\Startup.cpp" />
3637 <ClCompile Include="src\Taskbar7_Win32.cpp" />
@@ -50,6 +51,7 @@
5051 <ClInclude Include="include\MUtils\JobObject.h" />
5152 <ClInclude Include="include\MUtils\Hash_Keccak.h" />
5253 <ClInclude Include="include\MUtils\OSSupport.h" />
54+ <ClInclude Include="include\MUtils\Registry.h" />
5355 <ClInclude Include="include\MUtils\Sound.h" />
5456 <ClInclude Include="include\MUtils\Startup.h" />
5557 <ClInclude Include="include\MUtils\Taskbar7.h" />
@@ -60,7 +62,6 @@
6062 <ClInclude Include="src\3rd_party\keccak\include\keccak_impl.h" />
6163 <ClInclude Include="src\3rd_party\strnatcmp\include\strnatcmp.h" />
6264 <ClInclude Include="src\DirLocker.h" />
63- <ClInclude Include="src\Utils_Win32.h" />
6465 <CustomBuild Include="include\Mutils\UpdateChecker.h">
6566 <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)"</Command>
6667 <Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp"</Message>
--- a/MUtilities_VS2013.vcxproj.filters
+++ b/MUtilities_VS2013.vcxproj.filters
@@ -96,6 +96,9 @@
9696 <ClCompile Include="src\3rd_party\blake2\src\blake2.cpp">
9797 <Filter>Source Files\3rd Party</Filter>
9898 </ClCompile>
99+ <ClCompile Include="src\Registry_Win32.cpp">
100+ <Filter>Source Files</Filter>
101+ </ClCompile>
99102 </ItemGroup>
100103 <ItemGroup>
101104 <ClInclude Include="src\CriticalSection_Win32.h">
@@ -131,9 +134,6 @@
131134 <ClInclude Include="include\MUtils\GUI.h">
132135 <Filter>Public Headers</Filter>
133136 </ClInclude>
134- <ClInclude Include="src\Utils_Win32.h">
135- <Filter>Header Files</Filter>
136- </ClInclude>
137137 <ClInclude Include="src\3rd_party\strnatcmp\include\strnatcmp.h">
138138 <Filter>Header Files\3rd Party</Filter>
139139 </ClInclude>
@@ -167,6 +167,9 @@
167167 <ClInclude Include="src\3rd_party\blake2\include\blake2.h">
168168 <Filter>Header Files\3rd Party</Filter>
169169 </ClInclude>
170+ <ClInclude Include="include\MUtils\Registry.h">
171+ <Filter>Public Headers</Filter>
172+ </ClInclude>
170173 </ItemGroup>
171174 <ItemGroup>
172175 <CustomBuild Include="include\Mutils\UpdateChecker.h">
--- a/include/MUtils/OSSupport.h
+++ b/include/MUtils/OSSupport.h
@@ -167,6 +167,9 @@ namespace MUtils
167167 //Keyboard support
168168 MUTILS_API bool check_key_state_esc(void);
169169
170+ //Shell notification
171+ MUTILS_API void shell_change_notification(void);
172+
170173 //WOW64 redirection
171174 MUTILS_API bool wow64fsredir_disable(void *oldValue);
172175 MUTILS_API bool wow64fsredir_revert (void *oldValue);
--- /dev/null
+++ b/include/MUtils/Registry.h
@@ -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+///////////////////////////////////////////////////////////////////////////////
--- a/src/OSSupport_Win32.cpp
+++ b/src/OSSupport_Win32.cpp
@@ -22,12 +22,12 @@
2222 //Win32 API
2323 #define WIN32_LEAN_AND_MEAN 1
2424 #include <Windows.h>
25-#include <Objbase.h>
2625 #include <Psapi.h>
2726 #include <Sensapi.h>
2827 #include <Shellapi.h>
2928 #include <PowrProf.h>
3029 #include <Mmsystem.h>
30+#include <ShlObj.h>
3131
3232 //Internal
3333 #include <MUtils/Global.h>
@@ -440,7 +440,6 @@ static QReadWriteLock g_known_folders_lock;
440440
441441 const QString &MUtils::OS::known_folder(known_folder_t folder_id)
442442 {
443- static const int CSIDL_FLAG_CREATE = 0x8000;
444443 typedef enum { KF_FLAG_CREATE = 0x00008000 } kf_flags_t;
445444
446445 struct
@@ -1073,6 +1072,15 @@ bool MUtils::OS::check_key_state_esc(void)
10731072 }
10741073
10751074 ///////////////////////////////////////////////////////////////////////////////
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+///////////////////////////////////////////////////////////////////////////////
10761084 // WOW64 REDIRECTION
10771085 ///////////////////////////////////////////////////////////////////////////////
10781086
--- /dev/null
+++ b/src/Registry_Win32.cpp
@@ -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+}
Show on old repository browser