Skip to content
Advertisement

Check architecture for Linux and Mac

I’m trying to get the architecture of the computer executing my software, and so far it is working nice on Windows, but it fails on Linux and OSX. Let me show my current code:

//--Check processor architecture--
string strProcArchi = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
string strProcArchiBis = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432");

if((strProcArchi != null && strProcArchi.IndexOf("64") > 0) || (strProcArchiBis != null && strProcArchiBis.IndexOf("64") > 0))
{
    Console.WriteLine("Processor is 64-bit");
}
else
{
    Console.WriteLine("Processor is 32-bit");
}
//------

Linux and Mac always return “Processor is 32-bit”, so I guess that those environment variables are unused outside Windows. I’ve been googling for a couple of days but can’t find anything specific. Any ideas on how to achieve this? I need to load different assets whether the computer that’s executing it is 32-bit or 64-bit based, so this check is definitely important.

I’m under .NET 2.0 and I can’t use Environment.Is64BitOperatingSystem or Environment.Is64BitProcess since that’s a .NET 4.0 feature.

Thanks in advance.

Advertisement

Answer

To detect bitness of your .NET runtime process (mono process in your case) you can check IntPtr.Size == 8 and to detect bitness of your processor you can parse the output of sysctl hw.cpu64bit_capable command on OSX and the output of cat /proc/cpuinfo | grep "flags" | grep " lm " command on Linux.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement