views:

451

answers:

3

I'm attempting to detect the right cpu architecture for installing either a x86 msi or x64 msi file.

If I'm right, for the msi I need the os cpu architecture

I'm not totally sure if my way is right because I can't test it. What do you think?

private static string GetOSArchitecture()
    {
        string arch = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
        string archWOW = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432");
        if(archWOW != null && archWOW != "" && archWOW.Contains("64"))
            return "x64";
        if(arch.Contains("86"))
            return "x86";
        if (arch.Contains("64"))
            return "x64";
        return "";
    }
A: 

The right way is to call IsWow64Process. This API "Requires Windows XP SP2, Windows Vista, Windows Server 2003 SP1 or Windows Server 2008" though. This method is even easier.

Stuart Dunkeld
That won't tell you what the CPU arch is, only what the process itself is running as.
mletterle
Do you need the OS architecture or the actual CPU architecture?
George Edison
I need that architecture which is important for running the right msi. the code is part of an updater that downloads the msi and executes it.
Kai
...as you might know there's no msi that runs on 32bit and 64bit cpus. due to that I need to choose the right one which i'm providing on the server.
Kai
@stuart: the intptr size method seems to be simple but will fail on WinNT.
Kai
A: 

Simple, try executing a 64bit application. If it fails you're on a 32bit platform.

Edited to add, based on what you're trying to do, if you make sure your msi runner application is a 32bit app then use Stuart's method.

mletterle
no good idea, because I'm downloading just one file not both...
Kai
+2  A: 

You could P/Invoke to GetNativeSystemInfo, which will give the CPU architecture of the OS, even from within a 32-bit process on a 64-bit OS.

Michael
If I'm right, for the msi I need the os cpu architecture
Kai
And that's what this gives you.
Michael
A 32-bit Windows system can be installed on a 64-bit CPU. Both MSDN and your answer "the actual CPU architecture" tell me that GetNativeSystemInfo reports x64, but Kai needs to be told x86 in this case.
Windows programmer
Tweaked answer again. On an x64-capable CPU running 32-bit OS, this function will return that the system has an x86 system.
Michael
Next you need to tweak MSDN :-)
Windows programmer