-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathregistry3264.cpp
168 lines (138 loc) · 5 KB
/
registry3264.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************
* *
* Copyright (c) 2017 *
* FastFieldSolvers S.R.L. http://www.fastfieldsolvers.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
// class derived from wxWidgets wxRegKey to manage 64 bits vs. 32 bits keys
#include "wx_pch.h"
// valid only for MS Win
#ifdef __WXMSW__
#include "registry3264.h"
wxRegKey3264::wxRegKey3264(const wxString& strKey, REGSAM mode)
{
key3264access = mode;
::wxRegKey(strKey);
}
// parent is a normal regkey
wxRegKey3264::wxRegKey3264(const wxRegKey3264& keyParent, const wxString& strKey, REGSAM mode)
{
key3264access = mode;
::wxRegKey(keyParent, strKey);
}
void wxRegKey3264::SetKey32bits()
{
key3264access = KEY_WOW64_32KEY;
}
// opens key (it's not an error to call Open() on an already opened key)
bool wxRegKey3264::Open(AccessMode mode)
{
REGSAM mode3264;
if ( IsOpened() )
{
if ( mode <= m_mode )
return true;
// we had been opened in read mode but now must be reopened in write
Close();
}
HKEY tmpKey;
mode3264 = (Read ? KEY_READ : KEY_ALL_ACCESS) | key3264access;
m_dwLastError = ::RegOpenKeyEx
(
(HKEY) m_hRootKey,
m_strKey,
RESERVED,
mode3264,
&tmpKey
);
if ( m_dwLastError != ERROR_SUCCESS )
{
wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"),
(const char*)GetName());
return false;
}
m_hKey = (WXHKEY) tmpKey;
m_mode = mode;
return true;
}
// returns true if the key exists
bool wxRegKey3264::Exists() const
{
// opened key has to exist, try to open it if not done yet
return IsOpened() ? true : KeyExists(m_hRootKey, m_strKey);
}
// returns true if given subkey exists
bool wxRegKey3264::HasSubKey(const wxChar *szKey) const
{
// this function should be silent, so suppress possible messages from Open()
wxLogNull nolog;
if ( !CONST_CAST Open(Read) )
return false;
return KeyExists(m_hKey, szKey);
}
// creates key, failing if it exists and !bOkIfExists
bool wxRegKey3264::Create(bool bOkIfExists)
{
// check for existence only if asked (i.e. order is important!)
if ( !bOkIfExists && Exists() )
return false;
if ( IsOpened() )
return true;
HKEY tmpKey;
DWORD disposition;
m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey,
NULL, // reserved
NULL, // class string
0,
KEY_READ | KEY_WRITE | key3264access,
NULL,
&tmpKey,
&disposition);
if ( m_dwLastError != ERROR_SUCCESS ) {
wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"),
(const char*)GetName());
return false;
}
else
{
m_hKey = (WXHKEY) tmpKey;
return true;
}
}
bool wxRegKey3264::KeyExists(WXHKEY hRootKey, const wxChar *szKey)
{
// don't close this key itself for the case of empty szKey!
if ( wxIsEmpty(szKey) )
return true;
HKEY hkeyDummy;
if ( ::RegOpenKeyEx
(
(HKEY)hRootKey,
szKey,
RESERVED,
KEY_READ | key3264access, // we might not have enough rights for rw access
&hkeyDummy
) == ERROR_SUCCESS )
{
::RegCloseKey(hkeyDummy);
return true;
}
return false;
}
#endif