growled on Saturday, October 13, 2007 8:41:35 AM (Pacific Standard Time, UTC-08:00)
barked at code [.net]

Just the other day, I wanted to detect if an application is running on 32 or 64 bit (x86 versus x64) Windows operating system so I could display it in a label control.  I thought to myself "ok that should be easy with a registry key or a file version of a system file," but I didn't know a definitive source so off I go to the wild, wild web.  A few searches [1, 2, 3] yielded no useful results for doing this programmatically [and yes, I tried Google too ;-)]. 

So I turned to my brother who has been working with operating system deployments in our data centers for the past 10 years [probably what I should have done in the first place].  Just as I thought, there is a registry key that gives you this information. Since I couldn't find it easily online, I'll add it here.

HKLM\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE

Adding this to a .NET C# application is only a few lines of code (using Microsoft.Win32 namespace):

private string GetProcessorArchitecture()
{
    RegistryKey environmentKey = Registry.LocalMachine;  // points to HKLM hive
    environmentKey = environmentKey.OpenSubKey
        (@"System\CurrentControlSet\Control\Session Manager\Environment", false);
    string strEnvironment = 
        environmentKey.GetValue("PROCESSOR_ARCHITECTURE").ToString();
    return strEnvironment;
}

So there you go...how to determine whether the architecture of the operating system is 32-bit or 64-bit.

~tod

Monday, October 15, 2007 6:01:40 PM (Pacific Standard Time, UTC-08:00)
This should also work:

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
Al Gonzalez
Monday, October 15, 2007 6:40:12 PM (Pacific Standard Time, UTC-08:00)
Al- Cool, I didn't even think of checking the environment variables.
Wednesday, December 05, 2007 11:33:32 AM (Pacific Standard Time, UTC-08:00)
Actually, if I may suggest, neither is a perfect solution because then you may have to deal with parsing a vendor-specific Processor Architecture string. For example, on my 64bit HP 4400 machine, both approaches return the string "AMD64".

Perhaps a better approach:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT AddressWidth FROM Win32_Processor");
ManagementObjectCollection collection = searcher.Get();
bool is64Bit = (UInt16)(obj(0)["AddressWidth"]) == (UInt16)64;

Of course, that foreach loop isn't perfect, but you get the idea.

Regards,
\Steven
Steven Cohn
Wednesday, December 05, 2007 11:34:04 AM (Pacific Standard Time, UTC-08:00)
Oops, right. There is no foreach loop there! \S
Steven Cohn
Wednesday, December 05, 2007 11:45:18 AM (Pacific Standard Time, UTC-08:00)
Steven- Yep, since my method returns a string you would still have to parse it for evaluation before proceeding. Hold on...I think I see a foreach loop there...oh wait, nope. :)
Comments are closed.