FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/RegistryKey.cpp at main · ssh352/rstudio · GitHub
ssh352
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
RegistryKey.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
175 lines (147 loc) · 4.31 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
RegistryKey.cpp
Copy path
File metadata and controls
175 lines (147 loc) · 4.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* RegistryKey.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/system/RegistryKey.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
shared_core/Error.hpp
>
#
include
<
core/Log.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
system
{
RegistryKey::RegistryKey
()
: hKey_(
nullptr
)
{
}
RegistryKey::~RegistryKey
()
{
try
{
if
(hKey_)
::RegCloseKey
(hKey_);
hKey_ =
nullptr
;
}
catch
(...)
{
}
}
Error
RegistryKey::open
(
HKEY
hKey, std::string subKey,
REGSAM
samDesired)
{
if
(hKey_)
::RegCloseKey
(hKey_);
hKey_ =
nullptr
;
LONG
error = ::
RegOpenKeyEx
(hKey, subKey.
c_str
(),
0
, samDesired, &hKey_);
if
(error !=
ERROR_SUCCESS
)
{
hKey_ =
nullptr
;
return
systemError
(error,
ERROR_LOCATION
);
}
return
Success
();
}
bool
RegistryKey::isOpen
()
{
return
hKey_;
}
HKEY
RegistryKey::handle
()
{
return
hKey_;
}
std::string
RegistryKey::getStringValue
(std::string name,
std::string defaultValue)
{
std::string value;
Error error =
getStringValue
(name, &value);
if
(!error)
return
value;
return
defaultValue;
}
Error
RegistryKey::getStringValue
(std::string name, std::string *pValue)
{
if
(!hKey_)
return
systemError
(
ERROR_INVALID_HANDLE
,
ERROR_LOCATION
);
std::vector<
char
>
buffer
(
256
);
while
(
true
)
{
DWORD
type;
DWORD
size =
static_cast
<
DWORD
>(buffer.
capacity
());
LONG
result = ::
RegQueryValueEx
(hKey_,
name.
c_str
(),
nullptr
,
&type,
(
LPBYTE
)(&buffer[
0
]),
&size);
switch
(result)
{
case
ERROR_SUCCESS
:
{
if
(type !=
REG_SZ
&& type !=
REG_EXPAND_SZ
)
return
systemError
(
ERROR_INVALID_DATATYPE
,
ERROR_LOCATION
);
*pValue =
std::string
(&buffer[
0
], buffer.
capacity
());
//
REG_SZ and friends may or may not be null-terminated.
//
So trim the string at the first null, if any.
size_t
idxNull = pValue->
find
(
'
\0
'
);
if
(idxNull != std::string::npos)
pValue->
resize
(idxNull);
if
(type ==
REG_EXPAND_SZ
)
{
Error error =
expandEnvironmentVariables
(*pValue, pValue);
if
(error)
return
error;
}
return
Success
();
}
case
ERROR_MORE_DATA
:
buffer.
reserve
(size);
continue
;
default
:
return
systemError
(result,
ERROR_LOCATION
);
}
}
}
std::vector<std::string>
RegistryKey::keyNames
()
{
if
(!hKey_)
return
std::vector<std::string>();
LONG
result;
DWORD
subKeys, maxLen;
result = ::
RegQueryInfoKey
(hKey_,
nullptr
,
nullptr
,
nullptr
, &subKeys, &maxLen,
nullptr
,
nullptr
,
nullptr
,
nullptr
,
nullptr
,
nullptr
);
std::vector<
char
>
nameBuffer
(maxLen+
2
);
std::vector<std::string> results;
results.
reserve
(subKeys);
for
(
DWORD
i =
0
; ; i++)
{
DWORD
size =
static_cast
<
DWORD
>(nameBuffer.
capacity
());
LONG
result = ::
RegEnumKeyEx
(hKey_,
i,
&nameBuffer[
0
],
&size,
nullptr
,
nullptr
,
nullptr
,
nullptr
);
switch
(result)
{
case
ERROR_SUCCESS
:
results.
push_back
(
std::string
(&nameBuffer[
0
], size));
break
;
case
ERROR_NO_MORE_ITEMS
:
return
results;
default
:
Error error =
systemError
(result,
ERROR_LOCATION
);
LOG_ERROR
(error);
break
;
}
}
return
std::vector<std::string>();
}
}
//
namespace system
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL