/
regwrap.cpp
196 lines (146 loc) · 5.7 KB
/
regwrap.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
/*****************************************************************************
File name: regwrap.cpp
Project: FarVCS plugin
Purpose: Simple wrapper class for registry access
Compiler: MS Visual C++ 8.0
Authors: Michael Steinhaus
Dependencies: STL, Win32
*****************************************************************************/
#include <utility>
#include <shlwapi.h>
#include "regwrap.h"
struct keypair
{
LPCTSTR szName;
HKEY hRootKey;
};
static keypair rkam[] =
{
{ _T("HKLM"), HKEY_LOCAL_MACHINE },
{ _T("HKCU"), HKEY_CURRENT_USER },
{ _T("HKCR"), HKEY_CLASSES_ROOT },
{ _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE },
{ _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER },
{ _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT },
{ _T("HKEY_USERS"), HKEY_USERS },
{ _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG },
{ _T("HKEY_PERFORMANCE_DATA"), HKEY_PERFORMANCE_DATA },
{ _T("HKEY_DYN_DATA"), HKEY_DYN_DATA },
{ 0, 0 }
};
void RegWrap::Init( HKEY hRootKey, const tstring& sSubKey, REGSAM samDesired )
{
hkSession = 0;
// Open/create the registry key
nInitResult = samDesired & KEY_WRITE ? ::RegCreateKeyEx( hRootKey, sSubKey.c_str(), 0, _T(""), 0, samDesired, 0, &hkSession, 0 )
: ::RegOpenKeyEx( hRootKey, sSubKey.c_str(), 0, samDesired, &hkSession );
if ( nInitResult != ERROR_SUCCESS )
hkSession = 0;
}
RegWrap::RegWrap( const tstring& sKey, REGSAM samDesired ) :
hkSession( 0 )
{
// Parse the key
unsigned long iFirstSlash = sKey.find_first_of( _T("\\") );
if ( iFirstSlash == tstring::npos )
return;
tstring sRootKey = sKey.substr( 0, iFirstSlash );
tstring sSubKey = sKey.substr( iFirstSlash+1 );
// Get the root key by its name
HKEY hRootKey = (HKEY)-1;
for ( unsigned long i = 0; rkam[i].szName != 0; ++i )
if ( sRootKey == rkam[i].szName )
hRootKey = rkam[i].hRootKey;
if ( hRootKey == (HKEY)-1 )
return;
Init( hRootKey, sSubKey, samDesired );
}
RegWrap::~RegWrap()
{
if ( !this->operator!() )
::RegCloseKey( hkSession );
}
tstring RegWrap::ReadString( const tstring& sName, LPCTSTR szDefaultValue ) const
{
unsigned long dwValueType;
TCHAR szBuf[4096];
unsigned long dwBufSize = sizeof szBuf - 1;
// Read the value into buffer
if ( ReadToBuf(sName, dwValueType, (LPBYTE)szBuf, dwBufSize) != 0 || dwValueType != REG_SZ )
return szDefaultValue ? szDefaultValue : _T("");
return szBuf;
}
unsigned long RegWrap::ReadDword( const tstring& sName, unsigned long dwDefaultValue ) const
{
unsigned long dwValueType;
unsigned long dwValue;
unsigned long dwBufSize = sizeof dwValue;
// Read the value into buffer
if ( ReadToBuf(sName, dwValueType, (unsigned char*)&dwValue, dwBufSize) != 0 || dwValueType != REG_DWORD )
return dwDefaultValue;
return dwValue;
}
long RegWrap::ReadToBuf( const tstring& sName, unsigned long &dwValueType, unsigned char *buf, unsigned long &dwBufSize ) const
{
if ( this->operator!() || buf == 0 )
return -1;
// Read the value
long nResult = ::RegQueryValueEx( hkSession, sName.c_str(), 0, &dwValueType, buf, &dwBufSize );
if ( nResult != ERROR_SUCCESS )
return -1;
if ( dwValueType == REG_SZ || dwValueType == REG_MULTI_SZ || dwValueType == REG_EXPAND_SZ )
buf[dwBufSize] = '\0';
return 0;
}
HRESULT RegWrap::WriteString( const tstring& sName, const tstring& sValue ) const
{
if ( this->operator!() )
return HRESULT_FROM_WIN32( nInitResult );
long nResult = ::RegSetValueEx( hkSession, sName.c_str(), 0, REG_SZ, (const BYTE *)sValue.c_str(), sValue.length() );
return HRESULT_FROM_WIN32( nResult );
}
HRESULT RegWrap::WriteDword( const tstring& sName, unsigned long dwValue ) const
{
if ( this->operator!() )
return HRESULT_FROM_WIN32( nInitResult );
long nResult = ::RegSetValueEx( hkSession, sName.c_str(), 0, REG_DWORD, (const BYTE *)&dwValue, sizeof(dwValue) );
return HRESULT_FROM_WIN32( nResult );
}
HRESULT RegWrap::DeleteValue( const tstring& sName ) const
{
if ( this->operator!() )
return HRESULT_FROM_WIN32( nInitResult );
long nResult = ::RegDeleteValue( hkSession, sName.c_str() );
return HRESULT_FROM_WIN32( nResult );
}
HRESULT RegWrap::DeleteKey( const tstring& sName ) const
{
if ( this->operator!() )
return HRESULT_FROM_WIN32( nInitResult );
long nResult = ::SHDeleteKey( hkSession, sName.c_str() );
return HRESULT_FROM_WIN32( nResult );
}
template <typename ENUM_F> HRESULT RegWrap::Enum( std::vector<tstring>& v, ENUM_F enum_f ) const
{
v.clear();
if ( this->operator!() )
return HRESULT_FROM_WIN32( nInitResult );
long nResult = 0;
TCHAR szBuf[4096];
for ( unsigned long i = 0; ; ++i )
{
unsigned long dwBufSize = array_size(szBuf);
nResult = enum_f( hkSession, i, szBuf, &dwBufSize, 0, 0, 0, 0 );
if ( nResult == ERROR_SUCCESS || nResult == ERROR_MORE_DATA ) {
v.push_back( szBuf );
continue;
}
if ( nResult == ERROR_NO_MORE_ITEMS ) {
nResult = S_OK;
break;
}
}
return HRESULT_FROM_WIN32( nResult );
}
HRESULT RegWrap::EnumKeys ( std::vector<tstring>& v ) const { return Enum( v, ::RegEnumKeyEx ); }
HRESULT RegWrap::EnumValues( std::vector<tstring>& v ) const { return Enum( v, ::RegEnumValue ); }