-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegValue.cpp
More file actions
99 lines (79 loc) · 2.63 KB
/
RegValue.cpp
File metadata and controls
99 lines (79 loc) · 2.63 KB
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
#include "stdafx.h"
#include "regvalue.h"
#include "win32_exception.h"
void RegValue::CheckError(LPCSTR szMessage)
{
if (m_stResult != ERROR_SUCCESS)
{
LPCSTR szMessageTmp = szMessage != NULL? szMessage: "Error while operating with register";
throw win32_exception(szMessageTmp, m_stResult);
}
}
RegValue::RegValue(HKEY hkey, LPCTSTR lpSubKey, BOOL bIsWritable):
m_bIsWritable(bIsWritable)
{
HKEY hKey;
m_stResult = ::RegCreateKeyEx(hkey, lpSubKey, 0, NULL, 0,
m_bIsWritable? KEY_WRITE: KEY_READ, NULL, &hKey, NULL);
CheckError("Can't open registry key");
m_hKey.Attach(hKey);
}
void RegValue::CheckReadPerissions()
{
if (m_bIsWritable)
throw std::logic_error("This key is for write");
}
void RegValue::CheckWritePerissions()
{
if (m_bIsWritable == false)
throw std::logic_error("This key is for read");
}
CAtlString RegValue::GetString(LPCTSTR lpValue)
{
CheckReadPerissions();
DWORD dLen;
DWORD dwType;
m_stResult = ::RegQueryValueEx( (HKEY)m_hKey.m_h, lpValue, 0, &dwType, NULL, &dLen);
CheckError("Can't query registry value");
if (dwType != REG_SZ)
throw std::runtime_error("Incompatible types");
CAtlString str;
m_stResult = ::RegQueryValueEx( (HKEY)m_hKey.m_h, lpValue, 0, NULL,
(LPBYTE)str.GetBuffer(dLen / sizeof(TCHAR) + 1), &dLen);
CheckError("Can't query registry value");
str.ReleaseBuffer(dLen / sizeof(TCHAR));
str.ReleaseBuffer();
return str;
}
DWORD RegValue::GetDword(LPCTSTR lpValue)
{
CheckReadPerissions();
DWORD dwType;
DWORD dwVal;
DWORD dwLen;
m_stResult = ::RegQueryValueEx( (HKEY)m_hKey.m_h, lpValue, 0, &dwType, NULL, &dwLen);
CheckError("Can't query registry value");
if ((dwType != REG_DWORD) || (dwLen != sizeof(dwVal)))
throw std::runtime_error("Incompatible types");
m_stResult = ::RegQueryValueEx( (HKEY)m_hKey.m_h, lpValue, 0, &dwType, (LPBYTE)&dwVal, &dwLen);
CheckError("Can't query registry value");
return dwVal;
}
void RegValue::SetString(LPCTSTR lpValue, LPCTSTR lpText, DWORD dwLen)
{
CheckWritePerissions();
m_stResult = ::RegSetValueEx( (HKEY)m_hKey.m_h, lpValue, 0, REG_SZ, (LPBYTE)lpText, dwLen * sizeof(lpText[0]));
CheckError("Can't set registry value");
}
void RegValue::SetDword(LPCTSTR lpValue, DWORD dwVal)
{
CheckWritePerissions();
m_stResult = ::RegSetValueEx( (HKEY)m_hKey.m_h, lpValue, 0, REG_DWORD, (LPBYTE)&dwVal, sizeof(dwVal));
CheckError("Can't set registry value");
}
void RegValue::Delete(LPCTSTR lpValue)
{
CheckWritePerissions();
m_stResult = ::RegDeleteValue( (HKEY)m_hKey.m_h, lpValue);
CheckError("Can't delete registry value");
}