-
Notifications
You must be signed in to change notification settings - Fork 19
/
Registry.cpp
207 lines (173 loc) · 6.05 KB
/
Registry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// sktoolslib - common files for SK tools
// Copyright (C) 2012, 2017, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "registry.h"
//////////////////////////////////////////////////////////////////////////////////////////////
#ifdef __CSTRINGT_H__
CRegBase::CRegBase()
{
}
CRegBase::CRegBase(const CString& key, bool force, HKEY base, REGSAM sam)
: CRegBaseCommon<CString>(key, force, base, sam)
{
m_key.TrimLeft(L"\\");
int backslashpos = m_key.ReverseFind('\\');
m_path = m_key.Left(backslashpos);
m_path.TrimRight(L"\\");
m_key = m_key.Mid(backslashpos);
m_key.Trim(L"\\");
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////
CRegStdBase::CRegStdBase()
{
}
CRegStdBase::CRegStdBase(const std::wstring& key, bool force, HKEY base, REGSAM sam)
: CRegBaseCommon<std::wstring>(key, force, base, sam)
{
std::wstring::size_type pos = key.find_last_of(L'\\');
m_path = key.substr(0, pos);
m_key = key.substr(pos + 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////
#ifdef __ATLTYPES_H__ // defines CRect
CRegRect::CRegRect(void)
: CRegTypedBase<CRect, CRegBase>(CRect(0, 0, 0, 0))
{
}
CRegRect::CRegRect(const CString& key, const CRect& def, bool force, HKEY base, REGSAM sam)
: CRegTypedBase<CRect, CRegBase>(key, def, force, base, sam)
{
read();
}
void CRegRect::InternalRead(HKEY hKey, CRect& value)
{
DWORD size = 0;
DWORD type = 0;
m_lastError = RegQueryValueEx(hKey, m_key, NULL, &type, nullptr, (LPDWORD)&size);
if (m_lastError == ERROR_SUCCESS)
{
auto buffer = std::make_unique<char[]>(size);
if ((m_lastError = RegQueryValueEx(hKey, m_key, nullptr, &type, (BYTE*)buffer.get(), &size)) == ERROR_SUCCESS)
{
ASSERT(type == REG_BINARY);
value = CRect((LPRECT)buffer.get());
}
}
}
void CRegRect::InternalWrite(HKEY hKey, const CRect& value)
{
m_lastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE*)(LPCRECT)value, sizeof(value));
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////
#ifdef __ATLTYPES_H__ // defines CPoint
CRegPoint::CRegPoint(void)
: CRegTypedBase<CPoint, CRegBase>(CPoint(0, 0))
{
}
CRegPoint::CRegPoint(const CString& key, const CPoint& def, bool force, HKEY base, REGSAM sam)
: CRegTypedBase<CPoint, CRegBase>(key, def, force, base, sam)
{
read();
}
void CRegPoint::InternalRead(HKEY hKey, CPoint& value)
{
DWORD size = 0;
DWORD type = 0;
m_lastError = RegQueryValueEx(hKey, m_key, nullptr, &type, nullptr, (LPDWORD)&size);
if (m_lastError == ERROR_SUCCESS)
{
auto buffer = std::make_unique<char[]>(size);
if ((m_lastError = RegQueryValueEx(hKey, m_key, nullptr, &type, (BYTE*)buffer.get(), &size)) == ERROR_SUCCESS)
{
ASSERT(type == REG_BINARY);
value = CPoint(*(POINT*)buffer.get());
}
}
}
void CRegPoint::InternalWrite(HKEY hKey, const CPoint& value)
{
m_lastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE*)&value, sizeof(value));
}
#endif
/////////////////////////////////////////////////////////////////////
#ifdef __AFXCOLL_H__ // defines CStringList
CRegistryKey::CRegistryKey(const CString& key, HKEY base, REGSAM sam)
{
m_base = base;
m_hKey = nullptr;
m_sam = sam;
m_path = key;
m_path.TrimLeft(L"\\");
}
CRegistryKey::~CRegistryKey()
{
if (m_hKey)
RegCloseKey(m_hKey);
}
DWORD CRegistryKey::createKey()
{
DWORD disp;
DWORD rc = RegCreateKeyEx(m_base, m_path, 0, L"", REG_OPTION_NON_VOLATILE, KEY_WRITE | m_sam, nullptr, &m_hKey, &disp);
if (rc != ERROR_SUCCESS)
{
return rc;
}
return RegCloseKey(m_hKey);
}
DWORD CRegistryKey::removeKey()
{
RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE | m_sam, &m_hKey);
return SHDeleteKey(m_base, (LPCWSTR)m_path);
}
bool CRegistryKey::getValues(CStringList& values)
{
values.RemoveAll();
if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE | m_sam, &m_hKey) == ERROR_SUCCESS)
{
for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
{
wchar_t value[255];
DWORD size = _countof(value);
rc = RegEnumValue(m_hKey, i, value, &size, nullptr, nullptr, nullptr, nullptr);
if (rc == ERROR_SUCCESS)
{
values.AddTail(value);
}
}
}
return values.GetCount() > 0;
}
bool CRegistryKey::getSubKeys(CStringList& subkeys)
{
subkeys.RemoveAll();
if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE | m_sam, &m_hKey) == ERROR_SUCCESS)
{
for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
{
wchar_t value[1024];
DWORD size = _countof(value);
FILETIME last_write_time;
rc = RegEnumKeyEx(m_hKey, i, value, &size, nullptr, nullptr, nullptr, &last_write_time);
if (rc == ERROR_SUCCESS)
{
subkeys.AddTail(value);
}
}
}
return subkeys.GetCount() > 0;
}
#endif