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

发布于 2024-08-26 11:45:10 字数 56 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小傻瓜 2024-09-02 11:45:10
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;
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;
傾旎 2024-09-02 11:45:10

您需要使用 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 安装上无法更改。从多个线程同时调用此函数甚至是安全的,因为发现 WasCalledFalse 的两个线程都会调用该函数,将相同的结果写入相同的内存位置,然后才将 WasCalled 设置为 True

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;

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.

Edit:

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;

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.

丶视觉 2024-09-02 11:45:10

如果 a) 您使用的是 Windows,并且 b) 您可以访问注册表,则 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion 应该提供信息。

If a) you're on windows and b) you can access the registry then HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion should be informative.

淡紫姑娘! 2024-09-02 11:45:10
function TForm2.Arch: string;
begin
if TOSVersion.Architecture=arIntelX86 then
   Result := '32-bit' Else  Result := '64-bit'
end;
function TForm2.Arch: string;
begin
if TOSVersion.Architecture=arIntelX86 then
   Result := '32-bit' Else  Result := '64-bit'
end;
冷情妓 2024-09-02 11:45:10

除了 IsWow64Process 之外,您可能会对 GetNativeSystemInfo API 函数(它在 Windows 单元中定义)感兴趣,以了解有关您所使用的 CPU 的更多信息(或者您可以使用程序集和 CPUID )。

In addition to IsWow64Process, the GetNativeSystemInfo API function may be of interest to you (it's defined in the Windows unit) to find out more about the CPU you're on (or you can use assembly and CPUID).

自由范儿 2024-09-02 11:45:10

对于delphi XE+

Uses System.SysUtils

Function IsWin64Or32: string;
Begin
   if Pos( '64-bit', TOSVersion.ToString ) > 0 then
     Result := '64-bit'
   Else
     Result := '32-bit';
End;

示例

lbl1.Caption := IsWin64Or32;

for delphi XE+

Uses System.SysUtils

Function IsWin64Or32: string;
Begin
   if Pos( '64-bit', TOSVersion.ToString ) > 0 then
     Result := '64-bit'
   Else
     Result := '32-bit';
End;

Example

lbl1.Caption := IsWin64Or32;
明月松间行 2024-09-02 11:45:10

我不知道如何在Delphi中调用Win32函数。

但如果你编写32位程序,你可以调用Win32 API IsWow64Process来知道你是否在64位操作系统中。

当然,如果你写的是64位的exe,那么它只能在64位Windows上运行,所以就不用问了。

I don't know how to call Win32 function in Delphi.

But if you write a 32-bit program, you can call the Win32 API IsWow64Process to know if you are in a 64-bit OS.

Of course, if you write a 64-bit exe, it will only run on 64-bit Windows, so there is no need to ask.

流绪微梦 2024-09-02 11:45:10

//尚未测试,但你可以尝试这个

is64 := (Environment.GetEnvironmentVariable('ProgramW6432') <> '');

//not tested but u can try this

is64 := (Environment.GetEnvironmentVariable('ProgramW6432') <> '');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文