From a774075f2f36bffd342e4ef4d96d297a8524ef5e Mon Sep 17 00:00:00 2001 From: Rika Ichinose Date: Fri, 26 Jan 2024 16:57:49 +0300 Subject: [PATCH] Support Windows XP where SetThreadStackGuarantee is absent. --- rtl/win/sysos.inc | 6 +++--- rtl/win/systhrd.inc | 5 +++-- rtl/win32/system.pp | 4 +++- rtl/win64/system.pp | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/rtl/win/sysos.inc b/rtl/win/sysos.inc index aa8fd954d6..41c63dd625 100644 --- a/rtl/win/sysos.inc +++ b/rtl/win/sysos.inc @@ -273,8 +273,8 @@ type function GetModuleHandle(p : PAnsiChar) : THandle; stdcall;external KernelDLL name 'GetModuleHandleA'; - function SetThreadStackGuarantee(StackSizeInBytes : PPtrUint) : BOOL; - stdcall;external KernelDLL name 'SetThreadStackGuarantee'; +var + SetThreadStackGuarantee: function(StackSizeInBytes : PPtrUint) : BOOL; stdcall; {$else WINCE} { module functions } @@ -304,7 +304,7 @@ type procedure GetSystemInfo(lpSystemInfo: PSystemInfo); {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetSystemInfo'; function WinLoadLibraryW(lpLibFileName:pwidechar):THandle; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'LoadLibraryW'; - function WinLoadLibraryA(lpLibFileName:pwidechar):THandle; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'LoadLibraryA'; + function WinGetModuleHandleW(lpModuleName:pwidechar):THandle; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'GetModuleHandleW'; {$ifdef wince} function WinGetProcAddress(hModule:THandle; lpProcName:PAnsiChar):pointer; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'GetProcAddressA'; {$else} diff --git a/rtl/win/systhrd.inc b/rtl/win/systhrd.inc index fb8d2956ac..5f83d59e01 100644 --- a/rtl/win/systhrd.inc +++ b/rtl/win/systhrd.inc @@ -229,8 +229,9 @@ var if ThreadID=TThreadID(0) then begin InitThread(ti.stklen); -{$ifndef wince} - SetThreadStackGuarantee(@StackMargin); +{$ifndef wince} + if Assigned(SetThreadStackGuarantee) then + SetThreadStackGuarantee(@StackMargin); {$endif wince} end; diff --git a/rtl/win32/system.pp b/rtl/win32/system.pp index dcd43d0b68..305f0cf05b 100644 --- a/rtl/win32/system.pp +++ b/rtl/win32/system.pp @@ -619,7 +619,9 @@ initialization { pass dummy value } StackLength := CheckInitialStkLen($1000000); StackBottom := StackTop - StackLength; - SetThreadStackGuarantee(@StackMargin); + CodePointer(SetThreadStackGuarantee) := WinGetProcAddress(WinGetModuleHandleW(KernelDLL), 'SetThreadStackGuarantee'); + if Assigned(SetThreadStackGuarantee) then + SetThreadStackGuarantee(@StackMargin); cmdshow:=startupinfo.wshowwindow; { Setup heap and threading, these may be already initialized from TLS callback } diff --git a/rtl/win64/system.pp b/rtl/win64/system.pp index 7082761848..9d88293eb9 100644 --- a/rtl/win64/system.pp +++ b/rtl/win64/system.pp @@ -478,7 +478,9 @@ initialization { pass dummy value } StackLength := CheckInitialStkLen($1000000); StackBottom := StackTop - StackLength; - SetThreadStackGuarantee(@StackMargin); + CodePointer(SetThreadStackGuarantee) := WinGetProcAddress(WinGetModuleHandleW(KernelDLL), 'SetThreadStackGuarantee'); + if Assigned(SetThreadStackGuarantee) then + SetThreadStackGuarantee(@StackMargin); { get some helpful informations } GetStartupInfo(@startupinfo);