如何获取有关计算机的信息?[32位或64位]

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)


mgh*_*hie 7

您需要使用在运行时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安装上更改.从多个线程同时调用它是安全的,因为发现的两个线程WasCalledFalse将调用该函数,将相同的结果写入相同的内存位置,然后才设置WasCalledTrue.

  • 只有在项目设置中启用了可分配/可写常量时,才会编译此代码.避免被敏感该编译器设置,无论是引入指令,以确保此编译器行为被设置(和恢复)为解决此代码所需,或更好的是,我建议使用一个单元变量高速缓存的结果(使用整数避免需要使用两个这样的变量:例如,声明initialised = -1表示"未设置",set = 0表示Win32 set = 1表示Win64). (2认同)