11

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

Is there any API available?

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.

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

Community
  • 1
  • 1
Santhosh
  • 6,547
  • 15
  • 56
  • 63

5 Answers5

9

Sys::Info looks promising:

#!/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 incorrectly identifies my laptop's CPU as being 64 bit (see Intel® Core™ Duo Processor T2300—bug report filed).

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • 1
    I have checked this on 32 bit Windows 7 on a 64 bit processor. Works like a breeze. Will check this on 64 bit windows 7 on 64 bit processor when i get to work on monday. – Santhosh Jan 09 '10 at 13:21
  • When i test this on Windows 7 64 bit using 32 bit Perl installer (installing in WOW mode), i see the bitness as 32 surprisingly !! And then i uninstalled 32 bit perl and installed 64 bit perl but there is a problem with it - i cannot download and install the Sys-Info package "ppm install Sys-Info" says that Sys-Info is not found. Any comments on how i can do this in 64 bit Windows? – Santhosh Jan 12 '10 at 13:39
3

Perhaps you can just check some environment variables:

See HOWTO: Detect Process Bitness.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
kolbyjack
  • 17,660
  • 5
  • 48
  • 35
3

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

 #!/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;
}
mihai
  • 37,072
  • 9
  • 60
  • 86
Slid3r
  • 31
  • 1
2

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

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)

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";
}
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • Don't forget that the value could be "IA64" instead of "AMD64". So the if statement gets a bit more complex. – Jason Mar 03 '15 at 00:40
-1

The PROCESSOR_ARCHITECTURE variable is "x86" in 32bits

Max Caceres
  • 1,976
  • 3
  • 18
  • 18
  • 1
    And in 64bit, if running a 32bit process. – Brian Jan 08 '10 at 20:09
  • This is not a correct answer to the OP. See the [answer](http://stackoverflow.com/a/8406831/361855) given by Mark Lakata and the link to the MSDN blog that explains how to use this environment variable to detect bitness. Here's the link for quick reference: http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx – Jason Mar 03 '15 at 00:36