-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathregistry.cpp
145 lines (125 loc) · 3.83 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
#include "registry.h"
#include "guid.h"
#include "helpers.h"
DWORD readRegistryValueString(_In_ CONF_VALUE conf_value, _Outptr_result_nullonfailure_ PWSTR *data, _In_ PWSTR defaultValue) {
HKEY rootKeyValue = s_CONF_VALUES[conf_value].ROOT_KEY;
PWSTR confKeyName = s_CONF_VALUES[conf_value].KEY_NAME;
PWSTR confValueName = s_CONF_VALUES[conf_value].VALUE_NAME;
DWORD dwSize = 0;
// size_t len;
wchar_t confKeyNameCLSID[1024];
HRESULT hr;
PWSTR clsid;
*data = nullptr;
hr = StringFromCLSID(CLSID_CSample, &clsid);
if (hr == S_OK) {
wcscpy_s(confKeyNameCLSID, 1024, confKeyName);
if (confKeyName == (PWSTR)MOTP_SETTINGS) {
wcscat_s(confKeyNameCLSID, 1024, clsid);
}
CoTaskMemFree(clsid);//not needed
if (DEVELOPING) PrintLn(L"Reading REGISTRY Key: ", confKeyNameCLSID, L"\\", confValueName);
DWORD keyType = 0;
DWORD dataSize = 0;
const DWORD flags = RRF_RT_REG_SZ; // Only read strings (REG_SZ)
LONG result = ::RegGetValue(
rootKeyValue,
confKeyNameCLSID,
confValueName,
flags,
&keyType,
nullptr, // pvData == nullptr --> Request buffer size for string
&dataSize);
if ((result == ERROR_SUCCESS) && (keyType == REG_SZ)) {
//reserve read return
*data = (PWSTR)CoTaskMemAlloc(dataSize);
result = ::RegGetValue(
rootKeyValue,
confKeyNameCLSID,
confValueName,
flags,
nullptr,
*data, // Write string in this destination buffer
&dataSize);
if (result == ERROR_SUCCESS) {
dwSize = dataSize / sizeof(WCHAR);
if (DEVELOPING) PrintLn("Len %d", dataSize);
return dwSize;
}
else {
CoTaskMemFree(*data);
*data = nullptr;
dwSize = 0;
}
}
else {
/* https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx */
if (DEVELOPING) PrintLn("ReadRegistryValue: System Error Code ( %d )", result);
}
}
dwSize = wcslen(defaultValue);
*data = (PWSTR)CoTaskMemAlloc(sizeof(wchar_t) * (dwSize + 1));
wcscpy_s(*data, 1024, defaultValue);
return dwSize;
}
DWORD readRegistryValueInteger(_In_ CONF_VALUE conf_value, _In_ DWORD defaultValue) {
DWORD DWdata;
DWORD dataSize;
HKEY rootKeyValue = s_CONF_VALUES[conf_value].ROOT_KEY;
PWSTR confKeyName = s_CONF_VALUES[conf_value].KEY_NAME;
PWSTR confValueName = s_CONF_VALUES[conf_value].VALUE_NAME;
wchar_t confKeyNameCLSID[1024];
HRESULT hr;
PWSTR clsid;
hr = StringFromCLSID(CLSID_CSample, &clsid);
if (hr == S_OK) {
wcscpy_s(confKeyNameCLSID, 1024, confKeyName);
if (confKeyName == (PWSTR)MOTP_SETTINGS) {
wcscat_s(confKeyNameCLSID, 1024, clsid);
}
CoTaskMemFree(clsid);//not needed
if (DEVELOPING) PrintLn(L"Reading REGISTRY Key:", confKeyNameCLSID, L"\\", confValueName);
dataSize = sizeof(DWORD);
LONG result = ::RegGetValue(
rootKeyValue,
confKeyNameCLSID,
confValueName,
RRF_RT_REG_DWORD,
NULL,
&DWdata,
&dataSize);
if (result == ERROR_SUCCESS) {
return DWdata;
}
else if (result == ERROR_MORE_DATA) {
if (DEVELOPING) PrintLn("Result = %d", DWdata);
if (DEVELOPING) PrintLn("More data ( %d )", dataSize);
return 1;
}
else {
if (DEVELOPING) PrintLn("ReadRegistryValue: System Error Code ( %d )", result);
if (DEVELOPING) PrintLn("default value: %d", defaultValue);
return defaultValue;
}
}
else {
if (DEVELOPING) PrintLn("default value: %d", defaultValue);
return defaultValue;
}
/*returnStatus = RegOpenKeyExA(rootKeyValue, confKeyName, NULL, KEY_QUERY_VALUE, &hKey);
if (returnStatus == ERROR_SUCCESS)
{
dwSize = sizeof(DWORD);
returnStatus = RegQueryValueExA(hKey, confValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(&lszValue), &dwSize);
if (returnStatus == ERROR_SUCCESS)
{
*data = lszValue;
}
else
{
dwSize = 0;
}
RegCloseKey(hKey);
}
*/
}