Repository navigation
Expand file tree
/
Copy pathRegistryUtilities.cpp
More file actions
136 lines (125 loc) · 5.75 KB
/
Copy pathRegistryUtilities.cpp
File metadata and controls
136 lines (125 loc) · 5.75 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
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
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "RegistryUtilities.h"
#include <vector>
#include "logging.h"
namespace webdriver {
RegistryUtilities::RegistryUtilities(void) {
}
RegistryUtilities::~RegistryUtilities(void) {
}
bool RegistryUtilities::GetRegistryValue(const HKEY root_key,
const std::wstring& subkey,
const std::wstring& value_name,
std::wstring *value) {
std::string root_key_description = "HKEY_CURRENT_USER";
if (root_key == HKEY_CLASSES_ROOT) {
root_key_description = "HKEY_CLASSES_ROOT";
} else if (root_key == HKEY_LOCAL_MACHINE) {
root_key_description = "HKEY_LOCAL_MACHINE";
}
bool value_retrieved = false;
DWORD required_buffer_size;
DWORD value_type;
HKEY key_handle;
long registry_call_result = ::RegOpenKeyEx(root_key,
subkey.c_str(),
0,
KEY_QUERY_VALUE,
&key_handle);
if (ERROR_SUCCESS == registry_call_result) {
registry_call_result = ::RegQueryValueEx(key_handle,
value_name.c_str(),
NULL,
&value_type,
NULL,
&required_buffer_size);
if (ERROR_SUCCESS == registry_call_result) {
if (value_type == REG_SZ || value_type == REG_EXPAND_SZ || value_type == REG_MULTI_SZ) {
std::vector<wchar_t> value_buffer(required_buffer_size);
registry_call_result = ::RegQueryValueEx(key_handle,
value_name.c_str(),
NULL,
&value_type,
reinterpret_cast<LPBYTE>(&value_buffer[0]),
&required_buffer_size);
if (ERROR_SUCCESS == registry_call_result) {
*value = &value_buffer[0];
value_retrieved = true;
}
} else if (value_type == REG_DWORD) {
DWORD numeric_value = 0;
registry_call_result = ::RegQueryValueEx(key_handle,
value_name.c_str(),
NULL,
&value_type,
reinterpret_cast<LPBYTE>(&numeric_value),
&required_buffer_size);
if (ERROR_SUCCESS == registry_call_result) {
// Coerce the numeric value to a string to return back.
// Assume 10 characters will be enough to hold the size
// of the value.
std::vector<wchar_t> numeric_value_buffer(10);
_ltow_s(numeric_value, &numeric_value_buffer[0], numeric_value_buffer.size(), 10);
*value = &numeric_value_buffer[0];
value_retrieved = true;
}
} else {
LOG(WARN) << "Unexpected value type of " << value_type
<< " for RegQueryValueEx was found for value with name "
<< LOGWSTRING(value_name) << " in subkey "
<< LOGWSTRING(subkey) << " in hive "
<< root_key_description;
}
if (ERROR_SUCCESS != registry_call_result) {
LOG(WARN) << "RegQueryValueEx failed with error code "
<< registry_call_result << " retrieving value with name "
<< LOGWSTRING(value_name) << " in subkey "
<< LOGWSTRING(subkey) << "in hive "
<< root_key_description;
}
} else {
LOG(WARN) << "RegQueryValueEx failed with error code "
<< registry_call_result
<< " retrieving required buffer size for value with name "
<< LOGWSTRING(value_name) << " in subkey "
<< LOGWSTRING(subkey) << " in hive "
<< root_key_description;
}
::RegCloseKey(key_handle);
} else {
LOG(WARN) << "RegOpenKeyEx failed with error code "
<< registry_call_result << " attempting to open subkey "
<< LOGWSTRING(subkey) << " in hive "
<< root_key_description;
}
return value_retrieved;
}
bool RegistryUtilities::RegistryKeyExists(HKEY root_key,
const std::wstring& subkey) {
HKEY key_handle;
long registry_call_result = ::RegOpenKeyEx(root_key,
subkey.c_str(),
0,
KEY_QUERY_VALUE,
&key_handle);
bool result = (ERROR_SUCCESS == registry_call_result);
if (result) {
::RegCloseKey(key_handle);
}
return result;
}
} // namespace webdriver