tags:

views:

9524

answers:

8

Believe it or not my installer is so old that it doesn't have an option to detect the 64 bit version of Windows.

Is there a Win DLL call or (even better) an environment variable that would give that info for XP and Vista?

One possible solution

I see that Wikipedia states that the 64 bit version of XP and Vista have a unique Env Variable: * %ProgramW6432%* so I'm guessing that'd be empty on 32 bit windows.

This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).

+18  A: 

Refer to:

cletus
The second link isn't helpful. The solution it shows only detects whether the program itself was compiled to x64 or not.
Jader Dias
Jader: not true. Check again, it's the code for both cases, if you compile for x64 (nothing to solve) and x86 (using IsWow64Process()).
Miro Kropacek
+6  A: 

If you can make API calls, try using GetProcAddress / GetModuleHandle to check for the existence of IsWow64Process which is only present in Windows OS that have 64-bit versions.

You could also try the ProgramFiles(x86) environment variable used in Vista/2008 for backwards compatibility, but I'm not 100% sure about XP-64 or 2003-64.

Good luck!

Jason
PG(x86) is in at least XP-64 and since XP-64 is more related to 2003-64 than, say, XP original I'd bet it's in 2003-64 too.
Esko
+3  A: 

I don't know what language you're using, but .Net has the environment variable: PROCESSOR_ARCHITEW6432 if the OS is 64-bit.

If all you want to know is whether your application is running 32-bit or 64-bit, you can check IntPtr.Size. It will be 4 if running in 32-bit mode and 8 if running in 64-bit mode.

Andrew
This option only works in .net. I need an option that'll work with my installer.
Clay Nichols
If you force the project to 32-bit (e.g. Project->Propterties and change platform target to x86), the IntPtr.Size value will be 4.
Jader Dias
@Jader: Right you are. I didn't realize how ambiguous my answer was before, but it was obvious as soon as you pointed it out. I've edited my answer for clarity.
Andrew
A: 

In C#:

public bool Is64bit() {
    return Marshal.SizeOf(typeof(IntPtr)) == 8;
}

In VB.Net:

Public Sub Is64bit() As Boolean
    Is64bit = Marshal.SizeOf(GetType(IntPtr)) = 8
End Sub
Renaud Bompuis
That detects if the application is a 64-bit app. If you force it to 32-bit (e.g. Project->Propterties and change platform target to x86), the return value will be 4.
Raymond Martineau
This is a good, simple detection within a .Net application.
Christopher_G_Lewis
+7  A: 

I tested the solution I suggested in my question:

Tested for Windows Environment Variable: ProgramW6432

If it's non empty then it's 64 bit Windows.W

Clay Nichols
The environment variable you listed in your question was ProgramW6432. Here, you list PROCESSOR_ARCHITEW6432. Which one did you use?
Andrew
Thanks Andrew. I found out about this mistake the hard way: when I had someone test it ;-). Should have been: ProgramW6432 .Fixing it now.
Clay Nichols
I don't have either of these environment variables (ProgramW6432 PROCESSOR_ARCHITEW6432), on my Vista 64. I do have PROCESSOR_ARCHITECTURE, which is set to x86 or AMD64
Mike
Thanks Mike! Now, if I can just figure out if Processor_Architecture is *always* present on Win 64.
Clay Nichols
Except for PROCESSOR_ARCHITE*, these env variables were added in Win7/Server2008: http://msdn.microsoft.com/en-us/library/aa384274(VS.85).aspx
total
+3  A: 

This has been answered pretty well already, but since the question and current answers helped me come up with my own solution, I'll post it here in the hope that someone else will save some time.

Delphi 7 code to check whether your 32 bit program is running on a 64 bit operating system:

function Is64BitOS: Boolean;
type
  TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall;
var
  hKernel32 : Integer;
  IsWow64Process : TIsWow64Process;
  IsWow64 : BOOL;
begin
  // we can check if the operating system is 64-bit by checking whether
  // we are running under Wow64 (we are 32-bit code). We must check if this
  // function is implemented before we call it, because some older versions
  // of kernel32.dll (eg. Windows 2000) don't know about it.
  // see http://msdn.microsoft.com/en-us/library/ms684139%28VS.85%29.aspx
  Result := False;
  hKernel32 := LoadLibrary('kernel32.dll');
  if (hKernel32 = 0) then RaiseLastOSError;
  @IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process');
  if Assigned(IsWow64Process) then begin
    IsWow64 := False;
    if (IsWow64Process(GetCurrentProcess, IsWow64)) then begin
      Result := IsWow64;
    end
    else RaiseLastOSError;
  end;
  FreeLibrary(hKernel32);
end;
Blorgbeard
+4  A: 

To check for 64bit version of Windows in a command box I use the following template:

test.bat:

@echo off
if defined ProgramFiles(x86) (
@echo yes
@echo Some 64-bit work
) else (
@echo no
@echo Some 32-bit work
)
Dror Harari
+2  A: 

I used this within a login script to detect 64 bit Windows

if "%ProgramW6432%" == "%ProgramFiles%" goto is64flag

TallGuy