5

I have an ASP.NET server that I do not have direct access to. How can I write a .NET application that will programmatically determine whether ASP.NET is running 32 bit vs. 64 bit?

David Basarab
  • 72,212
  • 42
  • 129
  • 156
RationalGeek
  • 9,425
  • 11
  • 62
  • 90

3 Answers3

4

Already answered here:

How do I tell if my application is running as a 32-bit or 64-bit application?

Community
  • 1
  • 1
Badaro
  • 3,460
  • 1
  • 19
  • 18
3

The easiest way is to do this:

Int32 addressWidth = IntPtr.Size * 8;

since IntPtr.Size is 4 bytes on 32-bit architecture and 8 bytes on 64-bit architecture.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
1

You can use PInvoke

This is a code sample found here.

private enum Platform
{
    X86,
    X64,
    Unknown
}

internal const ushort PROCESSOR_ARCHITECTURE_INTEL = 0;
internal const ushort PROCESSOR_ARCHITECTURE_IA64 = 6;
internal const ushort PROCESSOR_ARCHITECTURE_AMD64 = 9;
internal const ushort PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF;

[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
    public ushort wProcessorArchitecture;
    public ushort wReserved;
    public uint dwPageSize;
    public IntPtr lpMinimumApplicationAddress;
    public IntPtr lpMaximumApplicationAddress;
    public UIntPtr dwActiveProcessorMask;
    public uint dwNumberOfProcessors;
    public uint dwProcessorType;
    public uint dwAllocationGranularity;
    public ushort wProcessorLevel;
    public ushort wProcessorRevision;
};

[DllImport("kernel32.dll")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);        

private static Platform GetPlatform()
{
    SYSTEM_INFO sysInfo = new SYSTEM_INFO();
    GetNativeSystemInfo(ref sysInfo);

    switch (sysInfo.wProcessorArchitecture)
    {
        case PROCESSOR_ARCHITECTURE_AMD64:
            return Platform.X64;

        case PROCESSOR_ARCHITECTURE_INTEL:
            return Platform.X86;

        default:
            return Platform.Unknown;
    }
}
David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • This is a good solution if your .NET app is running with a forced x86 compile flag for other reasons, but you still need to determine if the host system is 64 bit. – Mike Dec 17 '09 at 13:54