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

问题描述

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

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

推荐答案

您需要使用 GetProcAddress() 来检查 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 pre 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 安装上改变.从多个线程并发调用它甚至是安全的,因为发现 WasCalledFalse 的两个线程都会调用该函数,将相同的结果写入相同的内存位置,然后才将 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位]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 04:48