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 "";
    }
3
  • 3
    Are you really going to ship an installer without testing it first? That's courageous. Commented Jan 7, 2010 at 1:04
  • I can test it but just on one x64 machine and one x32 machine not on every cpu type...that's the problem Commented Jan 7, 2010 at 1:05
  • any final solution about it ? Commented Jan 10, 2012 at 16:04

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

And that's what this gives you.
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.
Tweaked answer again. On an x64-capable CPU running 32-bit OS, this function will return that the system has an x86 system.
Next you need to tweak MSDN :-)
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.

5 Comments

That won't tell you what the CPU arch is, only what the process itself is running as.
Do you need the OS architecture or the actual CPU architecture?
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.
...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.
@stuart: the intptr size method seems to be simple but will fail on WinNT.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.