Right way to detect cpu architecture?

一曲冷凌霜 提交于 2019-12-01 04:26:45

问题


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. 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 "";
    }

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2017409/right-way-to-detect-cpu-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!