W .Net Framework 4.0 klasa Environment oferuje właściwość Is64BitOperatingSystem, której nie ma w poprzednich wersjach. Sprawdza ona czy biblioteka kernel32.dll posiada niezerowy wskaźnik do metody IsWow64Process. Jeżeli tak to sprawdzane jest czy wywołanie tej metody z podaniem wskaźnika do procesu naszej aplikacji zwróci niezerowy wskaźnik.
namespace MY_NAMESPACE {
using System;
using System.Security;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
[SuppressUnmanagedCodeSecurity,
SecurityCritical]
internal static class Win32Native {
[SecuritySafeCritical]
public static bool Is64BitOperatingSystem() {
bool flag;
return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") &&
IsWow64Process(GetCurrentProcess(), out flag)) && flag);
}
[SecurityCritical]
static bool DoesWin32MethodExist(string moduleName, string methodName) {
IntPtr moduleHandle = GetModuleHandle(moduleName);
if (moduleHandle == IntPtr.Zero) {
return false;
}
return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool IsWow64Process([In] IntPtr hSourceProcessHandle,
[MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern IntPtr GetCurrentProcess();
[ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail),
DllImport("kernel32.dll",
CharSet=CharSet.Auto, SetLastError=true)]
static extern IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll",
CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string methodName);
}
}
Przydatne linki
IsWow64Process
GetProcAddress
GetModuleHandle
GetCurrentProcess