forked from xz00311/AutoEye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.cpp
177 lines (129 loc) · 3.57 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
#include "pch.h"
#include "Registry.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CRegistry::CRegistry(HKEY hRootKey)
{
m_hRootKey = hRootKey;
m_hSubKey = NULL;
}
CRegistry::~CRegistry()
{
Close();
}
BOOL CRegistry::VerifyKey(LPCTSTR lpszPath)
{
LSTATUS status = ::RegOpenKeyEx(m_hRootKey, lpszPath, 0, KEY_ALL_ACCESS, &m_hSubKey);
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::VerifyValue(LPCTSTR lpszValue)
{
LSTATUS status = ::RegQueryValueEx(m_hSubKey, lpszValue, NULL, NULL, NULL, NULL);
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::IsEqual(LPCTSTR lpszValue, int iVal)
{
int nTemp = 0;
BOOL bRet = Read(lpszValue, nTemp);
return (bRet ? (iVal == nTemp) : FALSE);
}
BOOL CRegistry::IsEqual(LPCTSTR lpszValue, DWORD dwVal)
{
DWORD dwTemp = 0;
BOOL bRet = Read(lpszValue, dwTemp);
return (bRet ? (dwVal == dwTemp) : FALSE);
}
BOOL CRegistry::IsEqual(LPCTSTR lpszValue, LPCTSTR lpszVal)
{
CString str;
BOOL bRet = Read(lpszValue, str);
return (bRet ? (str.CompareNoCase(lpszVal) == 0) : FALSE);
}
BOOL CRegistry::CreateKey(LPCTSTR lpszPath)
{
LSTATUS status = ::RegCreateKeyEx(m_hRootKey, lpszPath, 0, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hSubKey, NULL);
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::Write(LPCTSTR lpszKeyName, int iVal)
{
DWORD dwValue = (DWORD)iVal;
LSTATUS status = ::RegSetValueEx(m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(const BYTE*)&dwValue, sizeof(DWORD));
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::Write(LPCTSTR lpszKeyName, DWORD dwVal)
{
LSTATUS status = ::RegSetValueEx(m_hSubKey, lpszKeyName, 0, REG_DWORD,
(const BYTE*)&dwVal, sizeof(DWORD));
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::Write(LPCTSTR lpszKeyName, LPCTSTR lpszData)
{
LSTATUS status = ::RegSetValueEx(m_hSubKey, lpszKeyName, 0, REG_SZ,
(const BYTE*)lpszData, sizeof(TCHAR)*(lstrlen(lpszData)));
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::Read(LPCTSTR lpszKeyName, int& iVal)
{
DWORD dwType = 0;
int dwDest = 0;
DWORD dwSize = sizeof(dwDest);
LSTATUS status = ::RegQueryValueEx(m_hSubKey, lpszKeyName, NULL,
&dwType, (BYTE *)&dwDest, &dwSize);
if (status != ERROR_SUCCESS)
{
return FALSE;
}
iVal = dwDest;
return TRUE;
}
BOOL CRegistry::Read(LPCTSTR lpszKeyName, DWORD& dwVal)
{
DWORD dwType = 0;
DWORD dwDest = 0;
DWORD dwSize = sizeof(dwDest);
LSTATUS status = ::RegQueryValueEx(m_hSubKey, lpszKeyName, NULL,
&dwType, (BYTE *)&dwDest, &dwSize);
if (status != ERROR_SUCCESS)
{
return FALSE;
}
dwVal = dwDest;
return TRUE;
}
BOOL CRegistry::Read(LPCTSTR lpszKeyName, CString& strVal)
{
DWORD dwType = 0;
TCHAR szString[MAX_PATH] = {0};
DWORD dwSize = _countof(szString) - 1;
LSTATUS status = ::RegQueryValueEx(m_hSubKey, lpszKeyName, NULL,
&dwType, (BYTE *)szString, &dwSize);
if (status != ERROR_SUCCESS)
{
return FALSE;
}
strVal = szString;
return TRUE;
}
BOOL CRegistry::DeleteValue(LPCTSTR lpszValue)
{
LSTATUS status = ::RegDeleteValue(m_hSubKey, lpszValue);
return (status == ERROR_SUCCESS);
}
BOOL CRegistry::DeleteKey(LPCTSTR lpszPath)
{
LSTATUS status = ::RegDeleteKey(m_hRootKey, lpszPath);
return (status == ERROR_SUCCESS);
}
void CRegistry::Close()
{
if (m_hSubKey != NULL)
{
::RegCloseKey (m_hSubKey);
m_hSubKey = NULL;
}
m_hRootKey = NULL;
}