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

我怎样才能获得有关 Windows操作系统类型的信息?是32位还是64位?我如何以编程方式获取此信息?

解决方法

您需要使用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;

因为该功能仅适用于具有64位风格的Windows版本.将其声明为外部将阻止您的应用程序在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;

缓存此函数结果是安全的,因为API函数将存在或不存在,并且其结果不能在同一Windows安装上更改.从多个线程同时调用它是安全的,因为发现WasCalled为False的两个线程都会调用该函数,将相同的结果写入相同的内存位置,然后才将WasCalled设置为True.

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...