How to determine if we are running on 64 or 32 bit?

In my work I need to be able to pick different files depending on if I run in 64 bit or 32 bit system. The first thing I did try was to use the size of IntPtr which is 4 for 32 bit and 8 for 64 bit. But since I was using the visual studio unit test framework it did not work that god. The unit test framework run in a 32 bit process but the application I’m testing is running in 64 bit, so decided to use P/Invoke to call GetNativeSystemInfo.

The structure that you get is according to

[StructLayout(LayoutKind.Sequential)]
private struct SystemInfoNative
{
    internal ushort ProcessorArchitecture;
    internal ushort Reserved;
    internal uint PageSize;
    internal IntPtr MinimumApplicationAddress;
    internal IntPtr MaximumApplicationAddress;
    internal IntPtr ActiveProcessorMask;
    internal uint NumberOfProcessors;
    internal uint ProcessorType;
    internal uint AllocationGranularity;
    internal ushort ProcessorLevel;
    internal ushort ProcessorRevision;
}

Out from this you can check the processor architecture.

And the complete source code

using System;
using System.Runtime.InteropServices;
 
namespace SystemInfoTesting
{
    internal sealed class SystemInfo
    {
        public enum ProcessorArchitecture
        {
            None = -1,
            Intel = 0,
            IA64 = 6,
            AMD64 = 9
        }
 
        private SystemInfoNative nativeInfo;
 
        private SystemInfo(SystemInfoNative nativeInfo)
        {
            this.nativeInfo = nativeInfo;
        }
 
        public ProcessorArchitecture Architecture
        {
            get
            {
                return (ProcessorArchitecture)Enum.Parse(typeof(ProcessorArchitecture),
                         Enum.GetName(typeof(ProcessorArchitecture),
                         this.nativeInfo.ProcessorArchitecture));
            }
        }
 
        public static SystemInfo GetNativeSystemInfo()
        {
            SystemInfoNative native = new SystemInfoNative();
 
            GetNativeSystemInfo(ref native);
 
            return new SystemInfo(native);
        }
 
        [StructLayout(LayoutKind.Sequential)]
        private struct SystemInfoNative
        {
            internal ushort ProcessorArchitecture;
            internal ushort Reserved;
            internal uint PageSize;
            internal IntPtr MinimumApplicationAddress;
            internal IntPtr MaximumApplicationAddress;
            internal IntPtr ActiveProcessorMask;
            internal uint NumberOfProcessors;
            internal uint ProcessorType;
            internal uint AllocationGranularity;
            internal ushort ProcessorLevel;
            internal ushort ProcessorRevision;
        }
 
        [DllImport("kernel32.dll")]
        private static extern void GetNativeSystemInfo(ref SystemInfoNative lpSystemInfo);
 
    }
}
0.00 avg. rating (0% score) - 0 votes

About Peter Wibeck

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

*