You need to use GetProcAddress() to check the availability of the IsWow64Process() function at run time, for example:
function Is64BitWindows: boolean; type TIsWow64Process = function(hProcess: THandle; var Wow64Process: BOOL): BOOL; stdcall; var DLLHandle: THandle; pIsWow64Process: TIsWow64Process; IsWow64: BOOL; begin Result := False; DllHandle := LoadLibrary('kernel32.dll'); if DLLHandle <> 0 then begin pIsWow64Process := GetProcAddress(DLLHandle, 'IsWow64Process'); Result := Assigned(pIsWow64Process) and pIsWow64Process(GetCurrentProcess, IsWow64) and IsWow64; FreeLibrary(DLLHandle); end; end;
because this feature is only available on versions of Windows that have a 64-bit flavor. An external declaration will prevent your application from running on Windows 2000 or Windows XP prior to SP2.
Edit:
Chris posted a comment on caching the result for performance reasons. This may not be necessary for this particular API function, because kernel32.dll will always be there (and I canβt imagine a program that even loads without it), but for other functions it could be otherwise. So, here is the version that caches the result of the function:
function Is64BitWindows: boolean; type TIsWow64Process = function(hProcess: THandle; var Wow64Process: BOOL): BOOL; stdcall; var DLLHandle: THandle; pIsWow64Process: TIsWow64Process; const WasCalled: BOOL = False; IsWow64: BOOL = False; begin if not WasCalled then begin DllHandle := LoadLibrary('kernel32.dll'); if DLLHandle <> 0 then begin pIsWow64Process := GetProcAddress(DLLHandle, 'IsWow64Process'); if Assigned(pIsWow64Process) then pIsWow64Process(GetCurrentProcess, IsWow64); WasCalled := True; FreeLibrary(DLLHandle); end; end; Result := IsWow64; end;
Caching this function is safe, because the API function will either be there or not, and its result cannot change with the same Windows installation. Itβs even safe to call from multiple threads simultaneously, since two threads, finding WasCalled , which will be False , will call the function, write the same result to the same memory location, and only after that set WasCalled to True .
mghie Mar 26 2018-10-28T00: 00Z
source share