-
Notifications
You must be signed in to change notification settings - Fork 7
/
RegKey.cpp
129 lines (107 loc) · 3.28 KB
/
RegKey.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
#include "RegKey.h"
#include <assert.h>
BOOL RegKey::Create( HKEY hParentKey, LPCTSTR lpszSubKey ) {
assert( hParentKey != NULL && lpszSubKey != NULL );
Close();
DWORD dwDisposition;
if ( ::RegCreateKeyEx(
hParentKey, lpszSubKey,
0, REG_NONE,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
NULL, &m_hKey, &dwDisposition ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::Open( HKEY hParentKey, LPCTSTR lpszSubKey ) {
assert( hParentKey != NULL && lpszSubKey != NULL );
Close();
if ( ::RegOpenKeyEx(
hParentKey, lpszSubKey,
0, KEY_ALL_ACCESS, &m_hKey ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::DeleteSubKey( LPCTSTR lpszSubKey ) {
assert( lpszSubKey != NULL );
assert( isOpened() );
if ( ::RegDeleteKey( m_hKey, lpszSubKey ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
void RegKey::Close() {
if ( isOpened() ) {
::RegCloseKey( m_hKey );
m_hKey = NULL;
}
}
void RegKey::Flush() {
if ( isOpened() ) {
::RegFlushKey( m_hKey );
}
}
BOOL RegKey::SetValue( LPCTSTR lpValueName, DWORD dwValue ) {
assert( lpValueName != NULL );
if ( ::RegSetValueEx( m_hKey, lpValueName, 0,
REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD) ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::SetValue( LPCTSTR lpValueName, LPCTSTR lpValue ) {
assert( lpValueName != NULL && lpValue != NULL );
if ( ::RegSetValueEx( m_hKey, lpValueName, 0,
REG_SZ, (LPBYTE)lpValue, ::lstrlen(lpValue)+1 ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::SetValue( LPCTSTR lpValueName, LPCVOID lpBuffer, DWORD dwSize ) {
assert( lpValueName != NULL && lpBuffer != NULL && dwSize > 0 );
if ( ::RegSetValueEx( m_hKey, lpValueName, 0,
REG_BINARY, (LPBYTE)lpBuffer, dwSize ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::GetValue( LPCTSTR lpValueName, LPDWORD pdwBuffer ) {
assert( lpValueName != NULL && pdwBuffer != NULL );
DWORD dwSize = sizeof(DWORD);
if ( ::RegQueryValueEx( m_hKey, lpValueName, 0,
NULL, (LPBYTE)pdwBuffer, &dwSize ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::GetValue( LPCTSTR lpValueName, LPTSTR lpBuffer, DWORD dwBufSize ) {
assert( lpValueName != NULL && lpBuffer != NULL );
if ( ::RegQueryValueEx( m_hKey, lpValueName, 0,
NULL, (LPBYTE)lpBuffer, &dwBufSize ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::GetValueSize( LPCTSTR lpValueName, LPDWORD pdwSize ) {
assert( lpValueName != NULL && pdwSize != NULL );
if ( ::RegQueryValueEx( m_hKey, lpValueName,
NULL, NULL, NULL, pdwSize ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
BOOL RegKey::GetValue( LPCTSTR lpValueName, LPVOID lpBuffer, DWORD dwSize ) {
assert( lpValueName != NULL && lpBuffer != NULL );
if ( ::RegQueryValueEx( m_hKey, lpValueName, NULL, NULL,
(LPBYTE)lpBuffer, &dwSize ) != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
template<class T>
BOOL RegKey::SetValueT( LPCTSTR iName, const T* iT ) {
assert(iName != NULL);
assert(iT != NULL);
return SetValue(iName, iT, sizeof(T) );
}
template<class T>
BOOL RegKey::GetValueT( LPCTSTR iName, T* oT ) {
assert(iName != NULL);
assert(oT != NULL);
return GetValue(iName, oT, sizeof(T) );
}
BOOL RegKey::DeleteValue( LPCTSTR lpValueName ) {
assert( lpValueName != NULL );
return ( ::RegDeleteValue( m_hKey, lpValueName ) == ERROR_SUCCESS );
}