-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRegSettings.h
113 lines (83 loc) · 3.23 KB
/
RegSettings.h
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
/******************************************************************************
Module name: RegSettings.cpp
Written by: Jeffrey Richter
Purpose: CRegSettings C++ class to help with registry manipulations
******************************************************************************/
#pragma once
/////////////////////////////////////////////////////////////////////////////
class CRegSettings {
public: // Constructors/destructor
CRegSettings() : m_hkeySubkey(NULL) { };
CRegSettings(BOOL fReadOnly, HKEY hkeyRoot, LPCTSTR pszSubkey)
: m_hkeySubkey(NULL) {
OpenSubkey(fReadOnly, hkeyRoot, pszSubkey);
}
~CRegSettings() { CloseKey(); }
public: // Opening/closing registry subkey functions
operator HKEY() const { return(m_hkeySubkey); }
LONG OpenSubkey(BOOL fReadOnly, HKEY hkeyRoot, LPCTSTR pszSubkey) {
CloseKey();
LONG lErr;
if (fReadOnly) {
lErr = ::RegOpenKeyEx(hkeyRoot, pszSubkey, 0,
KEY_QUERY_VALUE, &m_hkeySubkey);
} else {
DWORD dwDisposition;
lErr = RegCreateKeyEx(hkeyRoot, pszSubkey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE | KEY_SET_VALUE,
NULL, &m_hkeySubkey, &dwDisposition);
}
return(lErr);
}
void CloseKey() {
if (m_hkeySubkey != NULL) {
::RegCloseKey(m_hkeySubkey);
m_hkeySubkey = NULL;
}
}
public: // Reading/writing registry value functions
LONG GetDWORD(LPCTSTR pszValName, PDWORD pdw) const {
DWORD cbData = sizeof(pdw);
return(GetVal(pszValName, (PBYTE) pdw, &cbData));
}
LONG SetDWORD(LPCTSTR pszValName, DWORD dw) const {
return(SetVal(pszValName, REG_DWORD, (BYTE*) &dw, sizeof(dw)));
}
LONG GetString(LPCTSTR pszValName, LPTSTR psz, int nMaxSize) const {
DWORD cbData = nMaxSize;
return(GetVal(pszValName, (PBYTE) psz, &cbData));
}
LONG SetString(LPCTSTR pszValName, LPCTSTR psz) const {
return(SetVal(pszValName, REG_SZ, (PBYTE) psz,
sizeof(TCHAR) * (lstrlen(psz) + 1)));
}
LONG GetMultiString(LPCTSTR pszValName, LPTSTR psz, int nMaxSize) const {
return(GetString(pszValName, psz, nMaxSize));
}
LONG SetMultiString(LPCTSTR pszValName, LPCTSTR psz) const {
for (DWORD cch = 0; psz[cch] != 0; cch += 1 + lstrlen(&psz[cch])) ;
return(SetVal(pszValName, REG_MULTI_SZ, (PBYTE) psz,
sizeof(TCHAR) * (cch + 1)));
}
LONG GetBinary(LPCTSTR pszValName, PBYTE pb, PDWORD pcbData) const {
return(GetVal(pszValName, pb, pcbData));
}
LONG SetBinary(LPCTSTR pszValName, CONST BYTE* pb, int nSize) const {
return(SetVal(pszValName, REG_BINARY, pb, nSize));
}
LONG GetSize(LPCTSTR pszValName, PDWORD pdw) {
return(GetVal(pszValName, NULL, pdw));
}
private:
HKEY m_hkeySubkey;
private:
LONG GetVal(LPCTSTR pszValName, PBYTE pbData, PDWORD pcb) const {
return(::RegQueryValueEx(m_hkeySubkey, pszValName,
NULL, NULL, pbData, pcb));
}
LONG SetVal(LPCTSTR pszValName, DWORD dwType, CONST BYTE *pbData, DWORD cb) const {
return(::RegSetValueEx(m_hkeySubkey, pszValName,
0, dwType, pbData, cb));
}
};
//////////////////////////////// End of File //////////////////////////////////