-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBuff.hpp
More file actions
777 lines (733 loc) · 25.8 KB
/
Copy pathAutoBuff.hpp
File metadata and controls
777 lines (733 loc) · 25.8 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
// AutoBuff.hpp --- C++/Win32 auto-buffering framework
// Author: katahiromz
// License: MIT
#pragma once
#if !defined(__cplusplus) || (__cplusplus < 201103L)
#error AutoBuff.hpp requires C++11 or later.
#endif
#ifndef _INC_WINDOWS
#include <windows.h>
#endif
#include <string>
#include <vector>
#include <stdarg.h>
#ifndef AUTOBUFF_MAX
#define AUTOBUFF_MAX 65536
#endif
namespace AutoBuff_internal {
template <typename TChar, typename TFunc>
inline DWORD call_with_retry(std::basic_string<TChar>& str, TFunc func) {
DWORD size = func(nullptr, 0);
if (size > 0) {
str.resize(size - 1);
DWORD written = func(&str[0], size);
str.resize(written);
return written;
}
str.clear();
return 0;
}
template <typename TChar, typename TFunc>
inline DWORD call_with_expand(std::basic_string<TChar>& str, TFunc func) {
DWORD size = MAX_PATH;
while (true) {
str.resize(size);
DWORD res = func(&str[0], size);
if (res < size - 1) {
str.resize(res);
return res;
}
size *= 2;
if (size > AUTOBUFF_MAX) break;
}
return 0;
}
} // namespace AutoBuff_internal
inline UINT AutoBuff_GetAtomNameA(ATOM nAtom, std::string& str) {
char buf[256];
UINT res = GetAtomNameA(nAtom, buf, 256);
if (res > 0) str = buf;
return res;
}
inline UINT AutoBuff_GetAtomNameW(ATOM nAtom, std::wstring& str) {
wchar_t buf[256];
UINT res = GetAtomNameW(nAtom, buf, 256);
if (res > 0) str = buf;
return res;
}
inline BOOL AutoBuff_GetComputerNameA(std::string& str) {
DWORD s = 0;
GetComputerNameA(NULL, &s);
if (s == 0) {
str.clear();
return FALSE;
}
str.resize(s);
BOOL ret = GetComputerNameA(&str[0], &s);
if (ret)
str.resize(s);
else
str.clear();
return ret;
}
inline BOOL AutoBuff_GetComputerNameW(std::wstring& str) {
DWORD s = 0;
GetComputerNameW(NULL, &s);
if (s == 0) {
str.clear();
return FALSE;
}
str.resize(s);
BOOL ret = GetComputerNameW(&str[0], &s);
if (ret)
str.resize(s);
else
str.clear();
return ret;
}
inline BOOL AutoBuff_GetComputerNameExA(COMPUTER_NAME_FORMAT t,
std::string& str) {
DWORD s = 0;
GetComputerNameExA(t, NULL, &s);
if (s == 0) {
str.clear();
return FALSE;
}
str.resize(s);
BOOL ret = GetComputerNameExA(t, &str[0], &s);
if (ret)
str.resize(s);
else
str.clear();
return ret;
}
inline BOOL AutoBuff_GetComputerNameExW(COMPUTER_NAME_FORMAT t,
std::wstring& str) {
DWORD s = 0;
GetComputerNameExW(t, NULL, &s);
if (s == 0) {
str.clear();
return FALSE;
}
str.resize(s);
BOOL ret = GetComputerNameExW(t, &str[0], &s);
if (ret)
str.resize(s);
else
str.clear();
return ret;
}
inline DWORD AutoBuff_GetCurrentDirectoryA(std::string& str) {
return AutoBuff_internal::call_with_retry(
str, [](LPSTR b, DWORD s) { return GetCurrentDirectoryA(s, b); });
}
inline DWORD AutoBuff_GetCurrentDirectoryW(std::wstring& str) {
return AutoBuff_internal::call_with_retry(
str, [](LPWSTR b, DWORD s) { return GetCurrentDirectoryW(s, b); });
}
inline DWORD AutoBuff_GetEnvironmentVariableA(LPCSTR n, std::string& str) {
return AutoBuff_internal::call_with_retry(
str, [&](LPSTR b, DWORD s) { return GetEnvironmentVariableA(n, b, s); });
}
inline DWORD AutoBuff_GetEnvironmentVariableW(LPCWSTR n, std::wstring& str) {
return AutoBuff_internal::call_with_retry(
str, [&](LPWSTR b, DWORD s) { return GetEnvironmentVariableW(n, b, s); });
}
inline DWORD AutoBuff_GetFullPathNameA(LPCSTR f, std::string& str,
LPSTR* p = nullptr) {
return AutoBuff_internal::call_with_retry(
str, [&](LPSTR b, DWORD s) { return GetFullPathNameA(f, s, b, p); });
}
inline DWORD AutoBuff_GetFullPathNameW(LPCWSTR f, std::wstring& str,
LPWSTR* p = nullptr) {
return AutoBuff_internal::call_with_retry(
str, [&](LPWSTR b, DWORD s) { return GetFullPathNameW(f, s, b, p); });
}
inline DWORD AutoBuff_GetLogicalDriveStringsA(std::string& str) {
return AutoBuff_internal::call_with_retry(
str, [](LPSTR b, DWORD s) { return GetLogicalDriveStringsA(s, b); });
}
inline DWORD AutoBuff_GetLogicalDriveStringsW(std::wstring& str) {
return AutoBuff_internal::call_with_retry(
str, [](LPWSTR b, DWORD s) { return GetLogicalDriveStringsW(s, b); });
}
inline DWORD AutoBuff_GetLongPathNameA(LPCSTR l, std::string& str) {
return AutoBuff_internal::call_with_retry(
str, [&](LPSTR b, DWORD s) { return GetLongPathNameA(l, b, s); });
}
inline DWORD AutoBuff_GetLongPathNameW(LPCWSTR l, std::wstring& str) {
return AutoBuff_internal::call_with_retry(
str, [&](LPWSTR b, DWORD s) { return GetLongPathNameW(l, b, s); });
}
inline DWORD AutoBuff_GetShortPathNameA(LPCSTR l, std::string& str) {
return AutoBuff_internal::call_with_retry(
str, [&](LPSTR b, DWORD s) { return GetShortPathNameA(l, b, s); });
}
inline DWORD AutoBuff_GetShortPathNameW(LPCWSTR l, std::wstring& str) {
return AutoBuff_internal::call_with_retry(
str, [&](LPWSTR b, DWORD s) { return GetShortPathNameW(l, b, s); });
}
inline DWORD AutoBuff_GetModuleFileNameA(HMODULE h, std::string& str) {
DWORD s = MAX_PATH;
while (true) {
str.resize(s);
DWORD r = GetModuleFileNameA(h, &str[0], s);
if (r < s) {
str.resize(r);
return r;
}
if (s > AUTOBUFF_MAX) {
str.clear();
return 0;
}
s *= 2;
}
}
inline DWORD AutoBuff_GetModuleFileNameW(HMODULE h, std::wstring& str) {
DWORD s = MAX_PATH;
while (true) {
str.resize(s);
DWORD r = GetModuleFileNameW(h, &str[0], s);
if (r < s) {
str.resize(r);
return r;
}
if (s > AUTOBUFF_MAX) {
str.clear();
return 0;
}
s *= 2;
}
}
inline DWORD AutoBuff_GetPrivateProfileStringA(LPCSTR a, LPCSTR k, LPCSTR d,
std::string& str, LPCSTR f) {
return AutoBuff_internal::call_with_expand(str, [&](LPSTR b, DWORD s) {
return GetPrivateProfileStringA(a, k, d, b, s, f);
});
}
inline DWORD AutoBuff_GetPrivateProfileStringW(LPCWSTR a, LPCWSTR k, LPCWSTR d,
std::wstring& str, LPCWSTR f) {
return AutoBuff_internal::call_with_expand(str, [&](LPWSTR b, DWORD s) {
return GetPrivateProfileStringW(a, k, d, b, s, f);
});
}
inline DWORD AutoBuff_GetPrivateProfileSectionA(LPCSTR a, std::string& str,
LPCSTR f) {
return AutoBuff_internal::call_with_expand(str, [&](LPSTR b, DWORD s) {
return GetPrivateProfileSectionA(a, b, s, f);
});
}
inline DWORD AutoBuff_GetPrivateProfileSectionW(LPCWSTR a, std::wstring& str,
LPCWSTR f) {
return AutoBuff_internal::call_with_expand(str, [&](LPWSTR b, DWORD s) {
return GetPrivateProfileSectionW(a, b, s, f);
});
}
inline DWORD AutoBuff_GetPrivateProfileSectionNamesA(std::string& str,
LPCSTR f) {
return AutoBuff_internal::call_with_expand(str, [&](LPSTR b, DWORD s) {
return GetPrivateProfileSectionNamesA(b, s, f);
});
}
inline DWORD AutoBuff_GetPrivateProfileSectionNamesW(std::wstring& str,
LPCWSTR f) {
return AutoBuff_internal::call_with_expand(str, [&](LPWSTR b, DWORD s) {
return GetPrivateProfileSectionNamesW(b, s, f);
});
}
inline UINT AutoBuff_GetSystemDirectoryA(std::string& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPSTR b, UINT s) { return GetSystemDirectoryA(b, s); });
}
inline UINT AutoBuff_GetSystemDirectoryW(std::wstring& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPWSTR b, UINT s) { return GetSystemDirectoryW(b, s); });
}
inline UINT AutoBuff_GetWindowsDirectoryA(std::string& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPSTR b, UINT s) { return GetWindowsDirectoryA(b, s); });
}
inline UINT AutoBuff_GetWindowsDirectoryW(std::wstring& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPWSTR b, UINT s) { return GetWindowsDirectoryW(b, s); });
}
inline UINT AutoBuff_GetSystemWindowsDirectoryA(std::string& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPSTR b, UINT s) { return GetSystemWindowsDirectoryA(b, s); });
}
inline UINT AutoBuff_GetSystemWindowsDirectoryW(std::wstring& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPWSTR b, UINT s) { return GetSystemWindowsDirectoryW(b, s); });
}
inline UINT AutoBuff_GetTempPathA(std::string& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPSTR b, UINT s) { return GetTempPathA(s, b); });
}
inline UINT AutoBuff_GetTempPathW(std::wstring& str) {
return (UINT)AutoBuff_internal::call_with_retry(
str, [](LPWSTR b, UINT s) { return GetTempPathW(s, b); });
}
inline BOOL AutoBuff_GetUserNameA(std::string& str) {
DWORD s = 0;
GetUserNameA(NULL, &s);
if (s == 0) return FALSE;
str.resize(s - 1);
return GetUserNameA(&str[0], &s);
}
inline BOOL AutoBuff_GetUserNameW(std::wstring& str) {
DWORD s = 0;
GetUserNameW(NULL, &s);
if (s == 0) return FALSE;
str.resize(s - 1);
return GetUserNameW(&str[0], &s);
}
inline DWORD AutoBuff_GetConsoleTitleA(std::string& str) {
return AutoBuff_internal::call_with_expand(
str, [](LPSTR b, DWORD s) { return GetConsoleTitleA(b, s); });
}
inline DWORD AutoBuff_GetConsoleTitleW(std::wstring& str) {
return AutoBuff_internal::call_with_expand(
str, [](LPWSTR b, DWORD s) { return GetConsoleTitleW(b, s); });
}
inline int AutoBuff_GetWindowTextA(HWND h, std::string& str) {
int l = GetWindowTextLengthA(h);
if (l <= 0) {
str.clear();
return 0;
}
str.resize(l);
int ret = GetWindowTextA(h, &str[0], l + 1);
str.resize(ret);
return ret;
}
inline int AutoBuff_GetWindowTextW(HWND h, std::wstring& str) {
int l = GetWindowTextLengthW(h);
if (l <= 0) {
str.clear();
return 0;
}
str.resize(l);
int ret = GetWindowTextW(h, &str[0], l + 1);
str.resize(ret);
return ret;
}
inline UINT AutoBuff_GetDlgItemTextA(HWND hDlg, int nIDDlgItem,
std::string& str) {
HWND hWndItem = GetDlgItem(hDlg, nIDDlgItem);
if (!hWndItem) return 0;
return AutoBuff_GetWindowTextA(hWndItem, str);
}
inline UINT AutoBuff_GetDlgItemTextW(HWND hDlg, int nIDDlgItem,
std::wstring& str) {
HWND hWndItem = GetDlgItem(hDlg, nIDDlgItem);
if (!hWndItem) return 0;
return AutoBuff_GetWindowTextW(hWndItem, str);
}
inline int AutoBuff_LoadStringW(HINSTANCE h, UINT i, std::wstring& str) {
LPCWSTR p = nullptr;
int l = LoadStringW(h, i, reinterpret_cast<LPWSTR>(&p), 0);
if (l > 0)
str.assign(p, l);
else
str.clear();
return l;
}
inline int AutoBuff_LoadStringA(HINSTANCE h, UINT i, std::string& str) {
std::wstring wstr;
int l = AutoBuff_LoadStringW(h, i, wstr);
if (l <= 0) {
str.clear();
return 0;
}
int needed =
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), l, NULL, 0, NULL, NULL);
if (needed <= 0) {
str.clear();
return 0;
}
str.resize(needed);
int written = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), l, &str[0], needed,
NULL, NULL);
str.resize(written);
return written;
}
inline int AutoBuff_GetClassNameA(HWND h, std::string& str) {
char b[256];
int r = GetClassNameA(h, b, 256);
if (r > 0) str = b;
return r;
}
inline int AutoBuff_GetClassNameW(HWND h, std::wstring& str) {
wchar_t b[256];
int r = GetClassNameW(h, b, 256);
if (r > 0) str = b;
return r;
}
inline int AutoBuff_GetDateFormatA(LCID l, DWORD f, const SYSTEMTIME* t,
LPCSTR m, std::string& str) {
int s = GetDateFormatA(l, f, t, m, NULL, 0);
if (s <= 0) {
str.clear();
return 0;
}
str.resize(s - 1);
return GetDateFormatA(l, f, t, m, &str[0], s);
}
inline int AutoBuff_GetDateFormatW(LCID l, DWORD f, const SYSTEMTIME* t,
LPCWSTR m, std::wstring& str) {
int s = GetDateFormatW(l, f, t, m, NULL, 0);
if (s <= 0) {
str.clear();
return 0;
}
str.resize(s - 1);
return GetDateFormatW(l, f, t, m, &str[0], s);
}
inline int AutoBuff_GetTimeFormatA(LCID l, DWORD f, const SYSTEMTIME* t,
LPCSTR m, std::string& str) {
int s = GetTimeFormatA(l, f, t, m, NULL, 0);
if (s <= 0) {
str.clear();
return 0;
}
str.resize(s - 1);
return GetTimeFormatA(l, f, t, m, &str[0], s);
}
inline int AutoBuff_GetTimeFormatW(LCID l, DWORD f, const SYSTEMTIME* t,
LPCWSTR m, std::wstring& str) {
int s = GetTimeFormatW(l, f, t, m, NULL, 0);
if (s <= 0) {
str.clear();
return 0;
}
str.resize(s - 1);
return GetTimeFormatW(l, f, t, m, &str[0], s);
}
inline int AutoBuff_GetNumberFormatA(LCID l, DWORD f, LPCSTR v,
const NUMBERFMTA* fmt, std::string& str) {
int s = GetNumberFormatA(l, f, v, fmt, NULL, 0);
if (s <= 0) {
str.clear();
return 0;
}
str.resize(s - 1);
return GetNumberFormatA(l, f, v, fmt, &str[0], s);
}
inline int AutoBuff_GetNumberFormatW(LCID l, DWORD f, LPCWSTR v,
const NUMBERFMTW* fmt, std::wstring& str) {
int s = GetNumberFormatW(l, f, v, fmt, NULL, 0);
if (s <= 0) {
str.clear();
return 0;
}
str.resize(s - 1);
return GetNumberFormatW(l, f, v, fmt, &str[0], s);
}
inline BOOL AutoBuff_GetFileVersionInfoA(LPCSTR f, std::vector<BYTE>& d) {
DWORD h;
DWORD s = GetFileVersionInfoSizeA(f, &h);
if (s == 0) return FALSE;
d.resize(s);
return GetFileVersionInfoA(f, h, s, d.data());
}
inline BOOL AutoBuff_GetFileVersionInfoW(LPCWSTR f, std::vector<BYTE>& d) {
DWORD h;
DWORD s = GetFileVersionInfoSizeW(f, &h);
if (s == 0) return FALSE;
d.resize(s);
return GetFileVersionInfoW(f, h, s, d.data());
}
inline std::string AutoBuff_wsprintfA(LPCSTR f, ...) {
char b[1024];
va_list a;
va_start(a, f);
wvsprintfA(b, f, a);
va_end(a);
return std::string(b);
}
inline std::wstring AutoBuff_wsprintfW(LPCWSTR f, ...) {
wchar_t b[1024];
va_list a;
va_start(a, f);
wvsprintfW(b, f, a);
va_end(a);
return std::wstring(b);
}
inline LSTATUS AutoBuff_RegQueryValueExA(HKEY hKey, LPCSTR lpValueName,
LPDWORD lpType,
std::vector<BYTE>& data) {
DWORD size = 0;
LSTATUS status = RegQueryValueExA(hKey, lpValueName, NULL, NULL, NULL, &size);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
data.resize(size);
status =
RegQueryValueExA(hKey, lpValueName, NULL, lpType, data.data(), &size);
if (status == ERROR_SUCCESS) data.resize(size);
}
return status;
}
inline LSTATUS AutoBuff_RegQueryValueExW(HKEY hKey, LPCWSTR lpValueName,
LPDWORD lpType,
std::vector<BYTE>& data) {
DWORD size = 0;
LSTATUS status = RegQueryValueExW(hKey, lpValueName, NULL, NULL, NULL, &size);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
data.resize(size);
status =
RegQueryValueExW(hKey, lpValueName, NULL, lpType, data.data(), &size);
if (status == ERROR_SUCCESS) data.resize(size);
}
return status;
}
inline LSTATUS AutoBuff_RegQueryStringA(HKEY hKey, LPCSTR lpValueName,
std::string& str) {
DWORD size = 0;
LSTATUS status = RegQueryValueExA(hKey, lpValueName, NULL, NULL, NULL, &size);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
str.resize(size > 0 ? size - 1 : 0);
status = RegQueryValueExA(hKey, lpValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(&str[0]), &size);
}
return status;
}
inline LSTATUS AutoBuff_RegQueryStringW(HKEY hKey, LPCWSTR lpValueName,
std::wstring& str) {
DWORD size = 0;
LSTATUS status = RegQueryValueExW(hKey, lpValueName, NULL, NULL, NULL, &size);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
str.resize(size / sizeof(wchar_t) - (size > 0 ? 1 : 0));
status = RegQueryValueExW(hKey, lpValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(&str[0]), &size);
}
return status;
}
inline LSTATUS AutoBuff_RegEnumValueA(HKEY hKey, DWORD dwIndex,
std::string& strValueName, LPDWORD lpType,
std::vector<BYTE>& data) {
DWORD nSize = 1, dSize = 0;
char dummy[1] = "";
LSTATUS status =
RegEnumValueA(hKey, dwIndex, dummy, &nSize, NULL, NULL, NULL, &dSize);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
strValueName.resize(nSize);
data.resize(dSize);
DWORD nSizeBuf = nSize + 1;
status = RegEnumValueA(hKey, dwIndex, &strValueName[0], &nSizeBuf, NULL,
lpType, data.data(), &dSize);
if (status == ERROR_SUCCESS) {
strValueName.resize(nSizeBuf);
data.resize(dSize);
}
}
return status;
}
inline LSTATUS AutoBuff_RegEnumValueW(HKEY hKey, DWORD dwIndex,
std::wstring& strValueName,
LPDWORD lpType, std::vector<BYTE>& data) {
DWORD nSize = 1, dSize = 0;
WCHAR dummy[1] = L"";
LSTATUS status =
RegEnumValueW(hKey, dwIndex, dummy, &nSize, NULL, NULL, NULL, &dSize);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
strValueName.resize(nSize);
data.resize(dSize);
DWORD nSizeBuf = nSize + 1;
status = RegEnumValueW(hKey, dwIndex, &strValueName[0], &nSizeBuf, NULL,
lpType, data.data(), &dSize);
if (status == ERROR_SUCCESS) {
strValueName.resize(nSizeBuf);
data.resize(dSize);
}
}
return status;
}
inline LSTATUS AutoBuff_RegQueryValueA(HKEY hKey, LPCSTR lpSubKey,
std::string& str) {
LONG size = 0;
LSTATUS status = RegQueryValueA(hKey, lpSubKey, NULL, &size);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
str.resize(size > 0 ? size - 1 : 0);
status = RegQueryValueA(hKey, lpSubKey, &str[0], &size);
}
return status;
}
inline LSTATUS AutoBuff_RegQueryValueW(HKEY hKey, LPCWSTR lpSubKey,
std::wstring& str) {
LONG size = 0;
LSTATUS status = RegQueryValueW(hKey, lpSubKey, NULL, &size);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
str.resize(size > 0 ? (size / sizeof(wchar_t)) - 1 : 0);
status = RegQueryValueW(hKey, lpSubKey, &str[0], &size);
}
return status;
}
inline LSTATUS AutoBuff_RegEnumKeyA(HKEY hKey, DWORD dwIndex,
std::string& strName) {
DWORD size = MAX_PATH;
while (true) {
strName.resize(size);
// cchName includes null terminator, so pass size+1
LSTATUS status = RegEnumKeyA(hKey, dwIndex, &strName[0], size + 1);
if (status == ERROR_SUCCESS) {
strName.resize(strlen(strName.c_str()));
return status;
}
if (status != ERROR_MORE_DATA) {
strName.clear();
return status;
}
size *= 2;
if (size > AUTOBUFF_MAX) {
strName.clear();
return status;
}
}
}
inline LSTATUS AutoBuff_RegEnumKeyW(HKEY hKey, DWORD dwIndex,
std::wstring& strName) {
DWORD size = MAX_PATH;
while (true) {
strName.resize(size);
LSTATUS status = RegEnumKeyW(hKey, dwIndex, &strName[0], size + 1);
if (status == ERROR_SUCCESS) {
strName.resize(wcslen(strName.c_str()));
return status;
}
if (status != ERROR_MORE_DATA) {
strName.clear();
return status;
}
size *= 2;
if (size > AUTOBUFF_MAX) {
strName.clear();
return status;
}
}
}
inline LSTATUS AutoBuff_RegEnumKeyExA(HKEY hKey, DWORD dwIndex,
std::string& strName,
std::string* pstrClass = nullptr,
PFILETIME lpft = nullptr) {
DWORD nSize = 1, cSize = 0;
char nDummy[1] = "", cDummy[1] = "";
LSTATUS status =
RegEnumKeyExA(hKey, dwIndex, nDummy, &nSize, NULL,
pstrClass ? cDummy : NULL, pstrClass ? &cSize : NULL, lpft);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
strName.resize(nSize);
DWORD nBuf = nSize + 1;
DWORD cBuf = cSize + 1;
if (pstrClass) pstrClass->resize(cSize);
status = RegEnumKeyExA(hKey, dwIndex, &strName[0], &nBuf, NULL,
pstrClass ? &(*pstrClass)[0] : NULL,
pstrClass ? &cBuf : NULL, lpft);
if (status == ERROR_SUCCESS) {
strName.resize(nBuf);
if (pstrClass) pstrClass->resize(cBuf);
} else {
strName.clear();
if (pstrClass) pstrClass->clear();
}
}
return status;
}
inline LSTATUS AutoBuff_RegEnumKeyExW(HKEY hKey, DWORD dwIndex,
std::wstring& strName,
std::wstring* pstrClass = nullptr,
PFILETIME lpft = nullptr) {
DWORD nSize = 1, cSize = 0;
WCHAR nDummy[1] = L"", cDummy[1] = L"";
LSTATUS status =
RegEnumKeyExW(hKey, dwIndex, nDummy, &nSize, NULL,
pstrClass ? cDummy : NULL, pstrClass ? &cSize : NULL, lpft);
if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {
strName.resize(nSize);
DWORD nBuf = nSize + 1;
DWORD cBuf = cSize + 1;
if (pstrClass) pstrClass->resize(cSize);
status = RegEnumKeyExW(hKey, dwIndex, &strName[0], &nBuf, NULL,
pstrClass ? &(*pstrClass)[0] : NULL,
pstrClass ? &cBuf : NULL, lpft);
if (status == ERROR_SUCCESS) {
strName.resize(nBuf);
if (pstrClass) pstrClass->resize(cBuf);
} else {
strName.clear();
if (pstrClass) pstrClass->clear();
}
}
return status;
}
#ifdef UNICODE
#define AutoBuff_GetAtomName AutoBuff_GetAtomNameW
#define AutoBuff_GetComputerName AutoBuff_GetComputerNameW
#define AutoBuff_GetComputerNameEx AutoBuff_GetComputerNameExW
#define AutoBuff_GetCurrentDirectory AutoBuff_GetCurrentDirectoryW
#define AutoBuff_GetEnvironmentVariable AutoBuff_GetEnvironmentVariableW
#define AutoBuff_GetFullPathName AutoBuff_GetFullPathNameW
#define AutoBuff_GetLogicalDriveStrings AutoBuff_GetLogicalDriveStringsW
#define AutoBuff_GetLongPathName AutoBuff_GetLongPathNameW
#define AutoBuff_GetShortPathName AutoBuff_GetShortPathNameW
#define AutoBuff_GetModuleFileName AutoBuff_GetModuleFileNameW
#define AutoBuff_GetPrivateProfileString AutoBuff_GetPrivateProfileStringW
#define AutoBuff_GetPrivateProfileSection AutoBuff_GetPrivateProfileSectionW
#define AutoBuff_GetPrivateProfileSectionNames \
AutoBuff_GetPrivateProfileSectionNamesW
#define AutoBuff_GetSystemDirectory AutoBuff_GetSystemDirectoryW
#define AutoBuff_GetWindowsDirectory AutoBuff_GetWindowsDirectoryW
#define AutoBuff_GetSystemWindowsDirectory AutoBuff_GetSystemWindowsDirectoryW
#define AutoBuff_GetTempPath AutoBuff_GetTempPathW
#define AutoBuff_GetUserName AutoBuff_GetUserNameW
#define AutoBuff_GetConsoleTitle AutoBuff_GetConsoleTitleW
#define AutoBuff_GetWindowText AutoBuff_GetWindowTextW
#define AutoBuff_GetDlgItemText AutoBuff_GetDlgItemTextW
#define AutoBuff_LoadString AutoBuff_LoadStringW
#define AutoBuff_GetClassName AutoBuff_GetClassNameW
#define AutoBuff_GetDateFormat AutoBuff_GetDateFormatW
#define AutoBuff_GetTimeFormat AutoBuff_GetTimeFormatW
#define AutoBuff_GetNumberFormat AutoBuff_GetNumberFormatW
#define AutoBuff_GetFileVersionInfo AutoBuff_GetFileVersionInfoW
#define AutoBuff_wsprintf AutoBuff_wsprintfW
#define AutoBuff_RegQueryValueEx AutoBuff_RegQueryValueExW
#define AutoBuff_RegQueryString AutoBuff_RegQueryStringW
#define AutoBuff_RegEnumValue AutoBuff_RegEnumValueW
#define AutoBuff_RegQueryValue AutoBuff_RegQueryValueW
#define AutoBuff_RegEnumKey AutoBuff_RegEnumKeyW
#define AutoBuff_RegEnumKeyEx AutoBuff_RegEnumKeyExW
#else
#define AutoBuff_GetAtomName AutoBuff_GetAtomNameA
#define AutoBuff_GetComputerName AutoBuff_GetComputerNameA
#define AutoBuff_GetComputerNameEx AutoBuff_GetComputerNameExA
#define AutoBuff_GetCurrentDirectory AutoBuff_GetCurrentDirectoryA
#define AutoBuff_GetEnvironmentVariable AutoBuff_GetEnvironmentVariableA
#define AutoBuff_GetFullPathName AutoBuff_GetFullPathNameA
#define AutoBuff_GetLogicalDriveStrings AutoBuff_GetLogicalDriveStringsA
#define AutoBuff_GetLongPathName AutoBuff_GetLongPathNameA
#define AutoBuff_GetShortPathName AutoBuff_GetShortPathNameA
#define AutoBuff_GetModuleFileName AutoBuff_GetModuleFileNameA
#define AutoBuff_GetPrivateProfileString AutoBuff_GetPrivateProfileStringA
#define AutoBuff_GetPrivateProfileSection AutoBuff_GetPrivateProfileSectionA
#define AutoBuff_GetPrivateProfileSectionNames \
AutoBuff_GetPrivateProfileSectionNamesA
#define AutoBuff_GetSystemDirectory AutoBuff_GetSystemDirectoryA
#define AutoBuff_GetWindowsDirectory AutoBuff_GetWindowsDirectoryA
#define AutoBuff_GetSystemWindowsDirectory AutoBuff_GetSystemWindowsDirectoryA
#define AutoBuff_GetTempPath AutoBuff_GetTempPathA
#define AutoBuff_GetUserName AutoBuff_GetUserNameA
#define AutoBuff_GetConsoleTitle AutoBuff_GetConsoleTitleA
#define AutoBuff_GetWindowText AutoBuff_GetWindowTextA
#define AutoBuff_GetDlgItemText AutoBuff_GetDlgItemTextA
#define AutoBuff_LoadString AutoBuff_LoadStringA
#define AutoBuff_GetClassName AutoBuff_GetClassNameA
#define AutoBuff_GetDateFormat AutoBuff_GetDateFormatA
#define AutoBuff_GetTimeFormat AutoBuff_GetTimeFormatA
#define AutoBuff_GetNumberFormat AutoBuff_GetNumberFormatA
#define AutoBuff_GetFileVersionInfo AutoBuff_GetFileVersionInfoA
#define AutoBuff_wsprintf AutoBuff_wsprintfA
#define AutoBuff_RegQueryValueEx AutoBuff_RegQueryValueExA
#define AutoBuff_RegQueryString AutoBuff_RegQueryStringA
#define AutoBuff_RegEnumValue AutoBuff_RegEnumValueA
#define AutoBuff_RegQueryValue AutoBuff_RegQueryValueA
#define AutoBuff_RegEnumKey AutoBuff_RegEnumKeyA
#define AutoBuff_RegEnumKeyEx AutoBuff_RegEnumKeyExA
#endif