0

I'm working on an application that should be able to run 1 of 2 scripts, depending on whether or not the OS running the app is x64 or x86.

Searched around and I came across this thread: How to detect Windows 64-bit platform with .NET?

But apparently my boss is afraid the top answer might not work on all OS' our users will be running (XP/Vista/7). He recommended this code sample:

    private void GetCpuDetails(out string cpuType)
    {
        cpuType = "..."; 

        try
        {
            using (RegistryKey regKey = Registry.LocalMachine)
            {
                using (RegistryKey subRegKey = regKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"))
                {
                    if (subRegKey.GetValue("ProcessorNameString") != null)
                    {
                        cpuType = subRegKey.GetValue("ProcessorNameString").ToString();
                    }

                    subRegKey.Close();
                }
                regKey.Close();
            }
        }
        catch
        {
            cpuType = "...";
        }
    }

But I don't understand how you could possibly determine the OS version from the CPU. This seems to be exactly the same conundrum as using PROCESSOR_ARCHITECTURE in that it you'd get 64 or 32 bit based on the CPU, not the OS.

Community
  • 1
  • 1
sab669
  • 3,984
  • 8
  • 38
  • 75
  • i think you can use `System.Environment.Is64BitOperatingSystem` property it will return true if os is 64 bit otheriwise false. – Sudhakar Tillapudi Nov 21 '13 at 14:55
  • @Sudhakar That's new in .NET 4.0, doesn't exist in .NET 3.5 – sab669 Nov 21 '13 at 14:56
  • :Sorry i did't see the Framework tag. Thanks for your point. – Sudhakar Tillapudi Nov 21 '13 at 14:57
  • > "`our users will be running (XP/Vista/7)`". You're still supporting XP? Are you aware that it reaches end of life in less than 6 months, and then Microsoft will cease all support, including for critical security issues? It's time to move away from XP _now_. – Joel Coehoorn Nov 21 '13 at 15:09
  • @JoelCoehoorn Trust me, I'm not thrilled with it either. But when you work for a huge corporation with well over 100K employees, not everyone can have the latest and greatest hardware or software. Hell, my boss is still on XP himself. – sab669 Nov 21 '13 at 15:23

2 Answers2

5

No, it is not possible to determine this from the CPU alone. 64bit CPUs are designed to also run 32bit operating systems, and so knowing what kind of CPU you have is not enough to know what kind of operating system you have.

This is especially true, because nearly all CPUs made in the last several years are 64bit, and it's been this way for long enough now that it's beginning to be unusual (and getting more so all the time) to find 32-bit PCs still in service. However, many PC vendors still ship with 32-bit Windows pre-installed. I wouldn't be surprised to learn that for a purely random sample of PCs, checking the CPU type to learn about the operating system is currently more likely to give you the wrong answer than the right one.

The best method I've seen for detecting the Operating System type is described by Raymond Chen and adpated for C# in this Stack Overflow question, that you have already seen:

How to detect Windows 64-bit platform with .NET?

That is the correct way to do this for .Net prior to .Net 4.0. For 4.0 and later, you can of course just check System.Environment.Is64BitOperatingSystem as mentioned in a comment.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

Why you don't just check IntPtr.Size to determine if your app is running on x64 or x86 mode?

It will mislead you if you are on WoW64, but maybe that could be fine on your scenario?

To clarify: Look in example at Antonijn's response in Load x64 or a x86 DLL depending upon the platform?

Type interop = (IntPtr.Size == 8) ? typeof(Foo64) : typeof(Foo32);

Or at May's response:

(IntPtr.Size == 4) ? "x86" : "x64"

I've used that approach many times.

Community
  • 1
  • 1
aledeniz
  • 431
  • 3
  • 13
  • Unfortunately, because of a third party library we're using I'm forced to set the Build Target as x86, so IntPtr.Size is 4 regardless of the platform it's actually running on. If it could be AnyPlatform I'm sure this would work, though my boss seems to have a problem with this too... – sab669 Nov 21 '13 at 16:22