-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwsUtilArch.pp
More file actions
108 lines (93 loc) · 3.39 KB
/
Copy pathwsUtilArch.pp
File metadata and controls
108 lines (93 loc) · 3.39 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
{ Copyright (C) 2020-2021 by Bill Stewart (bstewart at iname.com)
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
unit wsUtilArch;
interface
// Returns true if the current OS is 64-bit or false otherwise
function IsWin64(): Boolean;
// Returns true if the current process is WoW64 (i.e., 32-bit running on a
// 64-bit OS), or false otherwise
function IsProcessWoW64(): Boolean;
implementation
uses
Windows;
function IsProcessor64Bit(): Boolean;
const
PROCESSOR_ARCHITECTURE_INTEL = 0;
PROCESSOR_ARCHITECTURE_ARM = 5;
PROCESSOR_ARCHITECTURE_IA64 = 6;
PROCESSOR_ARCHITECTURE_AMD64 = 9;
PROCESSOR_ARCHITECTURE_ARM64 = 12;
PROCESSOR_ARCHITECTURE_UNKNOWN = $FFFF;
type
TGetNativeSystemInfo = procedure(var lpSystemInfo: SYSTEM_INFO); stdcall;
var
Kernel32: HMODULE;
GetNativeSystemInfo: TGetNativeSystemInfo;
SystemInfo: SYSTEM_INFO;
begin
result := false;
Kernel32 := GetModuleHandleW('kernel32'); // LPCWSTR lpModuleName
GetNativeSystemInfo := TGetNativeSystemInfo(GetProcAddress(Kernel32, // HMODULE hModule
'GetNativeSystemInfo')); // LPCSTR lpProcName
if Assigned(GetNativeSystemInfo) then
begin
GetNativeSystemInfo(SystemInfo); // LPSYSTEM_INFO lpSystemInfo
with SystemInfo do
result := (wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64) or
(wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64) or
(wProcessorArchitecture = PROCESSOR_ARCHITECTURE_ARM64);
end;
end;
function IsProcessWoW64(): Boolean;
type
TIsWow64Process = function(hProcess: HANDLE; var Wow64Process: BOOL): BOOL; stdcall;
var
Kernel32: HMODULE;
IsWow64Process: TIsWow64Process;
ProcessHandle: HANDLE;
IsWoW64: BOOL;
begin
result := false;
Kernel32 := GetModuleHandleW('kernel32'); // LPCWSTR lpModuleName
IsWow64Process := TIsWow64Process(GetProcAddress(Kernel32, // HMODULE hModule
'IsWow64Process')); // LPCSTR lpProcName
if Assigned(IsWow64Process) then
begin
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION, // DWORD dwDesiredAccess
true, // BOOL bInheritHandle
GetCurrentProcessId()); // DWORD dwProcessId
if ProcessHandle <> 0 then
begin
if IsWow64Process(ProcessHandle, // HANDLE hProcess
IsWoW64) then // PBOOL Wow64Process
begin
result := IsWoW64;
end;
CloseHandle(ProcessHandle); // HANDLE hObject
end;
end;
end;
function IsWin64(): Boolean;
begin
{$IFDEF WIN64}
// compiled on x64
result := true;
{$ELSE}
// true if processor is 64-bit and current process is WoW64
result := IsProcessor64Bit() and IsProcessWoW64();
{$ENDIF}
end;
begin
end.