Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a good way to determine OS Architecture?

Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system.

private Boolean is64BitOperatingSystem()
{
    RegistryKey localEnvironment = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
    String processorArchitecture = (String) localEnvironment.GetValue("PROCESSOR_ARCHITECTURE");

    if (processorArchitecture.Equals("x86")) {
        return false;
    }
    else {
        return true;
    }
}

It's worked out pretty well for us so far, but I'm not sure how much I like looking through the registry. Is this a pretty standard practice or is there a better method?

Edit: Wow, that code looks a lot prettier in the preview. I'll consider linking to a pastebin or something, next time.

like image 971
Jeremy Privett Avatar asked Aug 06 '08 19:08

Jeremy Privett


4 Answers

Take a look at Raymond Chens solution:

How to detect programmatically whether you are running on 64-bit Windows

and here's the PINVOKE for .NET:

IsWow64Process (kernel32)

Update: I'd take issue with checking for 'x86'. Who's to say what intel's or AMD's next 32 bit processor may be designated as. The probability is low but it is a risk. You should ask the OS to determine this via the correct API's, not by querying what could be a OS version/platform specific value that may be considered opaque to the outside world. Ask yourself the questions, 1 - is the registry entry concerned properly documented by MS, 2 - If it is do they provide a definitive list of possible values that is guaranteed to permit you as a developer to make the informed decision between whether you are running 32 bit or 64 bit. If the answer is no, then call the API's, yeah it's a but more long winded but it is documented and definitive.

like image 94
Kev Avatar answered Sep 20 '22 06:09

Kev


The easiest way to test for 64-bit under .NET is to check the value of IntPtr.Size.

I believe the value of IntPtr.Size is 4 for a 32bit app that's running under WOW, isn't it?

Edit: @Edit: Yeah. :)

like image 36
Jeremy Privett Avatar answered Sep 21 '22 06:09

Jeremy Privett


The easiest way to test for 64-bit under .NET is to check the value of IntPtr.Size.

EDIT: Doh! This will tell you whether or not the current process is 64-bit, not the OS as a whole. Sorry!

like image 38
Curt Hagenlocher Avatar answered Sep 18 '22 06:09

Curt Hagenlocher


Looking into the registry is perfectly valid, so long as you can be sure that the user of the application will always have access to what you need.

like image 27
Greg Hurlman Avatar answered Sep 22 '22 06:09

Greg Hurlman