如何获取有关电脑的信息? [32位或64位] [英] How to get information about the computer? [32bit or 64bit]

查看:144
本文介绍了如何获取有关电脑的信息? [32位或64位]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取有关Windows操作系统类型的信息?是32bit还是64bit?我如何以编程方式获取这些信息?

How I can get information about Windows OS type? Is it 32bit or 64bit? How I can get this information programatically?

推荐答案

您需要使用 GetProcAddress()来检查 <$ c的可用性$ c> IsWow64Process() 函数在运行时,如下所示:

You need to use GetProcAddress() to check the availability of the IsWow64Process() function at runtime, like so:

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版本。将其声明为 external 将阻止您的应用程序在Windows 2000或Windows XP SP2之前运行。

because that function is only available on Windows versions that do have a 64 bit flavour. Declaring it as external would prevent your application from running on Windows 2000 or Windows XP pre SP2.

编辑:

由于性能原因,Chris发布了缓存结果的评论。这对于这个特定的API函数来说可能不是必需的,因为kernel32.dll 将永远在那里(我无法想象一个甚至可以加载的程序),但是对于其他功能,不同。所以这里是缓存函数结果的一个版本:

Chris has posted a comment about caching the result for performance reasons. This may not be necessary for this particular API function, because kernel32.dll will always be there (and I can't imagine a program that would even load without it), but for other functions things may be different. So here's a version that caches the function result:

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

Caching this function result is safe, as the API function will either be there or not, and its result can't change on the same Windows installation. It is even safe to call this concurrently from multiple threads, as two threads finding WasCalled to be False will both call the function, write the same result to the same memory location, and only afterwards set WasCalled to True.

这篇关于如何获取有关电脑的信息? [32位或64位]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆