-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathkeys.cpp
More file actions
462 lines (434 loc) · 10.9 KB
/
Copy pathkeys.cpp
File metadata and controls
462 lines (434 loc) · 10.9 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* key.cpp
*
* Windows Registry Keys access.
*
* This file is part of the WinVetSim distribution (https://github.com/OpenVetSimDevelopers/sim-mgr).
*
* Copyright (c) 2021-2023 VetSim, Cornell University College of Veterinary Medicine Ithaca, NY
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Configuration parameters are now kept in the WinVetSim html direcotry, in the file winvetsim.ini
* This file provides a means to transfer previously set Registry keys to the .ini file.
* 1 - Defaults are set at program start
* 2 - Values are read from the Registry
* 3 - Values are read from winvetsim.ini
* 4 - If a value for a setting is found in the .ini file, it is used.
* 5 - If an entry is missing the entry, the .ini file us updated to add it.
*/
// QueryKey - Enumerates the subkeys of key and its associated values.
// hKey - Key whose subkeys and values are to be enumerated.
#include "vetsim.h"
#include <windows.h>
#include <stdio.h>
#include "ini.h"
using namespace std;
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
WCHAR achValue[MAX_VALUE_NAME];
void QueryKey(HKEY hKey)
{
WCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
WCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
DWORD cchValue = MAX_VALUE_NAME;
// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
wprintf(L"RegQueryInfoKey() returns %u\n", retCode);
// Enumerate the subkeys, until RegEnumKeyEx() fails
if (cSubKeys)
{
wprintf(L"\nNumber of subkeys: %d\n", cSubKeys);
for (i = 0; i < cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey,
i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
wprintf(L"(%d) %s\n", i + 1, achKey);
}
}
}
else
{
wprintf(L"No subkeys to be enumerated!\n");
}
// Enumerate the key values
if (cValues)
{
wprintf(L"\nNumber of values: %d\n", cValues);
for (i = 0, retCode = ERROR_SUCCESS; i < cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hKey, i, achValue, &cchValue, NULL, NULL,
NULL, NULL);
if (retCode == ERROR_SUCCESS)
{
wprintf(L"(%d) %s\n", i + 1, achValue);
}
}
}
else
{
wprintf(L"No values to be enumerated!\n");
}
}
BOOL writeStringInRegistry(HKEY hKeyParent, LPCWSTR subkey, LPCSTR valueName, char* strData, int len)
{
DWORD Ret;
HKEY hKey;
LPBYTE bptr = (LPBYTE)strData;
//Check if the registry exists
Ret = RegOpenKeyEx(
hKeyParent,
subkey,
0,
KEY_WRITE,
&hKey
);
if (Ret == ERROR_SUCCESS)
{
if (ERROR_SUCCESS !=
RegSetValueExA(
hKey,
valueName,
0,
REG_SZ,
bptr,
len ) )
{
RegCloseKey(hKey);
return FALSE;
}
RegCloseKey(hKey);
return TRUE;
}
return FALSE;
}
int WriteInRegistry(HKEY hKeyParent, LPCWSTR subkey, LPCWSTR valueName, DWORD data )
{
DWORD Ret; //use to check status
HKEY hKey; //key
//Open the key
Ret = RegOpenKeyEx(
hKeyParent,
subkey,
0,
KEY_WRITE,
&hKey
);
if (Ret == ERROR_SUCCESS)
{
//Set the value in key
if (ERROR_SUCCESS !=
RegSetValueEx(
hKey,
valueName,
0,
REG_DWORD,
reinterpret_cast<BYTE*>(&data),
sizeof(data)))
{
RegCloseKey(hKey);
return FALSE;
}
//close the key
RegCloseKey(hKey);
return TRUE;
}
return FALSE;
}
DWORD CreateRegistryKey(HKEY hKeyParent, LPCWSTR subkey)
{
DWORD dwDisposition; //It verify new key is created or open existing key
HKEY hKey;
DWORD Ret;
Ret =
RegCreateKeyEx(
hKeyParent,
subkey,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
&dwDisposition);
if (Ret != ERROR_SUCCESS)
{
printf("Error opening or creating key.\n");
}
else
{
RegCloseKey(hKey);
}
return Ret;
}
//Read data from registry
DWORD readDwordValueRegistry(HKEY hKeyParent, LPCWSTR subkey, LPCWSTR valueName, DWORD* readData)
{
HKEY hKey;
DWORD Ret;
//Check if the registry exists
Ret = RegOpenKeyExW(
hKeyParent,
subkey,
0,
KEY_READ,
&hKey
);
if (Ret == ERROR_SUCCESS)
{
DWORD data;
DWORD len = sizeof(DWORD);//size of data
Ret = RegQueryValueExW(
hKey,
valueName,
NULL,
NULL,
(LPBYTE)(&data),
&len
);
if (Ret == ERROR_SUCCESS)
{
(*readData) = data;
}
RegCloseKey(hKey);
}
return Ret;
}
DWORD readStringFromRegistry(HKEY hKeyParent, LPCWSTR subkey, LPCSTR valueName, char* readData, DWORD len)
{
HKEY hKey;
DWORD readDataLen = len;
//Check if the registry exists
DWORD Ret = RegOpenKeyEx(
hKeyParent,
subkey,
0,
KEY_READ,
&hKey
);
if (Ret == ERROR_SUCCESS)
{
Ret = RegQueryValueExA(
hKey,
valueName,
NULL,
NULL,
(BYTE*)readData,
&readDataLen
);
RegCloseKey(hKey);
}
return (Ret);
}
void outputData(mINI::INIStructure const& ini)
{
for (auto const& it : ini)
{
auto const& section = it.first;
auto const& collection = it.second;
std::cout << "[" << section << "]" << std::endl;
for (auto const& it2 : collection)
{
auto const& key = it2.first;
auto const& value = it2.second;
std::cout << key << "=" << value << std::endl;
}
std::cout << std::endl;
}
}
HKEY whichKey = HKEY_CURRENT_USER;
void
readSubKeys(void)
{
DWORD data;
DWORD Ret;
char stringBuf[FILENAME_SIZE];
int len;
Ret = readDwordValueRegistry(whichKey, L"SOFTWARE\\WinVetSim", L"PulsePortNum", &data);
if (Ret == ERROR_SUCCESS)
{
localConfig.port_pulse = data;
}
Ret = readDwordValueRegistry(whichKey, L"SOFTWARE\\WinVetSim", L"StatusPortNum", &data);
if (Ret == ERROR_SUCCESS)
{
localConfig.port_status = data;
}
Ret = readDwordValueRegistry(whichKey, L"SOFTWARE\\WinVetSim", L"ServerPortNum", &data);
if (Ret == ERROR_SUCCESS)
{
localConfig.php_server_port = data;
}
Ret = readStringFromRegistry(whichKey, L"SOFTWARE\\WinVetSim", "ServerAddress", stringBuf, STR_SIZE);
if (Ret == ERROR_SUCCESS)
{
sprintf_s(localConfig.php_server_addr, "%s", stringBuf);
}
Ret = readStringFromRegistry(whichKey, L"SOFTWARE\\WinVetSim", "LogName", stringBuf, STR_SIZE);
if (Ret == ERROR_SUCCESS)
{
sprintf_s(localConfig.log_name, "%s", stringBuf);
}
Ret = readStringFromRegistry(whichKey, L"SOFTWARE\\WinVetSim", "HTML_Path", stringBuf, STR_SIZE);
if (Ret == ERROR_FILE_NOT_FOUND)
{
len = (int)strlen(localConfig.html_path);
Ret = writeStringInRegistry(whichKey, L"SOFTWARE\\WinVetSim", "HTML_Path", localConfig.html_path, len);
}
else if (Ret == ERROR_SUCCESS)
{
sprintf_s(localConfig.html_path, "%s", stringBuf);
}
}
/*
Look for SOFTWARE/WinVetSim Key in HKEY_CURRENT_USER.
If not found, look for in HKEY_LOCAL_MACHINE
If not found:
Create Key HKEY_CURRENT_USER/SOFTWARE/WinVetSim
Create Key HKEY_CURRENT_USER/SOFTWARE/WinVetSim\HTML_Path - Value set to
*/
int getKeys()
{
HKEY theKey;
LPCTSTR strKeyName = L"SOFTWARE\\WinVetSim";
int rval = 0;
bool ret;
// Check first for Private Key
long sts = RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\WinVetSim", 0, KEY_READ, &theKey);
if ( ERROR_SUCCESS == sts )
{
// Found
rval = 1;
whichKey = HKEY_CURRENT_USER;
}
else if (ERROR_NO_MATCH == sts || ERROR_FILE_NOT_FOUND == sts)
{
// See if a Public Key exists
long sts = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\WinVetSim", 0, KEY_READ, &theKey);
if (ERROR_SUCCESS == sts)
{
// Found
rval = 1;
whichKey = HKEY_LOCAL_MACHINE;
}
else if (ERROR_NO_MATCH == sts || ERROR_FILE_NOT_FOUND == sts)
{
// Continue with the default path value
rval = -1;
}
}
else
{
rval = -1;
}
char errorstr[256];
if (rval >= 0)
{
// Main key is present. So set SubKeys
RegCloseKey(theKey);
readSubKeys();
}
// Now proces the ini file
char iniFileName[512];
errno_t et;
sprintf_s(iniFileName,
"%s/%s",
HTML_PATH,
"winvetsim.ini");
mINI::INIFile file(iniFileName);
mINI::INIStructure ini;
ret = file.read(ini);
if (ret != true)
{
et = strerror_s(errorstr, errno);
printf("INI Read Fails (%s), Try to create.\n", errorstr);
std::ofstream fileWriteStream(iniFileName);
if (fileWriteStream.is_open())
{
fileWriteStream << "; Configuration file for WinVetSim" << std::endl;
fileWriteStream << "[Server]" << std::endl;
fileWriteStream << "serverAddress = " << PHP_SERVER_ADDR << std::endl;
fileWriteStream << "serverPort = " << PHP_SERVER_PORT << std::endl;
fileWriteStream << "" << std::endl;
fileWriteStream << "[Listeners]" << std::endl;
fileWriteStream << "pulsePort = " << PORT_PULSE << std::endl;
fileWriteStream << "statusPort = " << PORT_STATUS << std::endl;
ret = file.read(ini);
if (ret != true)
{
et = strerror_s(errorstr, errno);
printf("INI Read fails after create (%s)\n", errorstr);
}
}
else
{
et = strerror_s(errorstr, errno);
printf("INI is_open() fails ()%s)\n", errorstr);
}
}
if (ret == true)
{
outputData(ini);
if (ini["Server"]["serverPort"].length() > 0)
{
localConfig.php_server_port = atoi((const char*)ini["Server"]["serverPort"].c_str());
}
if (ini["Server"]["serverAddress"].length() > 0)
{
sprintf_s(localConfig.php_server_addr, "%s", ini["Server"]["serverAddress"].c_str());
}
if (ini["Listeners"]["pulsePort"].length() > 0)
{
localConfig.port_pulse = atoi((const char*)ini["Listeners"]["pulsePort"].c_str());
}
if (ini["Listeners"]["statusPort"].length() > 0)
{
localConfig.port_status = atoi((const char*)ini["Listeners"]["statusPort"].c_str());
}
printf("Data from INI: Server %s:%d, Pulse %d, Status %d\n",
localConfig.php_server_addr,
localConfig.php_server_port,
localConfig.port_pulse,
localConfig.port_status);
}
return (rval);
}