4

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 "";
    }
Kai
  • 5,850
  • 13
  • 43
  • 63

3 Answers3

3

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
  • 54,279
  • 5
  • 125
  • 144
0

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.

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 1
    That won't tell you what the CPU arch is, only what the process itself is running as. – mletterle Jan 07 '10 at 00:51
  • Do you need the OS architecture or the actual CPU architecture? – Nathan Osman Jan 07 '10 at 00:54
  • 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 Jan 07 '10 at 00:58
  • ...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 Jan 07 '10 at 00:59
  • @stuart: the intptr size method seems to be simple but will fail on WinNT. – Kai Jan 07 '10 at 01:06
0

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
  • 3,968
  • 1
  • 24
  • 24