加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:07:02 所属栏目:大数据 来源:网络整理
导读:我怎样才能获得有关 Windows操作系统类型的信息?是32位还是64位?我如何以编程方式获取此信息? 解决方法 您需要使用GetProcAddress()在运行时检查 IsWow64Process() 函数的可用性,如下所示: function Is64BitWindows: boolean;type TIsWow64Process = fun
我怎样才能获得有关 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读