I'm attempting to detect the right cpu architecture for installing either a x86 msi or x64 msi file.
If I'm right, for the msi I need the os cpu architecture
I'm not totally sure if my way is right because I can't test it. 开发者_JAVA技巧 What do you think?
private static string GetOSArchitecture()
{
string arch = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
string archWOW = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432");
if(archWOW != null && archWOW != "" && archWOW.Contains("64"))
return "x64";
if(arch.Contains("86"))
return "x86";
if (arch.Contains("64"))
return "x64";
return "";
}
You could P/Invoke to GetNativeSystemInfo, which will give the CPU architecture of the OS, even from within a 32-bit process on a 64-bit OS.
The right way is to call IsWow64Process. This API "Requires Windows XP SP2, Windows Vista, Windows Server 2003 SP1 or Windows Server 2008" though. This method is even easier.
Simple, try executing a 64bit application. If it fails you're on a 32bit platform.
Edited to add, based on what you're trying to do, if you make sure your msi runner application is a 32bit app then use Stuart's method.
精彩评论