-
Notifications
You must be signed in to change notification settings - Fork 17
/
Registry.cpp
186 lines (164 loc) · 5.1 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
//
// レジストリサポートクラス
//
#include "DebugOut.h"
#include "Registry.h"
#include "App.h"
#include "Pathlib.h"
#define INI_USE
CHAR CRegistry::m_szRegistryKey[MAX_PATH] = { "App" };
void CRegistry::SetRegistryKey( LPCTSTR lpszKey )
{
#ifndef INI_USE
if( lpszKey ) {
::_tcscpy( m_szRegistryKey, lpszKey );
}
#else
string str;
str = CApp::GetModulePath();
str += lpszKey;
::_tcscpy( m_szRegistryKey, str.c_str() );
#endif
}
HKEY CRegistry::GetRegistryKey()
{
HKEY hAppKey = NULL;
HKEY hSoftKey = NULL;
if( ::RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ, &hSoftKey) == ERROR_SUCCESS ) {
DWORD dw;
::RegCreateKeyEx( hSoftKey, m_szRegistryKey, 0, REG_NONE,
REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hAppKey, &dw);
}
if( hSoftKey )
::RegCloseKey( hSoftKey );
return hAppKey;
}
HKEY CRegistry::GetSectionKey( LPCTSTR lpszSection )
{
HKEY hAppKey = GetRegistryKey();
if( !hAppKey )
return NULL;
HKEY hSectionKey = NULL;
DWORD dw;
::RegCreateKeyEx( hAppKey, lpszSection, 0, REG_NONE,
REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hSectionKey, &dw );
::RegCloseKey( hAppKey );
return hSectionKey;
}
UINT CRegistry::GetProfileInt( LPCTSTR lpszSection, LPCTSTR lpszEntry, INT nDefault )
{
#ifndef INI_USE
HKEY hSecKey = GetSectionKey( lpszSection );
if( !hSecKey )
return nDefault;
DWORD dwValue;
DWORD dwType;
DWORD dwCount = sizeof(DWORD);
LONG lResult = ::RegQueryValueEx( hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)&dwValue, &dwCount );
::RegCloseKey( hSecKey );
return (lResult==ERROR_SUCCESS)?(UINT)dwValue:nDefault;
#else
return ::GetPrivateProfileInt( lpszSection, lpszEntry, nDefault, m_szRegistryKey );
#endif
}
BOOL CRegistry::GetProfileString( LPCSTR lpszSection, LPCSTR lpszEntry, LPVOID lpData, UINT nBytes )
{
#ifndef INI_USE
HKEY hSecKey = GetSectionKey( lpszSection );
if( !hSecKey )
return lpszDefault;
DWORD dwType, dwCount;
LONG lResult = ::RegQueryValueEx( hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount );
if( dwCount > nBytes ) {
::RegCloseKey( hSecKey );
return FALSE;
}
if( lResult == ERROR_SUCCESS ) {
lResult = ::RegQueryValueEx( hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, lpData, &dwCount );
}
::RegCloseKey( hSecKey );
return (lResult==ERROR_SUCCESS)?TRUE:FALSE;
#else
DWORD dw = ::GetPrivateProfileString( lpszSection, lpszEntry, "", (CHAR*)lpData, nBytes, m_szRegistryKey );
return (dw&&(dw<nBytes))?TRUE:FALSE;
#endif
}
BOOL CRegistry::GetProfileBinary( LPCSTR lpszSection, LPCSTR lpszEntry, LPVOID lpData, UINT nBytes )
{
#ifndef INI_USE
HKEY hSecKey = GetSectionKey(lpszSection);
if( !hSecKey )
return FALSE;
DWORD dwType, dwCount;
LONG lResult = ::RegQueryValueEx( hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount );
if( dwCount > nBytes ) {
::RegCloseKey( hSecKey );
return FALSE;
}
if( lResult == ERROR_SUCCESS ) {
lResult = ::RegQueryValueEx( hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, lpData, &dwCount );
}
::RegCloseKey( hSecKey );
return (lResult==ERROR_SUCCESS)?TRUE:FALSE;
#else
return ::GetPrivateProfileStruct( lpszSection, lpszEntry, lpData, nBytes, m_szRegistryKey );
#endif
}
BOOL CRegistry::WriteProfileInt( LPCSTR lpszSection, LPCSTR lpszEntry, INT nValue )
{
#ifndef INI_USE
HKEY hSecKey = GetSectionKey( lpszSection );
if( !hSecKey )
return FALSE;
LONG lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,
(LPBYTE)&nValue, sizeof(nValue));
::RegCloseKey( hSecKey );
return (lResult == ERROR_SUCCESS);
#else
CHAR szTemp[16];
wsprintf( szTemp, "%d", nValue );
return ::WritePrivateProfileString( lpszSection, lpszEntry, szTemp, m_szRegistryKey );
#endif
}
BOOL CRegistry::WriteProfileString( LPCSTR lpszSection, LPCSTR lpszEntry, LPCSTR lpszValue )
{
#ifndef INI_USE
LONG lResult;
if( !lpszEntry ) {
HKEY hAppKey = GetRegistryKey();
if( !hAppKey )
return FALSE;
lResult = ::RegDeleteKey( hAppKey, lpszSection );
::RegCloseKey( hAppKey );
} else if( !lpszValue ) {
HKEY hSecKey = GetSectionKey( lpszSection );
if( !hSecKey )
return FALSE;
lResult = ::RegDeleteValue( hSecKey, (LPTSTR)lpszEntry );
::RegCloseKey( hSecKey );
} else {
HKEY hSecKey = GetSectionKey( lpszSection );
if( !hSecKey )
return FALSE;
lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
(LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
::RegCloseKey( hSecKey );
}
return (lResult == ERROR_SUCCESS);
#else
return ::WritePrivateProfileString( lpszSection, lpszEntry, lpszValue, m_szRegistryKey );
#endif
}
BOOL CRegistry::WriteProfileBinary( LPCSTR lpszSection, LPCSTR lpszEntry, LPVOID lpData, UINT nBytes )
{
#ifndef INI_USE
HKEY hSecKey = GetSectionKey( lpszSection );
if( !hSecKey )
return FALSE;
LONG lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY, lpData, nBytes );
::RegCloseKey( hSecKey );
return (lResult == ERROR_SUCCESS);
#else
return ::WritePrivateProfileStruct( lpszSection, lpszEntry, lpData, nBytes, m_szRegistryKey );
#endif
}