Folgende Klasse bestimmt die Plattform auf welcher der akt. Prozess ausgeführt wird.
using System;
/// <summary>
/// freeware helper class for getting platform info
/// (W) 2011 by admin of codezentrale.de
/// </summary>
public static class PlatFormDetect
{
private const string sX86 = "x86";
private const string sAMD64 = "AMD64";
public enum EPlatformType
{
Unknown,
Native32Bit, // 32Bit process on 32Bit OS
Native64Bit, // 64Bit process on 64Bit OS
WOW64 // 32Bit process on 64Bit OS
}
/// <summary>
/// returns the current platform type
/// </summary>
public static EPlatformType PlatformType
{
get
{
EPlatformType ePT = EPlatformType.Unknown;
// get native process bitness
// x86 - 32bit native AND WOW64
// AMD64 - 64bit native
string sArchitecture = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine);
// get original CPU architecture
// undefined (null) == 32Bit
// AMD64 == 64Bit
string sArchitectureW6432 = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432", EnvironmentVariableTarget.Machine);
// 32Bit process on 32Bit OS
if ((sArchitecture == sX86) && string.IsNullOrEmpty(sArchitectureW6432)) ePT = EPlatformType.Native32Bit;
// 64Bit process on 64Bit OS
if ((sArchitecture == sAMD64) && string.IsNullOrEmpty(sArchitectureW6432)) ePT = EPlatformType.Native64Bit;
// 32Bit process on 64Bit OS
if ((sArchitecture == sX86) && (sArchitectureW6432 == sAMD64)) ePT = EPlatformType.WOW64;
return ePT;
}
}
}
Ab .NET-Framework 4.0 stehen die Funktionen Environment.Is64BitProcess und Environment.Is64BitOperatingSystem zur Verfügung.
using System;
// helper class for getting platform info
// (W) 2010 by admin of codezentrale.de
public static class PlatFormDetect4
{
private const string s32Bit = "32Bit";
private const string s64Bit = "64Bit";
/// <summary>
/// prints the OS bitness
/// </summary>
/// <returns>OS bitness</returns>
public static string OSBitness
{
get
{
return Environment.Is64BitOperatingSystem ? s64Bit : s32Bit;
}
}
/// <summary>
/// prints the process bitness
/// </summary>
/// <returns>process bitness</returns>
public static string ProcessBitness
{
get
{
return Environment.Is64BitProcess ? s64Bit : s32Bit;
}
}
}