如何在 Windows 上使用 Perl 确定操作系统的位数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2030039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:43:19  来源:igfitidea点击:

How can I determine the bitness of the OS using Perl on Windows?

windowsperl64-bit

提问by Santhosh

Using Perl, how can I determine whether my program is running on 32 bit Windows or 64 bit Windows?

使用 Perl,我如何确定我的程序是在 32 位 Windows 还是 64 位 Windows 上运行?

Is there any API available?

有没有可用的API?

I can think of a couple of options..

我可以想到几个选择。

  1. Check the PE_HEADER of some windows file (eg: c:\windows\explorer.exe) - maybe I can use the details in How can I test a windows dll to determine if it is 32bit or 64bit?

  2. Check for the existence of c:\program files(x86)- if it exists then it is a 64 bit OS. Else it is a 32 bit windows OS.

  1. 检查某些 Windows 文件(例如:)的 PE_HEADER c:\windows\explorer.exe- 也许我可以使用如何测试 Windows dll 以确定它是 32 位还是 64 位中的详细信息?

  2. 检查是否存在c:\program files(x86)- 如果存在,则它是 64 位操作系统。否则它是一个 32 位 Windows 操作系统。

Is there any good way of doing this? Any API available in Perl?

有什么好的方法可以做到这一点吗?Perl 中有可用的 API 吗?

采纳答案by Sinan ünür

Sys::Infolooks promising:

Sys::Info看起来很有希望:

#!/usr/bin/perl

use strict; use warnings;
use Sys::Info;

my $info = Sys::Info->new;

my $cpu = $info->device('CPU');

printf "%s (%s bit)\n", scalar $cpu->identify, $cpu->bitness;

my $os = $info->os;

printf "%s (%s bit)\n", $os->name(long => 1), $os->bitness;

Output:

输出:

C:\Temp> t
Genuine Intel(R) CPU T2300 @ 1.66GHz (64 bit)
Windows XP Service Pack 3 build 2600 (32 bit)

Note that it incorrectlyidentifies my laptop's CPU as being 64 bit (see Intel? Core? Duo Processor T2300—bug report filed).

请注意,它错误地将我的笔记本电脑的 CPU 识别为 64 位(参见Intel?Core?Duo Processor T2300—提交的错误报告)。

回答by Slid3r

Testing for the existence of HKEY_LOCAL_MACHINE\Software\Wow6432Node is the most reliable method

测试HKEY_LOCAL_MACHINE\Software\Wow6432Node是否存在是最可靠的方法

 #!/usr/bin/perl

use strict; 
use Win32::Registry;

my $bitReturn = &bitter();

print "OS Bit: $bitReturn \n";

# Testing for the existence of HKEY_LOCAL_MACHINE\Software\Wow6432Node is the most reliable method
sub bitter {
    my $Register = "Software\Wow6432Node";
    my ($hkey,$bitReturn);

    if ($HKEY_LOCAL_MACHINE->Open($Register,$hkey)) {
        $bitReturn = "64";
    }
    else {
        $bitReturn = "32"
    }
    return $bitReturn;
}

Here is another easy method, checking Environment Variables

这是另一种简单的方法,检查环境变量

sub bitter {
     my $bit;
     my $OSbit = `set`;
     if ($OSbit =~ m/Files\(x86\)/i) {
         $bit = "64";
     }
     else {
         $bit = "32";
     }
     return $bit;
}

回答by kolbyHyman

Perhaps you can just check some environment variables:

也许您可以检查一些环境变量:

See HOWTO: Detect Process Bitness.

请参阅HOWTO:检测进程位数

回答by Mark Lakata

MSDN recommends this logic (jeezus, why does this have to be so complicated?) http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx

MSDN推荐这个逻辑(天啊,为什么要这么复杂?) http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx

IF PROCESSOR_ARCHITECTURE == amd64 OR
   PROCESSOR_ARCHITEW6432 == amd64 THEN
   // OS is 64bit
ELSE
   // OS is 32bit
END IF

here's how I used it in my script (note that the MSDN example messes up the capitalization of the variable values, at least on Win7, so I do a case insensitive compare)

这是我在脚本中使用它的方式(请注意,MSDN 示例混淆了变量值的大小写,至少在 Win7 上,因此我进行了不区分大小写的比较)

if (uc($ENV{PROCESSOR_ARCHITECTURE}) eq "AMD64" || 
    uc($ENV{PROCESSOR_ARCHITEW6432}) eq "AMD64") {
    push @impactBinaries,  "C:/Xilinx/13.1/LabTools/LabTools/bin/nt64/impact.exe";
} else {
    push @impactBinaries,  "C:/Xilinx/13.1/LabTools/LabTools/bin/nt/impact.exe";
}

回答by Max Caceres

The PROCESSOR_ARCHITECTURE variable is "x86" in 32bits

PROCESSOR_ARCHITECTURE 变量是 32 位的“x86”