-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.cpp
149 lines (124 loc) · 2.65 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
#include "pch.h"
#include "registry.h"
BOOL CRegistry::OpenKey(enum Keys hKey, LPCTSTR szKey)
{
if(RegOpenKeyEx((HKEY)hKey, szKey, 0, KEY_ALL_ACCESS, &m_hKey) == ERROR_SUCCESS)
{
return TRUE;
}
else
{
m_hKey = NULL;
return FALSE;
}
}
BOOL CRegistry::CreateKey(enum Keys hKey, LPCTSTR szKey)
{
if(RegCreateKeyEx((HKEY)hKey, szKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, NULL) == ERROR_SUCCESS)
{
return TRUE;
}
else
{
m_hKey = NULL;
return FALSE;
}
}
BOOL CRegistry::SetValue(LPCTSTR lpValueName, LPCTSTR lpData)
{
ASSERT(m_hKey != NULL);
DWORD dwType = REG_SZ;
if(::RegSetValueEx(m_hKey, lpValueName, 0, dwType, (LPBYTE)(LPCTSTR)lpData, (_tcslen(lpData) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS)
{
::RegFlushKey(m_hKey);
return TRUE;
}
return FALSE;
}
BOOL CRegistry::SetValue(LPCTSTR lpValueName, DWORD dwValue)
{
ASSERT(m_hKey != NULL);
DWORD dwType = REG_DWORD;
if(::RegSetValueEx(m_hKey, lpValueName, 0, dwType, (LPBYTE)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS)
{
::RegFlushKey(m_hKey);
return TRUE;
}
return FALSE;
}
BOOL CRegistry::GetValue(LPCTSTR lpValueName, CString& strValue)
{
BOOL bRet = FALSE;
BYTE * pValue;
DWORD dwType = REG_SZ;
DWORD lpcbData;
bRet = FALSE;
pValue = new BYTE[1024];
memset(pValue, 0, 1024);
lpcbData = 1024;
if(::RegQueryValueEx(m_hKey,
lpValueName,
NULL,
&dwType,
pValue,
&lpcbData) == ERROR_SUCCESS)
{
bRet = TRUE;
strValue = (LPCTSTR)pValue;
}
else
{
strValue.Empty();
}
delete []pValue;
return bRet;
}
BOOL CRegistry::GetValue(LPCTSTR lpValueName, DWORD& dwValue)
{
BOOL bRet = FALSE;
DWORD dwType = REG_DWORD;
DWORD lpcbData = sizeof(DWORD);
dwValue = 0;
if(RegQueryValueEx(m_hKey,
lpValueName,
NULL,
&dwType,
(BYTE*)(DWORD)&dwValue,
&lpcbData) == ERROR_SUCCESS)
bRet = TRUE;
return bRet;
}
BOOL CRegistry::DeleteKey(enum Keys hKey, LPCTSTR szKey)
{
return ::RegDeleteKey((HKEY)hKey, szKey) == ERROR_SUCCESS;
}
BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
{
if(::RegDeleteValue(m_hKey, lpValueName) == ERROR_SUCCESS)
{
return TRUE;
}
else
{
return FALSE;
}
}
void CRegistry::CloseKey()
{
::RegCloseKey(m_hKey);
m_hKey = NULL;
}
BOOL CRegistry::SaveKey(LPCTSTR lpszFileName)
{
ASSERT(m_hKey != NULL);
return ::RegSaveKey(m_hKey, lpszFileName, NULL) == ERROR_SUCCESS;
}
BOOL CRegistry::RestoreKey(LPCTSTR lpszFileName, DWORD dwFlags)
{
ASSERT(m_hKey != NULL);
return ::RegRestoreKey(m_hKey, lpszFileName, dwFlags) == ERROR_SUCCESS;
}
BOOL CRegistry::LoadKey(enum Keys hKey, LPCTSTR lpszSubKey, LPCTSTR lpszFileName)
{
return ::RegLoadKey((HKEY)hKey, lpszSubKey, lpszFileName) == ERROR_SUCCESS;
}