检测cpu架构的正确方法?

发布于 2024-08-17 00:49:25 字数 643 浏览 2 评论 0 原文

我正在尝试检测用于安装 x86 msi 或 x64 msi 文件的正确 cpu 架构。

如果我是对的,对于 msi,我需要操作系统 cpu 架构,

我不完全确定我的方法是否正确,因为我无法测试它。 你怎么认为?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九公里浅绿 2024-08-24 00:49:25

您可以 P/Invoke 到 GetNativeSystemInfo,这将提供操作系统的 CPU 架构,即使是在 64 位操作系统上的 32 位进程中也是如此。

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.

奢华的一滴泪 2024-08-24 00:49:25

正确的方法是调用 IsWow64Process。不过,此 API“需要 Windows XP SP2、Windows Vista、Windows Server 2003 SP1 或 Windows Server 2008”。 此方法更简单。

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.

站稳脚跟 2024-08-24 00:49:25

很简单,尝试执行 64 位应用程序。如果失败,说明您使用的是 32 位平台。

编辑添加,根据您想要执行的操作,如果您确保您的 msi 运行程序应用程序是 32 位应用程序,则使用 Stuart 的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文