VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/vcc-fakes-kernel32.cpp@ 103795

Last change on this file since 103795 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.7 KB
Line 
1/* $Id: vcc-fakes-kernel32.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Tricks to make the Visual C++ 2010 CRT work on NT4, W2K and XP.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/cdefs.h>
42#include <iprt/types.h>
43#include <iprt/asm.h>
44#include <iprt/assert.h>
45#include <iprt/string.h>
46#ifdef DEBUG
47# include <stdio.h> /* _snprintf */
48#endif
49
50#ifndef RT_ARCH_X86
51# error "This code is X86 only"
52#endif
53
54#include <iprt/nt/nt-and-windows.h>
55
56#include "vcc-fakes.h"
57
58
59/*********************************************************************************************************************************
60* Defined Constants And Macros *
61*********************************************************************************************************************************/
62#ifndef HEAP_STANDARD
63# define HEAP_STANDARD 0
64#endif
65
66
67/** Declare a kernel32 API.
68 * @note We are not exporting them as that causes duplicate symbol troubles in
69 * the OpenGL bits. */
70#define DECL_KERNEL32(a_Type) extern "C" a_Type WINAPI
71
72#if defined(WDK_NTDDI_VERSION) && defined(NTDDI_WIN10)
73# if WDK_NTDDI_VERSION >= NTDDI_WIN10 /* In Windows 10 SDK the 'Sequence' field has been renamed to 'CpuId'. */
74# define SLIST_HEADER_SEQUENCE_NOW_CALLED_CPUID
75# endif
76#endif
77
78
79
80/*********************************************************************************************************************************
81* Global Variables *
82*********************************************************************************************************************************/
83static volatile bool g_fInitialized = false;
84#define MAKE_IMPORT_ENTRY(a_uMajorVer, a_uMinorVer, a_Name, a_cb) DECLARE_FUNCTION_POINTER(a_Name, a_cb)
85#ifdef VCC_FAKES_TARGET_VCC100
86# include "vcc-fakes-kernel32-100.h"
87#elif defined(VCC_FAKES_TARGET_VCC140)
88# include "vcc-fakes-kernel32-141.h"
89#elif defined(VCC_FAKES_TARGET_VCC141)
90# include "vcc-fakes-kernel32-141.h"
91#elif defined(VCC_FAKES_TARGET_VCC142)
92# include "vcc-fakes-kernel32-141.h"
93#else
94# error "Port me!"
95#endif
96
97
98static BOOL FakeSetLastErrorFromNtStatus(NTSTATUS rcNt)
99{
100 DWORD dwErr;
101 switch (rcNt)
102 {
103 case STATUS_INVALID_PARAMETER:
104 case STATUS_INVALID_PARAMETER_1:
105 case STATUS_INVALID_PARAMETER_2:
106 case STATUS_INVALID_PARAMETER_3:
107 case STATUS_INVALID_PARAMETER_4:
108 case STATUS_INVALID_PARAMETER_5:
109 case STATUS_INVALID_PARAMETER_6:
110 case STATUS_INVALID_PARAMETER_7:
111 case STATUS_INVALID_PARAMETER_8:
112 case STATUS_INVALID_PARAMETER_9:
113 case STATUS_INVALID_PARAMETER_10:
114 case STATUS_INVALID_PARAMETER_11:
115 case STATUS_INVALID_PARAMETER_12:
116 dwErr = ERROR_INVALID_PARAMETER;
117 break;
118
119 case STATUS_INVALID_HANDLE:
120 dwErr = ERROR_INVALID_HANDLE;
121 break;
122
123 case STATUS_ACCESS_DENIED:
124 dwErr = ERROR_ACCESS_DENIED;
125 break;
126
127 default:
128 dwErr = ERROR_INVALID_PARAMETER;
129 break;
130 }
131 SetLastError(dwErr);
132 return FALSE;
133}
134
135
136
137DECL_KERNEL32(PVOID) Fake_DecodePointer(PVOID pvEncoded)
138{
139 return pvEncoded;
140}
141
142
143DECL_KERNEL32(PVOID) Fake_EncodePointer(PVOID pvNative)
144{
145 return pvNative;
146}
147
148
149DECL_KERNEL32(BOOL) Fake_InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION pCritSect, DWORD cSpin)
150{
151 RT_NOREF(cSpin);
152 InitializeCriticalSection(pCritSect);
153 return TRUE;
154}
155
156
157DECL_KERNEL32(HANDLE) Fake_CreateIoCompletionPort(HANDLE hFile, HANDLE hExistingCompletionPort, ULONG_PTR uCompletionKey,
158 DWORD cConcurrentThreads)
159{
160 RT_NOREF(hFile, hExistingCompletionPort, uCompletionKey, cConcurrentThreads);
161 SetLastError(ERROR_NOT_SUPPORTED);
162 return NULL;
163}
164
165
166DECL_KERNEL32(BOOL) Fake_GetQueuedCompletionStatus(HANDLE hCompletionPort, PDWORD_PTR pcbTransfered, PULONG_PTR puCompletionKey,
167 LPOVERLAPPED *ppOverlapped, DWORD cMs)
168{
169 RT_NOREF(hCompletionPort, pcbTransfered, puCompletionKey, ppOverlapped, cMs);
170 SetLastError(ERROR_NOT_SUPPORTED);
171 return FALSE;
172}
173
174
175DECL_KERNEL32(BOOL) Fake_PostQueuedCompletionStatus(HANDLE hCompletionPort, DWORD cbTransfered, ULONG_PTR uCompletionKey,
176 LPOVERLAPPED pOverlapped)
177{
178 RT_NOREF(hCompletionPort, cbTransfered, uCompletionKey, pOverlapped);
179 SetLastError(ERROR_NOT_SUPPORTED);
180 return FALSE;
181}
182
183
184DECL_KERNEL32(BOOL) Fake_HeapSetInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS enmInfoClass, PVOID pvBuf, SIZE_T cbBuf)
185{
186 RT_NOREF(hHeap);
187 if (enmInfoClass == HeapCompatibilityInformation)
188 {
189 if ( cbBuf != sizeof(ULONG)
190 || !pvBuf
191 || *(PULONG)pvBuf == HEAP_STANDARD
192 )
193 {
194 SetLastError(ERROR_INVALID_PARAMETER);
195 return FALSE;
196 }
197 return TRUE;
198 }
199
200 SetLastError(ERROR_INVALID_PARAMETER);
201 return FALSE;
202}
203
204
205DECL_KERNEL32(BOOL) Fake_HeapQueryInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS enmInfoClass,
206 PVOID pvBuf, SIZE_T cbBuf, PSIZE_T pcbRet)
207{
208 RT_NOREF(hHeap);
209 if (enmInfoClass == HeapCompatibilityInformation)
210 {
211 *pcbRet = sizeof(ULONG);
212 if (cbBuf < sizeof(ULONG) || !pvBuf)
213 {
214 SetLastError(ERROR_INSUFFICIENT_BUFFER);
215 return FALSE;
216 }
217 *(PULONG)pvBuf = HEAP_STANDARD;
218 return TRUE;
219 }
220
221 SetLastError(ERROR_INVALID_PARAMETER);
222 return FALSE;
223}
224
225
226/* These are used by INTEL\mt_obj\Timer.obj: */
227
228DECL_KERNEL32(HANDLE) Fake_CreateTimerQueue(void)
229{
230 SetLastError(ERROR_NOT_SUPPORTED);
231 return NULL;
232}
233
234DECL_KERNEL32(BOOL) Fake_CreateTimerQueueTimer(PHANDLE phTimer, HANDLE hTimerQueue, WAITORTIMERCALLBACK pfnCallback, PVOID pvUser,
235 DWORD msDueTime, DWORD msPeriod, ULONG fFlags)
236{
237 NOREF(phTimer); NOREF(hTimerQueue); NOREF(pfnCallback); NOREF(pvUser); NOREF(msDueTime); NOREF(msPeriod); NOREF(fFlags);
238 SetLastError(ERROR_NOT_SUPPORTED);
239 return FALSE;
240}
241
242DECL_KERNEL32(BOOL) Fake_DeleteTimerQueueTimer(HANDLE hTimerQueue, HANDLE hTimer, HANDLE hEvtCompletion)
243{
244 NOREF(hTimerQueue); NOREF(hTimer); NOREF(hEvtCompletion);
245 SetLastError(ERROR_NOT_SUPPORTED);
246 return FALSE;
247}
248
249/* This is used by several APIs. */
250
251DECL_KERNEL32(VOID) Fake_InitializeSListHead(PSLIST_HEADER pHead)
252{
253 pHead->Alignment = 0;
254}
255
256
257DECL_KERNEL32(PSLIST_ENTRY) Fake_InterlockedFlushSList(PSLIST_HEADER pHead)
258{
259 PSLIST_ENTRY pRet = NULL;
260 if (pHead->Next.Next)
261 {
262 for (;;)
263 {
264 SLIST_HEADER OldHead = *pHead;
265 SLIST_HEADER NewHead;
266 NewHead.Alignment = 0;
267#ifdef SLIST_HEADER_SEQUENCE_NOW_CALLED_CPUID
268 NewHead.CpuId = OldHead.CpuId + 1;
269#else
270 NewHead.Sequence = OldHead.Sequence + 1;
271#endif
272 if (ASMAtomicCmpXchgU64(&pHead->Alignment, NewHead.Alignment, OldHead.Alignment))
273 {
274 pRet = OldHead.Next.Next;
275 break;
276 }
277 }
278 }
279 return pRet;
280}
281
282DECL_KERNEL32(PSLIST_ENTRY) Fake_InterlockedPopEntrySList(PSLIST_HEADER pHead)
283{
284 PSLIST_ENTRY pRet = NULL;
285 for (;;)
286 {
287 SLIST_HEADER OldHead = *pHead;
288 pRet = OldHead.Next.Next;
289 if (pRet)
290 {
291 SLIST_HEADER NewHead;
292 __try
293 {
294 NewHead.Next.Next = pRet->Next;
295 }
296 __except(EXCEPTION_EXECUTE_HANDLER)
297 {
298 continue;
299 }
300 NewHead.Depth = OldHead.Depth - 1;
301#ifdef SLIST_HEADER_SEQUENCE_NOW_CALLED_CPUID
302 NewHead.CpuId = OldHead.CpuId + 1;
303#else
304 NewHead.Sequence = OldHead.Sequence + 1;
305#endif
306 if (ASMAtomicCmpXchgU64(&pHead->Alignment, NewHead.Alignment, OldHead.Alignment))
307 break;
308 }
309 else
310 break;
311 }
312 return pRet;
313}
314
315DECL_KERNEL32(PSLIST_ENTRY) Fake_InterlockedPushEntrySList(PSLIST_HEADER pHead, PSLIST_ENTRY pEntry)
316{
317 PSLIST_ENTRY pRet = NULL;
318 for (;;)
319 {
320 SLIST_HEADER OldHead = *pHead;
321 pRet = OldHead.Next.Next;
322 pEntry->Next = pRet;
323 SLIST_HEADER NewHead;
324 NewHead.Next.Next = pEntry;
325 NewHead.Depth = OldHead.Depth + 1;
326#ifdef SLIST_HEADER_SEQUENCE_NOW_CALLED_CPUID
327 NewHead.CpuId = OldHead.CpuId + 1;
328#else
329 NewHead.Sequence = OldHead.Sequence + 1;
330#endif
331 if (ASMAtomicCmpXchgU64(&pHead->Alignment, NewHead.Alignment, OldHead.Alignment))
332 break;
333 }
334 return pRet;
335}
336
337DECL_KERNEL32(WORD) Fake_QueryDepthSList(PSLIST_HEADER pHead)
338{
339 return pHead->Depth;
340}
341
342
343/* curl drags these in: */
344DECL_KERNEL32(BOOL) Fake_VerifyVersionInfoA(LPOSVERSIONINFOEXA pInfo, DWORD fTypeMask, DWORDLONG fConditionMask)
345{
346 OSVERSIONINFOEXA VerInfo;
347 RT_ZERO(VerInfo);
348 VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
349 if (!GetVersionExA((OSVERSIONINFO *)&VerInfo))
350 {
351 RT_ZERO(VerInfo);
352 VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
353 BOOL fRet = GetVersionExA((OSVERSIONINFO *)&VerInfo);
354 if (fRet)
355 { /* likely */ }
356 else
357 {
358 MY_ASSERT(false, "VerifyVersionInfoA: #1");
359 return FALSE;
360 }
361 }
362
363 BOOL fRet = TRUE;
364 for (unsigned i = 0; i < 8 && fRet; i++)
365 if (fTypeMask & RT_BIT_32(i))
366 {
367 uint32_t uLeft, uRight;
368 switch (RT_BIT_32(i))
369 {
370#define MY_CASE(a_Num, a_Member) case a_Num: uLeft = VerInfo.a_Member; uRight = pInfo->a_Member; break
371 MY_CASE(VER_MINORVERSION, dwMinorVersion);
372 MY_CASE(VER_MAJORVERSION, dwMajorVersion);
373 MY_CASE(VER_BUILDNUMBER, dwBuildNumber);
374 MY_CASE(VER_PLATFORMID, dwPlatformId);
375 MY_CASE(VER_SERVICEPACKMINOR, wServicePackMinor);
376 MY_CASE(VER_SERVICEPACKMAJOR, wServicePackMinor);
377 MY_CASE(VER_SUITENAME, wSuiteMask);
378 MY_CASE(VER_PRODUCT_TYPE, wProductType);
379#undef MY_CASE
380 default: uLeft = uRight = 0; MY_ASSERT(false, "VerifyVersionInfoA: #2");
381 }
382 switch ((uint8_t)(fConditionMask >> (i*8)))
383 {
384 case VER_EQUAL: fRet = uLeft == uRight; break;
385 case VER_GREATER: fRet = uLeft > uRight; break;
386 case VER_GREATER_EQUAL: fRet = uLeft >= uRight; break;
387 case VER_LESS: fRet = uLeft < uRight; break;
388 case VER_LESS_EQUAL: fRet = uLeft <= uRight; break;
389 case VER_AND: fRet = (uLeft & uRight) == uRight; break;
390 case VER_OR: fRet = (uLeft & uRight) != 0; break;
391 default: fRet = FALSE; MY_ASSERT(false, "VerifyVersionInfoA: #3"); break;
392 }
393 }
394
395 return fRet;
396}
397
398
399DECL_KERNEL32(ULONGLONG) Fake_VerSetConditionMask(ULONGLONG fConditionMask, DWORD fTypeMask, BYTE bOperator)
400{
401 for (unsigned i = 0; i < 8; i++)
402 if (fTypeMask & RT_BIT_32(i))
403 {
404 uint64_t fMask = UINT64_C(0xff) << (i*8);
405 fConditionMask &= ~fMask;
406 fConditionMask |= (uint64_t)bOperator << (i*8);
407
408 }
409 return fConditionMask;
410}
411
412
413#if VCC_FAKES_TARGET >= 140
414/** @since 5.0 (windows 2000) */
415DECL_KERNEL32(BOOL) Fake_GetModuleHandleExW(DWORD dwFlags, LPCWSTR pwszModuleName, HMODULE *phModule)
416{
417 HMODULE hmod;
418 if (dwFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
419 {
420 /** @todo search the loader list. */
421 SetLastError(ERROR_NOT_SUPPORTED);
422 return FALSE;
423 }
424 else
425 {
426 hmod = GetModuleHandleW(pwszModuleName);
427 if (!hmod)
428 return FALSE;
429 }
430
431 /*
432 * Get references to the module.
433 */
434 if ( !(dwFlags & (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_PIN))
435 && GetModuleHandleW(NULL) != hmod)
436 {
437 WCHAR wszModule[MAX_PATH];
438 if (GetModuleFileNameW(hmod, wszModule, RT_ELEMENTS(wszModule)) > 0)
439 {
440 if (dwFlags & GET_MODULE_HANDLE_EX_FLAG_PIN)
441 {
442 uint32_t cRefs = 32;
443 while (cRefs-- > 0)
444 LoadLibraryW(wszModule);
445 }
446 else if (!LoadLibraryW(wszModule))
447 return FALSE;
448 }
449 }
450
451 *phModule = hmod;
452 return TRUE;
453}
454#endif /* VCC_FAKES_TARGET >= 141 */
455
456
457#if VCC_FAKES_TARGET >= 140
458/** @since 5.0 (windows 2000) */
459DECL_KERNEL32(BOOL) Fake_SetFilePointerEx(HANDLE hFile, LARGE_INTEGER offDistanceToMove,
460 PLARGE_INTEGER pNewFilePointer, DWORD dwMoveMethod)
461{
462 NTSTATUS rcNt;
463 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
464
465 FILE_POSITION_INFORMATION PosInfo;
466 switch (dwMoveMethod)
467 {
468 case FILE_BEGIN:
469 PosInfo.CurrentByteOffset = offDistanceToMove;
470 break;
471
472 case FILE_CURRENT:
473 PosInfo.CurrentByteOffset.QuadPart = INT64_MAX;
474 rcNt = NtQueryInformationFile(hFile, &Ios, &PosInfo, sizeof(PosInfo), FilePositionInformation);
475 if (NT_SUCCESS(rcNt))
476 {
477 PosInfo.CurrentByteOffset.QuadPart += offDistanceToMove.QuadPart;
478 break;
479 }
480 return FakeSetLastErrorFromNtStatus(rcNt);
481
482 case FILE_END:
483 {
484 FILE_STANDARD_INFO StdInfo = {{0}};
485 rcNt = NtQueryInformationFile(hFile, &Ios, &StdInfo, sizeof(StdInfo), FileStandardInformation);
486 if (NT_SUCCESS(rcNt))
487 {
488 PosInfo.CurrentByteOffset.QuadPart = offDistanceToMove.QuadPart + StdInfo.EndOfFile.QuadPart;
489 break;
490 }
491 return FakeSetLastErrorFromNtStatus(rcNt);
492 }
493
494 default:
495 SetLastError(ERROR_INVALID_PARAMETER);
496 return FALSE;
497 }
498
499 rcNt = NtSetInformationFile(hFile, &Ios, &PosInfo, sizeof(PosInfo), FilePositionInformation);
500 if (NT_SUCCESS(rcNt))
501 {
502 if (pNewFilePointer)
503 *pNewFilePointer = PosInfo.CurrentByteOffset;
504 return TRUE;
505 }
506 return FakeSetLastErrorFromNtStatus(rcNt);
507}
508#endif /* VCC_FAKES_TARGET >= 140 */
509
510
511#if VCC_FAKES_TARGET >= 140
512/** @since 5.0 (windows 2000) */
513DECL_KERNEL32(BOOL) Fake_GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER pcbFile)
514{
515 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
516 FILE_STANDARD_INFO StdInfo = {{0}};
517 NTSTATUS rcNt = NtQueryInformationFile(hFile, &Ios, &StdInfo, sizeof(StdInfo), FileStandardInformation);
518 if (NT_SUCCESS(rcNt))
519 {
520 *pcbFile = StdInfo.EndOfFile;
521 return TRUE;
522 }
523 return FakeSetLastErrorFromNtStatus(rcNt);
524}
525#endif /* VCC_FAKES_TARGET >= 140 */
526
527
528
529
530/*
531 * NT 3.51 stuff.
532 */
533
534#if VCC_FAKES_TARGET >= 140
535/** @since 4.0 */
536DECL_KERNEL32(HANDLE) Fake_FindFirstFileExW(LPCWSTR pwszFileName, FINDEX_INFO_LEVELS enmInfoLevel, LPVOID pvFindFileData,
537 FINDEX_SEARCH_OPS enmSearchOp, LPVOID pvSearchFilter, DWORD dwAdditionalFlags)
538{
539 // STL: FindFirstFileExW(, FindExInfoBasic, , FindExSearchNameMatch, NULL, 0);
540 // CRT/_findfile: FindFirstFileExW(, FindExInfoStandard, , FindExSearchNameMatch, NULL, 0);
541 // CRT/argv_wildcards: FindFirstFileExW(, FindExInfoStandard, , FindExSearchNameMatch, NULL, 0);
542 MY_ASSERT_STMT_RETURN(dwAdditionalFlags == 0, SetLastError(ERROR_INVALID_PARAMETER), INVALID_HANDLE_VALUE);
543 MY_ASSERT_STMT_RETURN(pvSearchFilter == NULL, SetLastError(ERROR_INVALID_PARAMETER), INVALID_HANDLE_VALUE);
544 MY_ASSERT_STMT_RETURN(enmSearchOp == FindExSearchNameMatch, SetLastError(ERROR_INVALID_PARAMETER), INVALID_HANDLE_VALUE);
545 MY_ASSERT_STMT_RETURN(enmInfoLevel == FindExInfoStandard || enmInfoLevel == FindExInfoBasic,
546 SetLastError(ERROR_INVALID_PARAMETER), INVALID_HANDLE_VALUE);
547
548 return FindFirstFileW(pwszFileName, (WIN32_FIND_DATAW *)pvFindFileData);
549}
550#endif /* VCC_FAKES_TARGET >= 140 */
551
552
553DECL_KERNEL32(BOOL) Fake_IsProcessorFeaturePresent(DWORD enmProcessorFeature)
554{
555 /* Could make more of an effort here... */
556 RT_NOREF(enmProcessorFeature);
557 return FALSE;
558}
559
560
561DECL_KERNEL32(BOOL) Fake_CancelIo(HANDLE hHandle)
562{
563 /* All NT versions the NTDLL API this corresponds to. */
564 RESOLVE_NTDLL_API(NtCancelIoFile);
565 if (pfnNtCancelIoFile)
566 {
567 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
568 NTSTATUS rcNt = pfnNtCancelIoFile(hHandle, &Ios);
569 if (RT_SUCCESS(rcNt))
570 return TRUE;
571 if (rcNt == STATUS_INVALID_HANDLE)
572 SetLastError(ERROR_INVALID_HANDLE);
573 else
574 SetLastError(ERROR_INVALID_FUNCTION);
575 }
576 else
577 SetLastError(ERROR_NOT_SUPPORTED);
578 return FALSE;
579}
580
581
582/*
583 * NT 3.50 stuff.
584 */
585
586#if VCC_FAKES_TARGET >= 140
587/** @since 3.51 */
588DECL_KERNEL32(VOID) Fake_FreeLibraryAndExitThread(HMODULE hLibModule, DWORD dwExitCode)
589{
590 if (hLibModule)
591 FreeModule(hLibModule);
592 ExitThread(dwExitCode);
593}
594#endif /* VCC_FAKES_TARGET >= 140 */
595
596
597DECL_KERNEL32(BOOL) Fake_IsDebuggerPresent(VOID)
598{
599 /* Fallback: */
600 return FALSE;
601}
602
603
604DECL_KERNEL32(VOID) Fake_GetSystemTimeAsFileTime(LPFILETIME pTime)
605{
606 DWORD dwVersion = GetVersion();
607 if ( (dwVersion & 0xff) > 3
608 || ( (dwVersion & 0xff) == 3
609 && ((dwVersion >> 8) & 0xff) >= 50) )
610 {
611 PKUSER_SHARED_DATA pUsd = (PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA;
612
613 /* use interrupt time */
614 LARGE_INTEGER Time;
615 do
616 {
617 Time.HighPart = pUsd->SystemTime.High1Time;
618 Time.LowPart = pUsd->SystemTime.LowPart;
619 } while (pUsd->SystemTime.High2Time != Time.HighPart);
620
621 pTime->dwHighDateTime = Time.HighPart;
622 pTime->dwLowDateTime = Time.LowPart;
623 }
624 else
625 {
626 /* NT 3.1 didn't have a KUSER_SHARED_DATA nor a GetSystemTimeAsFileTime export. */
627 SYSTEMTIME SystemTime;
628 GetSystemTime(&SystemTime);
629 BOOL fRet = SystemTimeToFileTime(&SystemTime, pTime);
630 if (fRet)
631 { /* likely */ }
632 else
633 {
634 MY_ASSERT(false, "GetSystemTimeAsFileTime: #2");
635 pTime->dwHighDateTime = 0;
636 pTime->dwLowDateTime = 0;
637 }
638 }
639}
640
641
642/*
643 * NT 3.1 stuff.
644 */
645
646DECL_KERNEL32(BOOL) Fake_GetVersionExA(LPOSVERSIONINFOA pInfo)
647{
648 DWORD dwVersion = GetVersion();
649
650 /* Common fields: */
651 pInfo->dwMajorVersion = dwVersion & 0xff;
652 pInfo->dwMinorVersion = (dwVersion >> 8) & 0xff;
653 if (!(dwVersion & RT_BIT_32(31)))
654 pInfo->dwBuildNumber = dwVersion >> 16;
655 else
656 pInfo->dwBuildNumber = 511;
657 pInfo->dwPlatformId = VER_PLATFORM_WIN32_NT;
658/** @todo get CSD from registry. */
659 pInfo->szCSDVersion[0] = '\0';
660
661 /* OSVERSIONINFOEX fields: */
662 if (pInfo->dwOSVersionInfoSize > sizeof((*pInfo)))
663 {
664 LPOSVERSIONINFOEXA pInfoEx = (LPOSVERSIONINFOEXA)pInfo;
665 if (pInfoEx->dwOSVersionInfoSize > RT_UOFFSETOF(OSVERSIONINFOEXA, wServicePackMinor))
666 {
667 pInfoEx->wServicePackMajor = 0;
668 pInfoEx->wServicePackMinor = 0;
669 }
670 if (pInfoEx->dwOSVersionInfoSize > RT_UOFFSETOF(OSVERSIONINFOEXA, wSuiteMask))
671 pInfoEx->wSuiteMask = 0;
672 if (pInfoEx->dwOSVersionInfoSize > RT_UOFFSETOF(OSVERSIONINFOEXA, wProductType))
673 pInfoEx->wProductType = VER_NT_WORKSTATION;
674 if (pInfoEx->wReserved > RT_UOFFSETOF(OSVERSIONINFOEXA, wProductType))
675 pInfoEx->wReserved = 0;
676 }
677
678 return TRUE;
679}
680
681
682DECL_KERNEL32(BOOL) Fake_GetVersionExW(LPOSVERSIONINFOW pInfo)
683{
684 DWORD dwVersion = GetVersion();
685
686 /* Common fields: */
687 pInfo->dwMajorVersion = dwVersion & 0xff;
688 pInfo->dwMinorVersion = (dwVersion >> 8) & 0xff;
689 if (!(dwVersion & RT_BIT_32(31)))
690 pInfo->dwBuildNumber = dwVersion >> 16;
691 else
692 pInfo->dwBuildNumber = 511;
693 pInfo->dwPlatformId = VER_PLATFORM_WIN32_NT;
694/** @todo get CSD from registry. */
695 pInfo->szCSDVersion[0] = '\0';
696
697 /* OSVERSIONINFOEX fields: */
698 if (pInfo->dwOSVersionInfoSize > sizeof((*pInfo)))
699 {
700 LPOSVERSIONINFOEXW pInfoEx = (LPOSVERSIONINFOEXW)pInfo;
701 if (pInfoEx->dwOSVersionInfoSize > RT_UOFFSETOF(OSVERSIONINFOEXW, wServicePackMinor))
702 {
703 pInfoEx->wServicePackMajor = 0;
704 pInfoEx->wServicePackMinor = 0;
705 }
706 if (pInfoEx->dwOSVersionInfoSize > RT_UOFFSETOF(OSVERSIONINFOEXW, wSuiteMask))
707 pInfoEx->wSuiteMask = 0;
708 if (pInfoEx->dwOSVersionInfoSize > RT_UOFFSETOF(OSVERSIONINFOEXW, wProductType))
709 pInfoEx->wProductType = VER_NT_WORKSTATION;
710 if (pInfoEx->wReserved > RT_UOFFSETOF(OSVERSIONINFOEXW, wProductType))
711 pInfoEx->wReserved = 0;
712 }
713
714 return TRUE;
715}
716
717
718DECL_KERNEL32(LPWCH) Fake_GetEnvironmentStringsW(void)
719{
720 /*
721 * Environment is ANSI in NT 3.1. We should only be here for NT 3.1.
722 * For now, don't try do a perfect job converting it, just do it.
723 */
724 char *pszzEnv = (char *)RTNtCurrentPeb()->ProcessParameters->Environment;
725 size_t offEnv = 0;
726 while (pszzEnv[offEnv] != '\0')
727 {
728 size_t cchLen = strlen(&pszzEnv[offEnv]);
729 offEnv += cchLen + 1;
730 }
731 size_t const cchEnv = offEnv + 1;
732
733 PRTUTF16 pwszzEnv = (PRTUTF16)HeapAlloc(GetProcessHeap(), 0, cchEnv * sizeof(RTUTF16));
734 if (!pwszzEnv)
735 return NULL;
736 for (offEnv = 0; offEnv < cchEnv; offEnv++)
737 {
738 unsigned char ch = pwszzEnv[offEnv];
739 if (!(ch & 0x80))
740 pwszzEnv[offEnv] = ch;
741 else
742 pwszzEnv[offEnv] = '_';
743 }
744 return pwszzEnv;
745}
746
747
748DECL_KERNEL32(BOOL) Fake_FreeEnvironmentStringsW(LPWCH pwszzEnv)
749{
750 if (pwszzEnv)
751 HeapFree(GetProcessHeap(), 0, pwszzEnv);
752 return TRUE;
753}
754
755
756DECL_KERNEL32(int) Fake_GetLocaleInfoA(LCID idLocale, LCTYPE enmType, LPSTR pData, int cchData)
757{
758 NOREF(idLocale); NOREF(enmType); NOREF(pData); NOREF(cchData);
759 //MY_ASSERT(false, "GetLocaleInfoA: idLocale=%#x enmType=%#x cchData=%#x", idLocale, enmType, cchData);
760 MY_ASSERT(false, "GetLocaleInfoA");
761 SetLastError(ERROR_NOT_SUPPORTED);
762 return 0;
763}
764
765
766#if VCC_FAKES_TARGET >= 140
767/** @since 3.51 */
768DECL_KERNEL32(BOOL) Fake_EnumSystemLocalesW(LOCALE_ENUMPROCW pfnLocaleEnum, DWORD dwFlags)
769{
770 RT_NOREF(pfnLocaleEnum, dwFlags);
771 SetLastError(ERROR_NOT_SUPPORTED);
772 return FALSE;
773}
774#endif /* VCC_FAKES_TARGET >= 140 */
775
776
777DECL_KERNEL32(BOOL) Fake_EnumSystemLocalesA(LOCALE_ENUMPROCA pfnCallback, DWORD fFlags)
778{
779 NOREF(pfnCallback); NOREF(fFlags);
780 //MY_ASSERT(false, "EnumSystemLocalesA: pfnCallback=%p fFlags=%#x", pfnCallback, fFlags);
781 MY_ASSERT(false, "EnumSystemLocalesA");
782 SetLastError(ERROR_NOT_SUPPORTED);
783 return FALSE;
784}
785
786
787DECL_KERNEL32(BOOL) Fake_IsValidLocale(LCID idLocale, DWORD fFlags)
788{
789 NOREF(idLocale); NOREF(fFlags);
790 //MY_ASSERT(false, "IsValidLocale: idLocale fFlags=%#x", idLocale, fFlags);
791 MY_ASSERT(false, "IsValidLocale");
792 SetLastError(ERROR_NOT_SUPPORTED);
793 return FALSE;
794}
795
796
797DECL_KERNEL32(DWORD_PTR) Fake_SetThreadAffinityMask(HANDLE hThread, DWORD_PTR fAffinityMask)
798{
799 SYSTEM_INFO SysInfo;
800 GetSystemInfo(&SysInfo);
801 //MY_ASSERT(false, "SetThreadAffinityMask: hThread=%p fAffinityMask=%p SysInfo.dwActiveProcessorMask=%p",
802 // hThread, fAffinityMask, SysInfo.dwActiveProcessorMask);
803 MY_ASSERT(false, "SetThreadAffinityMask");
804 if ( SysInfo.dwActiveProcessorMask == fAffinityMask
805 || fAffinityMask == ~(DWORD_PTR)0)
806 return fAffinityMask;
807
808 SetLastError(ERROR_NOT_SUPPORTED);
809 RT_NOREF(hThread);
810 return 0;
811}
812
813
814DECL_KERNEL32(BOOL) Fake_GetProcessAffinityMask(HANDLE hProcess, PDWORD_PTR pfProcessAffinityMask, PDWORD_PTR pfSystemAffinityMask)
815{
816 SYSTEM_INFO SysInfo;
817 GetSystemInfo(&SysInfo);
818 //MY_ASSERT(false, "GetProcessAffinityMask: SysInfo.dwActiveProcessorMask=%p", SysInfo.dwActiveProcessorMask);
819 MY_ASSERT(false, "GetProcessAffinityMask");
820 if (pfProcessAffinityMask)
821 *pfProcessAffinityMask = SysInfo.dwActiveProcessorMask;
822 if (pfSystemAffinityMask)
823 *pfSystemAffinityMask = SysInfo.dwActiveProcessorMask;
824 RT_NOREF(hProcess);
825 return TRUE;
826}
827
828
829DECL_KERNEL32(BOOL) Fake_GetHandleInformation(HANDLE hObject, DWORD *pfFlags)
830{
831 OBJECT_HANDLE_FLAG_INFORMATION Info = { 0, 0 };
832 DWORD cbRet = sizeof(Info);
833 NTSTATUS rcNt = NtQueryObject(hObject, ObjectHandleFlagInformation, &Info, sizeof(Info), &cbRet);
834 if (NT_SUCCESS(rcNt))
835 {
836 *pfFlags = (Info.Inherit ? HANDLE_FLAG_INHERIT : 0)
837 | (Info.ProtectFromClose ? HANDLE_FLAG_PROTECT_FROM_CLOSE : 0);
838 return TRUE;
839 }
840 *pfFlags = 0;
841 //MY_ASSERT(rcNt == STATUS_INVALID_HANDLE, "rcNt=%#x", rcNt);
842 MY_ASSERT(rcNt == STATUS_INVALID_HANDLE || rcNt == STATUS_INVALID_INFO_CLASS, "GetHandleInformation");
843 SetLastError(rcNt == STATUS_INVALID_HANDLE ? ERROR_INVALID_HANDLE : ERROR_INVALID_FUNCTION); /* see also process-win.cpp */
844 return FALSE;
845}
846
847
848DECL_KERNEL32(BOOL) Fake_SetHandleInformation(HANDLE hObject, DWORD fMask, DWORD fFlags)
849{
850 NOREF(hObject); NOREF(fMask); NOREF(fFlags);
851 SetLastError(ERROR_INVALID_FUNCTION);
852 return FALSE;
853}
854
855
856
857/**
858 * Resolves all the APIs ones and for all, updating the fake IAT entries.
859 */
860DECLASM(void) FakeResolve_kernel32(void)
861{
862 CURRENT_VERSION_VARIABLE();
863
864 HMODULE hmod = GetModuleHandleW(L"kernel32");
865 MY_ASSERT(hmod != NULL, "kernel32");
866
867#undef MAKE_IMPORT_ENTRY
868#define MAKE_IMPORT_ENTRY(a_uMajorVer, a_uMinorVer, a_Name, a_cb) RESOLVE_IMPORT(a_uMajorVer, a_uMinorVer, a_Name, a_cb)
869#ifdef VCC_FAKES_TARGET_VCC100
870# include "vcc-fakes-kernel32-100.h"
871#elif defined(VCC_FAKES_TARGET_VCC140)
872# include "vcc-fakes-kernel32-141.h"
873#elif defined(VCC_FAKES_TARGET_VCC141)
874# include "vcc-fakes-kernel32-141.h"
875#elif defined(VCC_FAKES_TARGET_VCC142)
876# include "vcc-fakes-kernel32-141.h"
877#else
878# error "Port me!"
879#endif
880
881 g_fInitialized = true;
882}
883
884
885/* Dummy to force dragging in this object in the link, so the linker
886 won't accidentally use the symbols from kernel32. */
887extern "C" int vcc100_kernel32_fakes_cpp(void)
888{
889 return 42;
890}
891
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette