While working on some installer code I ran into a problem where my C# code was called by msiexec.exe on an x64 operating system. Msiexec.exe runs as a 32 bit process on 64 bit systems so using (IntPtr.Size == 8) or IsWow64Process won't work, the solution is to use GetSystemWow64Directory, if it returns 0 it fails and you are running on a 32 bit system, if it's non zero you are running on a 64 bit system.
[DllImport("Kernel32.dll")]
public static extern int GetSystemWow64Directory( [MarshalAs(UnmanagedType.LPStr, SizeConst=256)] [In,Out] StringBuilder lpBuffer, [MarshalAs(UnmanagedType.U4)] uint size); public static bool Is64BitOs()
{
StringBuilder path = new StringBuilder(256);
int result = GetSystemWow64Directory(path, (uint)path.Length);
return result != 0;
}