ged*_*edO 7 delphi delphi-2007 32bit-64bit
如何获取有关Windows操作系统类型的信息?是32位还是64位?我如何以编程方式获取此信息?
klu*_*udg 12
function IsWin64: Boolean;
var
IsWow64Process : function(hProcess : THandle; var Wow64Process : BOOL): BOOL; stdcall;
Wow64Process : BOOL;
begin
Result := False;
IsWow64Process := GetProcAddress(GetModuleHandle(Kernel32), 'IsWow64Process');
if Assigned(IsWow64Process) then begin
if IsWow64Process(GetCurrentProcess, Wow64Process) then begin
Result := Wow64Process;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
您需要使用在运行时GetProcAddress()
检查IsWow64Process()
函数的可用性,如下所示:
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;
Run Code Online (Sandbox Code Playgroud)
因为该功能仅适用于具有64位风格的Windows版本.将其声明为external
阻止您的应用程序在Windows 2000或Windows XP SP2之前运行.
编辑:
由于性能原因,Chris发布了关于缓存结果的评论.对于这个特定的API函数,这可能不是必需的,因为kernel32.dll 将永远存在(并且我无法想象一个程序甚至可以在没有它的情况下加载),但是对于其他函数,事情可能会有所不同.所以这是一个缓存函数结果的版本:
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;
Run Code Online (Sandbox Code Playgroud)
缓存此函数结果是安全的,因为API函数将存在或不存在,并且其结果不能在同一Windows安装上更改.从多个线程同时调用它是安全的,因为发现的两个线程WasCalled
都False
将调用该函数,将相同的结果写入相同的内存位置,然后才设置WasCalled
为True
.