-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwinstrct.h
More file actions
896 lines (768 loc) · 21.2 KB
/
Copy pathwinstrct.h
File metadata and controls
896 lines (768 loc) · 21.2 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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/* winstrct.h
*
* Defines Windows "Unix-like" error number and error message variables and
* functions.
* C++: Encapsulates in C++ objects some Windows API get/set function pairs.
* Written by Olof Lagerkvist 2000-2005.
*/
#ifndef _INC_WINSTRCT_
#define _INC_WINSTRCT_
#ifndef _INC_WINDOWS_
# include <windows.h>
#endif
#ifndef _USERENTRY
# define _USERENTRY
#endif
// The inlines here uses the stream I/O library functions.
#ifndef _INC_STDIO
# include <stdio.h>
#endif
#include <tchar.h>
#ifndef EXTERN_C
# ifdef __cplusplus
# define EXTERN_C extern "C"
# else
# define EXTERN_C
# endif
#endif
#define WINSOCK_MODULE TEXT("WSOCK32")
#define PDH_MODULE TEXT("PDH")
#define NTDLL_MODULE TEXT("NTDLL")
#ifdef _MSC_VER
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#ifndef NO_WINSTRCT_LIB_IMPORT
#pragma comment(lib, "winstrcp.lib")
#pragma comment(lib, "winstrct.lib")
#endif
#endif
// import/export macros
#define IMPORT __declspec(dllimport)
#define EXPORT __declspec(dllexport)
#ifdef __DLL__
# define DLL_EXTERN EXPORT
# define EXE_EXTERN IMPORT
#else
# define DLL_EXTERN IMPORT
# define EXE_EXTERN EXPORT
#endif
#if __BCPLUSPLUS__ > 0x0520
#pragma option push -vi
#endif
#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
#define _UNALIGNED __unaligned
#else
#define _UNALIGNED
#endif
/* _countof helper */
#if !defined(_countof)
#if !defined(__cplusplus)
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#else
extern "C++"
{
template <typename _CountofType, size_t _SizeOfArray>
char(*__countof_helper(_UNALIGNED _CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
}
#endif
#endif
#ifndef INVALID_HKEY
#define INVALID_HKEY ((HKEY)INVALID_HANDLE_VALUE)
#endif
#ifndef WORD_MIN
#define WORD_MIN ((WORD)0x80000000)
#endif
#ifndef WORD_MAX
#define WORD_MAX ((WORD)0x7FFFFFFF)
#endif
#ifndef DWORD_MAX
#define DWORD_MAX ((DWORD)0xFFFFFFFF)
#endif
#ifndef LONGLONG_MIN
#define LONGLONG_MIN ((DWORDLONG)0x8000000000000000)
#endif
#ifndef LONGLONG_MAX
#define LONGLONG_MAX ((DWORDLONG)0x7FFFFFFFFFFFFFFF)
#endif
#ifndef DWORDLONG_MAX
#define DWORDLONG_MAX ((DWORDLONG)0xFFFFFFFFFFFFFFFF)
#endif
// Windows version macros
#define WinVer_Major (GetVersion()&0x000000FF)
#define WinVer_Minor ((GetVersion()&0x0000FF00)>>8)
#define WinVer_Build ((GetVersion()&0x7FFF0000)>>16)
// Windows platform macros (boolean return values)
#define WinVer_WindowsNT (!(GetVersion()&0x80000000))
#define WinVer_Windows95 (!!((GetVersion()&0x800000FF) >= 0x80000004))
#define WinVer_Win32s (!!(((GetVersion()^0x80000000)&0x800000FF) < 4))
#define WinVer_Win95orNT4 (WinVer_Major >= 4)
#define WinVer_Win32sor95 (!!(GetVersion()&0x80000000))
#define WinVer_Win32sorNT3 (WinVer_Major < 4)
#define WinVer_Win2korXP (WinVer_Major >= 5)
typedef struct _REPARSE_DATA
{
DWORD ReparseTag;
WORD ReparseDataLength;
WORD Reserved;
union
{
struct _REPARSE_DATA_MOUNT_POINT
{
WORD NameOffset;
WORD NameLength;
WORD DisplayNameOffset;
WORD DisplayNameLength;
BYTE Data[1];
} MountPoint;
struct _REPARSE_DATA_SYMLINK
{
WORD NameOffset;
WORD NameLength;
WORD DisplayNameOffset;
WORD DisplayNameLength;
DWORD Flags;
BYTE Data[1];
} SymLink;
};
} REPARSE_DATA, *PREPARSE_DATA;
#define SYMLINK_FLAG_RELATIVE 0x00000001
/// Macros for "human readable" sizes in bytes
#define _1KB (1ui64<<10)
#define _1MB (1ui64<<20)
#define _1GB (1ui64<<30)
#define _1TB (1ui64<<40)
#define TO_B(n) ((double)(n))
#define TO_KB(n) ((double)(n)/_1KB)
#define TO_MB(n) ((double)(n)/_1MB)
#define TO_GB(n) ((double)(n)/_1GB)
#define TO_TB(n) ((double)(n)/_1TB)
#define TO_h(n) ((n)>=_1TB ? TO_TB(n) : (n)>=_1GB ? TO_GB(n) : \
(n)>=_1MB ? TO_MB(n) : (n)>=_1KB ? TO_KB(n) : (n))
#define TO_pA(n) ((n)>=_1TB ? "TB" : (n)>=_1GB ? "GB" : \
(n)>=_1MB ? "MB" : (n)>=_1KB ? "KB": (n)==1 ? "byte" : "bytes")
#define TO_pW(n) ((n)>=_1TB ? L"TB" : (n)>=_1GB ? L"GB" : \
(n)>=_1MB ? L"MB" : (n)>=_1KB ? L"KB": (n)==1 ? L"byte" : L"bytes")
#ifdef UNICODE
#define TO_p(n) TO_pW(n)
#else
#define TO_p(n) TO_pA(n)
#endif
/// Prototype for rundll32.exe callable functions.
typedef void WINAPI
RunDLLFunc(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
#define halloc_seh(n) HeapAlloc(GetProcessHeap(),HEAP_GENERATE_EXCEPTIONS,(n))
#define halloc(n) HeapAlloc(GetProcessHeap(), 0, (n))
#define hrealloc_seh(p, n) ((p) != NULL ? \
HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (p), (n)) : \
HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (n)))
#define hrealloc(p, n) ((p) != NULL ? \
HeapReAlloc(GetProcessHeap(), 0, (p), (n)) : \
HeapAlloc(GetProcessHeap(), 0, (n)))
#define hsize(p) HeapSize(GetProcessHeap(), 0, (p))
#define hfree(p) HeapFree(GetProcessHeap(), 0, (p))
#define REPLACE_FIELD(field, freefunc, newvalue) \
{ if ((field) != NULL) { freefunc(field); } (field) = (newvalue); }
#define FREE_FIELD(field, freefunc) \
REPLACE_FIELD(field, freefunc, NULL)
#if defined(__cplusplus_cli) || !defined(_M_IX86)
#define htonll(n) _byteswap_ulong(n)
#else
__inline long long __declspec(naked)
htonll()
{
__asm
{
mov eax, [esp + 4];
mov edx, [esp + 8];
bswap eax;
bswap edx;
xchg eax, edx;
ret;
}
}
#endif
#if defined(_INC_WCHAR) && defined(_CRT_FUNCTIONS_REQUIRED) && _MSC_VER >= 1400
// Returns pointer to first match of search_string in memory_block, or NULL if not found
__forceinline LPCWSTR
FindWStringInMemory(LPCVOID memory_block, SIZE_T byte_size, LPCWSTR search_string)
{
LPCWSTR block = (LPCWSTR)memory_block;
SIZE_T block_length = byte_size / sizeof(wchar_t);
SIZE_T search_length = wcslen(search_string); // length of search string in wchar_t
SIZE_T i = 0;
if (search_length == 0 || search_length > block_length)
return NULL;
for (i = 0; i <= block_length - search_length; ++i)
{
if (wmemcmp(&block[i], search_string, search_length) == 0)
{
return &block[i]; // found
}
}
return NULL; // not found
}
#endif // _INC_WCHAR
#if (_MSC_VER >= 1400) && (defined(_M_IX86) || defined(_M_AMD64))
EXTERN_C void __cpuid(int a[4], int b);
#pragma intrinsic(__cpuid, memcmp, memcpy, memset, strcat, strcmp, strcpy, strlen)
#define MICROSOFT_HV_CPUID "Microsoft Hv"
__forceinline BOOL
IsHyperV()
{
struct
{
int highest_leaf;
char vendor[12];
} cpuinfo = { 0 };
__cpuid((int*)&cpuinfo, 0x40000000);
return memcmp(cpuinfo.vendor, MICROSOFT_HV_CPUID, sizeof(cpuinfo.vendor)) == 0;
}
#else
#pragma intrinsic(memcmp, memcpy, memset, strcat, strcmp, strcpy, strlen)
#if (_MSC_VER >= 1400) && defined(_INC_WCHAR) && defined(_CRT_FUNCTIONS_REQUIRED) && defined(RegGetValue)
__forceinline BOOL
IsHyperV()
{
wchar_t buffer[512] = L"";
DWORD bufferSize;
// Common Hyper-V clues
return
((bufferSize = sizeof(buffer)), RegGetValue(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System", L"SystemBiosVersion", RRF_RT_ANY, NULL, buffer, &bufferSize) == NO_ERROR && FindWStringInMemory(buffer, bufferSize, L"Hyper-V") != NULL) ||
((bufferSize = sizeof(buffer)), RegGetValue(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System", L"VideoBiosVersion", RRF_RT_ANY, NULL, buffer, &bufferSize) == NO_ERROR && FindWStringInMemory(buffer, bufferSize, L"Hyper-V") != NULL) ||
((bufferSize = sizeof(buffer)), RegGetValue(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System", L"SystemManufacturer", RRF_RT_ANY, NULL, buffer, &bufferSize) == NO_ERROR && FindWStringInMemory(buffer, bufferSize, L"Microsoft") != NULL) ||
((bufferSize = sizeof(buffer)), RegGetValue(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System", L"SystemProductName", RRF_RT_ANY, NULL, buffer, &bufferSize) == NO_ERROR && FindWStringInMemory(buffer, bufferSize, L"Virtual") != NULL) ||
((bufferSize = sizeof(buffer)), RegGetValue(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System", L"SystemProductName", RRF_RT_ANY, NULL, buffer, &bufferSize) == NO_ERROR && FindWStringInMemory(buffer, bufferSize, L"Hyper-V") != NULL);
}
#endif
#endif
__forceinline size_t
StringLength(LPCWSTR string, size_t maxlen)
{
size_t i = 0;
while (i < maxlen && string[i] != 0)
{
++i;
}
return i;
}
// Complement to the lstr*** API functions
__forceinline int
lstrncmpi(LPCTSTR lpString1, LPCTSTR lpString2, int iMaxLength)
{
int iLength1 = lstrlen(lpString1);
int iLength2 = lstrlen(lpString2);
int iCompare = CompareString(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE |
SORT_STRINGSORT, lpString1,
iLength1<iMaxLength ? iLength1 : iMaxLength,
lpString2,
iLength2<iMaxLength ? iLength2 : iMaxLength);
return iCompare - 2;
}
/// <summary>
/// Sets a bit to 1 in a bit field.
/// </summary>
/// <param name="data">Bit field</param>
/// <param name="bitnumber">Bit number to set to 1</param>
__forceinline void
SetBit(PBYTE data, int bitnumber)
{
data[bitnumber >> 3] |= 1 << ((~bitnumber) & 7);
}
/// <summary>
/// Sets a bit to 0 in a bit field.
/// </summary>
/// <param name="data">Bit field</param>
/// <param name="bitnumber">Bit number to set to 0</param>
__forceinline void
ClearBit(PBYTE data, int bitnumber)
{
data[bitnumber >> 3] &= ~(1 << ((~bitnumber) & 7));
}
/// <summary>
/// Gets a bit from a bit field.
/// </summary>
/// <param name="data">Bit field</param>
/// <param name="bitnumber">Bit number to get/param>
/// <returns>True if value of specified bit is 1, false if 0.</returns>
__forceinline BOOL
GetBit(PBYTE data, int bitnumber)
{
return (data[bitnumber >> 3] & (1 << ((~bitnumber) & 7))) != 0;
}
/// <summary>
/// Finds greatest common divisor for two values.
/// </summary>
/// <param name="a">First value</param>
/// <param name="b">Second value</param>
/// <returns>Greatest common divisor for values a and b.</returns>
__forceinline __int64
GreatestCommonDivisor(__int64 a, __int64 b)
{
while (b != 0)
{
__int64 temp = b;
b = a % b;
a = temp;
}
return a;
}
/// <summary>
/// Finds greatest common divisor for a sequence of values.
/// </summary>
/// <param name="values">Sequence of values</param>
/// <returns>Greatest common divisor for values.</returns>
__forceinline __int64
GreatestCommonDivisorFromSequence(PLONGLONG values, int count)
{
__int64 result;
int i;
if (count <= 0)
{
return 0;
}
if (count == 1)
{
return values[0];
}
result = values[0];
for (i = 1; i < count; i++)
{
result = GreatestCommonDivisor(result, values[i]);
}
return result;
}
/// <summary>
/// Finds least common multiple for two values.
/// </summary>
/// <param name="a">First value</param>
/// <param name="b">Second value</param>
/// <returns>Least common multiple for values a and b.</returns>
__forceinline __int64
LeastCommonMultiple(__int64 a, __int64 b)
{
return (a / GreatestCommonDivisor(a, b)) * b;
}
/// <summary>
/// Finds least common multiple for a sequence of values.
/// </summary>
/// <param name="values">Sequence of values</param>
/// <returns>Least common multiple for values.</returns>
__forceinline __int64
LeastCommonMultipleFromSequence(PLONGLONG values, int count)
{
__int64 result;
int i;
if (count <= 0)
{
return 0;
}
if (count == 1)
{
return values[0];
}
result = values[0];
for (i = 1; i < count; i++)
{
result = LeastCommonMultiple(result, values[i]);
}
return result;
}
/// <summary>
/// Returns a sequence of prime factors for a value.
/// </summary>
/// <param name="value">Value</param>
/// <returns>Sequence of prime factors</returns>
__forceinline void
PrimeFactors(__int64 value, void (*action)(__int64 prime))
{
__int64 z = 2;
while (z * z <= value)
{
if (value % z == 0)
{
action(z);
value /= z;
}
else
{
z++;
}
}
if (value > 1)
{
action(value);
}
}
#ifdef __cplusplus
extern "C" {
#endif
extern
int
oem_printf_line_length;
__forceinline
void
SetOemPrintFLineLength(HANDLE hConsole)
{
CONSOLE_SCREEN_BUFFER_INFO con_info;
if (GetConsoleScreenBufferInfo(hConsole, &con_info))
{
oem_printf_line_length =
con_info.srWindow.Right - con_info.srWindow.Left - 2;
if (oem_printf_line_length > 250)
{
oem_printf_line_length = 250;
}
}
else
{
oem_printf_line_length = 0;
}
}
#ifdef __cplusplus
}
#endif
#ifdef _M_ARM
#ifdef CharToOemA
#undef CharToOemA
#endif
#define CharToOemA(s,t)
#ifdef MessageBoxA
#undef MessageBoxA
#endif
#ifdef MessageBoxW
#undef MessageBoxW
#endif
#define MessageBoxA ConsoleMessageA
#define MessageBoxW ConsoleMessageW
#endif
EXTERN_C
HMODULE
WINAPI
LoadRemoteLibrary(
HANDLE process,
LPCWSTR dllpath);
EXTERN_C
int
WINAPI
ConsoleMessageA(
HWND hWnd,
LPCSTR lpText,
LPCSTR lpCaption,
UINT uType);
EXTERN_C
int
WINAPI
ConsoleMessageW(
HWND hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT uType);
EXTERN_C
BOOL
oem_printf(FILE *stream, LPCSTR lpMessage, ...);
/* win_error
*/
__forceinline LPSTR
win_errmsgA(DWORD dwErrNo)
{
LPSTR errmsg = NULL;
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwErrNo,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&errmsg, 0, NULL))
{
return errmsg;
}
else if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, "Error %1!#x!%n", 0, 0,
(LPSTR)&errmsg, 0, (va_list*)&dwErrNo))
{
return errmsg;
}
else
{
return NULL;
}
}
__forceinline LPWSTR
win_errmsgW(DWORD dwErrNo)
{
LPWSTR errmsg = NULL;
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwErrNo,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errmsg, 0, NULL))
{
return errmsg;
}
else if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, L"Error %1!#x!%n", 0,
0, (LPWSTR)&errmsg, 0, (va_list*)&dwErrNo))
{
return errmsg;
}
else
{
return NULL;
}
}
#ifdef UNICODE
#define win_errmsg win_errmsgW
#define win_error win_errmsgW(GetLastError())
#define h_error win_errmsgW(WSAGetLastError())
#else
#define win_errmsg win_errmsgA
#define win_error win_errmsgA(GetLastError())
#define h_error win_errmsgA(WSAGetLastError())
#endif
#define win_errorA win_errmsgA(GetLastError())
#define win_errorW win_errmsgW(GetLastError())
#define h_errorA win_errmsgA(WSAGetLastError())
#define h_errorW win_errmsgW(WSAGetLastError())
/* pdh_error
*/
__forceinline LPSTR pdh_errmsgA(DWORD dwErrorCode)
{
LPSTR errmsg = NULL;
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetModuleHandle(PDH_MODULE), dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&errmsg, 0, NULL))
{
return errmsg;
}
else if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, "Error %1!#x!%n", 0, 0,
(LPSTR)&errmsg, 0, (va_list*)&dwErrorCode))
{
return errmsg;
}
else
{
return NULL;
}
}
__forceinline LPWSTR pdh_errmsgW(DWORD dwErrorCode)
{
LPWSTR errmsg = NULL;
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetModuleHandle(PDH_MODULE), dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errmsg, 0, NULL))
{
return errmsg;
}
else if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, L"Error 0x%1!x!%n", 0,
0, (LPWSTR)&errmsg, 0, (va_list*)&dwErrorCode))
{
return errmsg;
}
else
{
return NULL;
}
}
#ifdef UNICODE
#define pdh_errmsg pdh_errmsgW
#define pdh_error pdh_errmsgW(GetLastError())
#else
#define pdh_errmsg pdh_errmsgA
#define pdh_error pdh_errmsgA(GetLastError())
#endif
#define pdh_errorA pdh_errmsgA(GetLastError())
#define pdh_errorW pdh_errmsgW(GetLastError())
#ifdef _NTDDK_
/* nt_error
*/
__forceinline LPSTR nt_errmsgA(NTSTATUS dwErrorCode)
{
LPSTR errmsg = NULL;
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetModuleHandle(NTDLL_MODULE), dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&errmsg, 0, NULL))
{
return errmsg;
}
else if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, "Error %1!#x!%n", 0, 0,
(LPSTR)&errmsg, 0, (va_list*)&dwErrorCode))
{
return errmsg;
}
else
{
return NULL;
}
}
__forceinline LPWSTR nt_errmsgW(NTSTATUS dwErrorCode)
{
LPWSTR errmsg = NULL;
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetModuleHandle(NTDLL_MODULE), dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errmsg, 0, NULL))
{
return errmsg;
}
else if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, L"Error 0x%1!x!%n", 0,
0, (LPWSTR)&errmsg, 0, (va_list*)&dwErrorCode))
{
return errmsg;
}
else
{
return NULL;
}
}
#ifdef UNICODE
#define nt_errmsg nt_errmsgW
#else
#define nt_errmsg nt_errmsgA
#endif
#endif // _NTDDK_
// Use Borland compiler's internal functions
#ifdef __BORLANDC__
# define lstrcmpA __strcmp__
# define lstrcmpW __wcscmp__
# define lstrcpyA __strcpy__
# define lstrcpyW __wcscpy__
#endif
EXTERN_C LPSTR
CDECL
mprintfA(LPCSTR lpMsg, ...);
EXTERN_C LPWSTR
CDECL
mprintfW(LPCWSTR lpMsg, ...);
#ifdef UNICODE
#define mprintf mprintfW
#else
#define mprintf mprintfA
#endif
EXTERN_C LPSTR
WideToOemAlloc(LPCWSTR lpSrc);
EXTERN_C LPSTR
WideToByteAlloc(LPCWSTR lpSrc);
EXTERN_C LPWSTR
OemToWideAlloc(LPCSTR lpSrc);
EXTERN_C LPWSTR
ByteToWideAlloc(LPCSTR lpSrc);
__forceinline int
wcscnt(LPCWSTR lp, WCHAR c)
{
int o = 0;
while ((lp = wcschr(lp, c)) != NULL)
{
lp++;
o++;
}
return o;
}
// This function enables backup and restore privileges for this process.
__forceinline
BOOL
EnableBackupPrivileges()
{
LPCTSTR privileges[] = {
SE_BACKUP_NAME,
SE_RESTORE_NAME,
SE_SECURITY_NAME,
SE_MANAGE_VOLUME_NAME,
SE_DEBUG_NAME
};
BYTE buf[FIELD_OFFSET(TOKEN_PRIVILEGES, Privileges) +
_countof(privileges) * sizeof(LUID_AND_ATTRIBUTES)] = { 0 };
PTOKEN_PRIVILEGES tkp = (PTOKEN_PRIVILEGES)buf;
HANDLE hToken;
BOOL result;
int i;
for (i = 0; i < _countof(privileges); i++)
{
if (!LookupPrivilegeValue(NULL, privileges[i], &tkp->Privileges[i].Luid))
{
return FALSE;
}
tkp->Privileges[i].Attributes = SE_PRIVILEGE_ENABLED;
}
tkp->PrivilegeCount = _countof(privileges);
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
result = AdjustTokenPrivileges(hToken, FALSE, tkp, sizeof buf, NULL, NULL);
CloseHandle(hToken);
return result;
}
// Win32 version of kbhit()
#define lkbhit() (WaitForSingleObject(hStdIn,0)==WAIT_OBJECT_0?1:0)
#ifdef __cplusplus
#include <winstrct.hpp>
#else /*_cplusplus*/
// Do the best possible if not C++
/* win_perror()
*
* Used similar to perror() in Unix environments.
*
* Used to print to stderr the error message associated with the error code of
* the most recently non-sucessful call to a Windows API function. The error
* message buffer is only used internally and is automatically freed by this
* function.
*/
void win_perrorA(LPCSTR __errmsg);
void win_perrorW(LPCWSTR __errmsg);
#ifdef UNICODE
#define win_perror win_perrorW
#else
#define win_perror win_perrorA
#endif
/* h_perror()
*
*/
void h_perrorA(LPCSTR __errmsg);
void h_perrorW(LPCWSTR __errmsg);
#ifdef UNICODE
#define h_perror h_perrorW
#else
#define h_perror h_perrorA
#endif
/* pdh_perror()
*
*/
void pdh_perrorA(LPCSTR __errmsg);
void pdh_perrorW(LPCWSTR __errmsg);
#ifdef UNICODE
#define pdh_perror pdh_perrorW
#else
#define pdh_perror pdh_perrorA
#endif
#ifdef _NTDDK_
/* nt_perror()
*
*/
void nt_perrorA(NTSTATUS status, LPCSTR __errmsg);
void nt_perrorW(NTSTATUS status, LPCWSTR __errmsg);
#ifdef UNICODE
#define nt_perror nt_perrorW
#else
#define nt_perror nt_perrorA
#endif
#endif // _NTDDK_
#endif /*_cplusplus*/
VOID WINAPI s_perror(LPCSTR szMsg, int iErrNo);
#endif // !_INC_WINSTRCT_