-
Notifications
You must be signed in to change notification settings - Fork 0
/
newapisa.h
476 lines (383 loc) · 13.3 KB
/
newapisa.h
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
/*
New APIs A
Drop-in replacement for Windows SDK <include/newapis.h> header.
(c) 2021 Ai4rei/AN
Licensed under the Non-Profit Open Software License version 3.0
*/
/*
This implements a fallback mechanism for APIs that did not exist
on Windows 95 or Windows NT version prior 4.0 SP3.
Usage:
To enable a specific wrapper, define one or more following:
Function | Define
----------------------+-----------------------------------------
GetDiskFreeSpaceEx | WANT_GETDISKFREESPACEEX_WRAPPER
GetFileAttributesEx | WANT_GETFILEATTRIBUTESEX_WRAPPER
GetLongPathName | WANT_GETLONGPATHNAME_WRAPPER
IsDebuggerPresent | WANT_ISDEBUGGERPRESENT_WRAPPER
In addition one source file has to define all used defines above
and in addition COMPILE_NEWAPIS_STUBS.
All defines must be specified before including this file.
*/
#ifndef GITBITS_NEWAPIS_NEWAPISA_H
#define GITBITS_NEWAPIS_NEWAPISA_H
/* on Win64 this is not needed (newapis.h does not exist there in
the first place) */
#ifndef _WIN64
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
/*
GetDiskFreeSpaceEx
*/
#ifdef WANT_GETDISKFREESPACEEX_WRAPPER
#undef GetDiskFreeSpaceEx
#define GetDiskFreeSpaceEx _GetDiskFreeSpaceEx
typedef BOOL (WINAPI* LPFNGETDISKFREESPACEEX)
(
LPCTSTR lpszDirectory,
PULARGE_INTEGER lpuliFreeBytesAvailable,
PULARGE_INTEGER lpuliTotalNumberOfBytes,
PULARGE_INTEGER lpuliTotalNumberOfFreeBytes
);
extern LPFNGETDISKFREESPACEEX GetDiskFreeSpaceEx;
#ifdef COMPILE_NEWAPIS_STUBS
/* This kicks in, when running on a system before the introduction
of FAT32, where disk sizes are up to 2GB (FAT16). */
static BOOL WINAPI NewApisA_GetDiskFreeSpaceEx_Fallback
(
LPCTSTR lpszDirectory,
PULARGE_INTEGER lpuliFreeBytesAvailable,
PULARGE_INTEGER lpuliTotalNumberOfBytes,
PULARGE_INTEGER lpuliTotalNumberOfFreeBytes
)
{
DWORD dwSectorsPerCluster = 0, dwBytesPerSector = 0, dwNumberOfFreeClusters = 0, dwTotalNumberOfClusters = 0;
if(GetDiskFreeSpace(lpszDirectory, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters))
{
DWORD const dwBytesPerCluster = dwBytesPerSector*dwSectorsPerCluster;
if(lpuliFreeBytesAvailable!=NULL)
{
lpuliFreeBytesAvailable->QuadPart = UInt32x32To64(dwBytesPerCluster, dwNumberOfFreeClusters);
}
if(lpuliTotalNumberOfBytes!=NULL)
{
lpuliTotalNumberOfBytes->QuadPart = UInt32x32To64(dwBytesPerCluster, dwTotalNumberOfClusters);
}
if(lpuliTotalNumberOfFreeBytes!=NULL)
{
lpuliTotalNumberOfFreeBytes->QuadPart = UInt32x32To64(dwBytesPerCluster, dwNumberOfFreeClusters);
}
return TRUE;
}
return FALSE;
}
static BOOL WINAPI NewApisA_GetDiskFreeSpaceEx_Loader
(
LPCTSTR lpszDirectory,
PULARGE_INTEGER lpuliFreeBytesAvailable,
PULARGE_INTEGER lpuliTotalNumberOfBytes,
PULARGE_INTEGER lpuliTotalNumberOfFreeBytes
)
{
LPFNGETDISKFREESPACEEX const Func = (LPFNGETDISKFREESPACEEX)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
#ifndef UNICODE
"GetDiskFreeSpaceExA"
#else /* UNICODE */
"GetDiskFreeSpaceExW"
#endif /* UNICODE */
);
if(Func!=NULL)
{
BOOL const bResult = Func(lpszDirectory, lpuliFreeBytesAvailable, lpuliTotalNumberOfBytes, lpuliTotalNumberOfFreeBytes);
if(bResult || GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED)
{
GetDiskFreeSpaceEx = Func;
return bResult;
}
}
GetDiskFreeSpaceEx = &NewApisA_GetDiskFreeSpaceEx_Fallback;
return NewApisA_GetDiskFreeSpaceEx_Fallback(lpszDirectory, lpuliFreeBytesAvailable, lpuliTotalNumberOfBytes, lpuliTotalNumberOfFreeBytes);
}
LPFNGETDISKFREESPACEEX GetDiskFreeSpaceEx = &NewApisA_GetDiskFreeSpaceEx_Loader;
#endif /* COMPILE_NEWAPIS_STUBS */
#endif /* WANT_GETDISKFREESPACEEX_WRAPPER */
/*
GetFileAttributesEx
*/
#ifdef WANT_GETFILEATTRIBUTESEX_WRAPPER
/* In case the SDK is not even aware of this function. */
#ifndef GetFileAttributesEx
typedef enum _GET_FILEEX_INFO_LEVELS {
GetFileExInfoStandard,
GetFileExMaxInfoLevel
} GET_FILEEX_INFO_LEVELS;
typedef struct _WIN32_FILE_ATTRIBUTE_DATA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
} WIN32_FILE_ATTRIBUTE_DATA, *LPWIN32_FILE_ATTRIBUTE_DATA;
#endif /* GetFileAttributesEx */
#undef GetFileAttributesEx
#define GetFileAttributesEx _GetFileAttributesEx
typedef BOOL (WINAPI* LPFNGETFILEATTRIBUTESEX)
(
LPCTSTR lpszFileName,
GET_FILEEX_INFO_LEVELS nInfoLevelId,
LPVOID lpFileInformation
);
extern LPFNGETFILEATTRIBUTESEX GetFileAttributesEx;
#ifdef COMPILE_NEWAPIS_STUBS
static BOOL WINAPI NewApisA_GetFileAttributesEx_Fallback
(
LPCTSTR lpszFileName,
GET_FILEEX_INFO_LEVELS nInfoLevelId,
LPVOID lpFileInformation
)
{
if(nInfoLevelId==GetFileExInfoStandard)
{
if(GetFileAttributes(lpszFileName)!=INVALID_FILE_ATTRIBUTES)
{
WIN32_FIND_DATA Wfd;
HANDLE const hFind = FindFirstFile(lpszFileName, &Wfd);
if(hFind!=INVALID_HANDLE_VALUE)
{
LPWIN32_FILE_ATTRIBUTE_DATA const lpWfad = lpFileInformation;
FindClose(hFind);
lpWfad->dwFileAttributes = Wfd.dwFileAttributes;
lpWfad->ftCreationTime = Wfd.ftCreationTime;
lpWfad->ftLastAccessTime = Wfd.ftLastAccessTime;
lpWfad->ftLastWriteTime = Wfd.ftLastWriteTime;
lpWfad->nFileSizeHigh = Wfd.nFileSizeHigh;
lpWfad->nFileSizeLow = Wfd.nFileSizeLow;
return TRUE;
}
}
}
else
{
SetLastError(ERROR_INVALID_PARAMETER);
}
return FALSE;
}
static BOOL WINAPI NewApisA_GetFileAttributesEx_Loader
(
LPCTSTR lpszFileName,
GET_FILEEX_INFO_LEVELS nInfoLevelId,
LPVOID lpFileInformation
)
{
LPFNGETFILEATTRIBUTESEX const Func = (LPFNGETFILEATTRIBUTESEX)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
#ifndef UNICODE
"GetFileAttributesExA"
#else /* UNICODE */
"GetFileAttributesExW"
#endif /* UNICODE */
);
if(Func!=NULL)
{
BOOL const bResult = Func(lpszFileName, nInfoLevelId, lpFileInformation);
if(bResult || GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED)
{
GetFileAttributesEx = Func;
return bResult;
}
}
GetFileAttributesEx = &NewApisA_GetFileAttributesEx_Fallback;
return NewApisA_GetFileAttributesEx_Fallback(lpszFileName, nInfoLevelId, lpFileInformation);
}
LPFNGETFILEATTRIBUTESEX GetFileAttributesEx = &NewApisA_GetFileAttributesEx_Loader;
#endif /* COMPILE_NEWAPIS_STUBS */
#endif /* WANT_GETFILEATTRIBUTESEX_WRAPPER */
/*
GetLongPathName
*/
#ifdef WANT_GETLONGPATHNAME_WRAPPER
#undef GetLongPathName
#define GetLongPathName _GetLongPathName
typedef DWORD (WINAPI* LPFNGETLONGPATHNAME)
(
LPCTSTR lpszShortPath,
LPTSTR lpszLongPath,
DWORD cchBuffer
);
extern LPFNGETLONGPATHNAME GetLongPathName;
#ifdef COMPILE_NEWAPIS_STUBS
static LPTSTR WINAPI NewApisA_GetLongPathName_StrRChr(LPTSTR const lpszString, TCHAR const chNeedle)
{
LPTSTR lpszMatch = NULL;
LPTSTR lpszIdx;
for(lpszIdx = lpszString; lpszIdx[0]!=TEXT('\0'); lpszIdx = CharNext(lpszIdx))
{
if(lpszIdx[0]==chNeedle)
{
lpszMatch = lpszIdx;
}
}
return lpszMatch;
}
static BOOL WINAPI NewApisA_GetLongPathName_GetLongR(LPTSTR const lpszLongPath, DWORD const dwLongPathSize, LPDWORD const lpdwLength)
{
BOOL bSuccess = FALSE;
DWORD const dwAttributes = GetFileAttributes(lpszLongPath);
DWORD dwLength = 0;
/* must be actual path */
if(dwAttributes!=INVALID_FILE_ATTRIBUTES)
{
if(!(dwAttributes&FILE_ATTRIBUTE_DIRECTORY) /* file */
|| ( lstrcmp(lpszLongPath, TEXT("."))!=0 && lstrcmp(lpszLongPath, TEXT(".."))!=0 && !( IsCharAlpha(lpszLongPath[0]) && lpszLongPath[1]==TEXT(':') && lpszLongPath[2]==TEXT('\0') ) )) /* real directory */
{
WIN32_FIND_DATA Wfd = { 0 };
HANDLE const hFind = FindFirstFile(lpszLongPath, &Wfd);
if(hFind!=INVALID_HANDLE_VALUE)
{
FindClose(hFind);
if(lstrcmp(Wfd.cFileName, TEXT("."))!=0)
{
LPTSTR const lpszSlash = NewApisA_GetLongPathName_StrRChr(lpszLongPath, TEXT('\\'));
if(lpszSlash!=NULL)
{/* parent */
lpszSlash[0] = TEXT('\0');
if(NewApisA_GetLongPathName_GetLongR(lpszLongPath, dwLongPathSize, &dwLength))
{
DWORD const dwNewLength = dwLength+lstrlen(Wfd.cFileName)+1;
if(dwNewLength<dwLongPathSize)
{/* update path with long file name */
wsprintf(&lpszLongPath[dwLength], TEXT("\\%s"), Wfd.cFileName);
}
dwLength = dwNewLength;
bSuccess = TRUE;
}
}
else
{/* relative path */
dwLength+= lstrlen(lpszLongPath);
if(dwLength<dwLongPathSize)
{
lstrcpy(lpszLongPath, Wfd.cFileName);
}
bSuccess = TRUE;
}
}
else
{/* network share root */
dwLength+= lstrlen(lpszLongPath);
bSuccess = TRUE;
}
}
}
else
{/* other things that do not have long names */
dwLength+= lstrlen(lpszLongPath);
bSuccess = TRUE;
}
}
else
{/* non-directory path portion */
dwLength+= lstrlen(lpszLongPath);
bSuccess = TRUE;
}
lpdwLength[0]+= dwLength;
return bSuccess;
}
/* Unlike the official fallback, this works more closely to the
actual function in regards to failures and handling of relative
paths. It also does not depend upon IE nor COM to be installed. */
static DWORD WINAPI NewApisA_GetLongPathName_Fallback
(
LPCTSTR lpszShortPath,
LPTSTR lpszLongPath,
DWORD dwLongPathSize
)
{
DWORD dwLength = 0;
/* path must exist, otherwise no way to tell the long name,
because there is not any */
if(GetFileAttributes(lpszShortPath)!=INVALID_FILE_ATTRIBUTES)
{
DWORD const dwShortLength = lstrlen(lpszShortPath);
if(dwShortLength<dwLongPathSize)
{
/* pre-load destination buffer with short path */
lstrcpy(lpszLongPath, lpszShortPath);
if(!NewApisA_GetLongPathName_GetLongR(lpszLongPath, dwLongPathSize, &dwLength))
{/* if cannot be resolved, default to short path */
dwLength = dwShortLength;
lstrcpy(lpszLongPath, lpszShortPath);
}
}
else
{
dwLength = dwShortLength;
SetLastError(ERROR_BUFFER_OVERFLOW);
}
}
return dwLength;
}
static DWORD WINAPI NewApisA_GetLongPathName_Loader
(
LPCTSTR lpszShortPath,
LPTSTR lpszLongPath,
DWORD cchBuffer
)
{
LPFNGETLONGPATHNAME const Func = (LPFNGETLONGPATHNAME)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
#ifndef UNICODE
"GetLongPathNameA"
#else /* UNICODE */
"GetLongPathNameW"
#endif /* UNICODE */
);
if(Func!=NULL)
{
BOOL const bResult = Func(lpszShortPath, lpszLongPath, cchBuffer);
if(bResult || GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED)
{
GetLongPathName = Func;
return bResult;
}
}
GetLongPathName = &NewApisA_GetLongPathName_Fallback;
return NewApisA_GetLongPathName_Fallback(lpszShortPath, lpszLongPath, cchBuffer);
}
LPFNGETLONGPATHNAME GetLongPathName = &NewApisA_GetLongPathName_Loader;
#endif /* COMPILE_NEWAPIS_STUBS */
#endif /* WANT_GETLONGPATHNAME_WRAPPER */
/*
IsDebuggerPresent
*/
#ifdef WANT_ISDEBUGGERPRESENT_WRAPPER
#define IsDebuggerPresent _IsDebuggerPresent
typedef BOOL (WINAPI* LPFNISDEBUGGERPRESENT)(VOID);
extern LPFNISDEBUGGERPRESENT IsDebuggerPresent;
#ifdef COMPILE_NEWAPIS_STUBS
static BOOL WINAPI NewApisA_IsDebuggerPresent_Fallback(VOID)
{
return FALSE; /* no way to tell, assume no */
}
static BOOL WINAPI NewApisA_IsDebuggerPresent_Loader(VOID)
{
LPFNISDEBUGGERPRESENT const Func = (LPFNISDEBUGGERPRESENT)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "IsDebuggerPresent");
if(Func!=NULL)
{
IsDebuggerPresent = Func;
}
else
{
IsDebuggerPresent = &NewApisA_IsDebuggerPresent_Fallback;
}
return IsDebuggerPresent();
}
LPFNISDEBUGGERPRESENT IsDebuggerPresent = &NewApisA_IsDebuggerPresent_Loader;
#endif /* COMPILE_NEWAPIS_STUBS */
#endif /* WANT_ISDEBUGGERPRESENT_WRAPPER */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _WIN64 */
#endif /* GITBITS_NEWAPIS_NEWAPISA_H */