C# 如何确定正在执行的程序是否为 64 位编译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/635853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:11:55  来源:igfitidea点击:

How to determine if the executing program was compiled for 64 bit?

c#64-bit

提问by Steve

I need for my program to determine if it was compiled for 32 bit windows or 64 bit/any CPU. I need to choose a different COM server flag depending on the compilation options. Any ideas?

我需要让我的程序确定它是为 32 位 Windows 还是 64 位/任何 CPU 编译的。我需要根据编译选项选择不同的 COM 服务器标志。有任何想法吗?

采纳答案by Brian R. Bondy

Checking at run time if you are running a 64-bit application:

在运行时检查您是否正在运行 64 位应用程序:

To see whether your process is 64-bit or 32-bit you can simply check the IntPtr.Sizeor any other pointer type. If you get 4 then you are running on 32-bit. If you get 8 then you are running on 64-bit.

要查看您的进程是 64 位还是 32 位,您只需检查IntPtr.Size或任何其他指针类型。如果你得到 4,那么你正在 32 位上运行。如果你得到 8,那么你在 64 位上运行。

What type of computer is your process running as:

您的进程运行的是哪种类型的计算机:

You can check the PROCESSOR_ARCHITECTURE environment variable to see if you are running on an x64, ia64 or x86 machine.

您可以检查 PROCESSOR_ARCHITECTURE 环境变量以查看您是否在 x64、ia64 或 x86 机器上运行。

Is your process running under emulation:

您的进程是否在仿真下运行:

The Win32 API IsWow64Process will tell you if you are running a 32-bit application under x64. Wow64 means windows32 on windows64.

Win32 API IsWow64Process 会告诉您是否在 x64 下运行 32 位应用程序。Wow64 表示 windows64 上的 windows32。

Contrary to what other people have posted: IsWow64Process will not tell you if you are running a 32-bit or a 64bit application. On 32-bit machines it will tell you FALSE for 32-bit applications when run, and on a 64-bit machine it will tell you TRUE for a 32-bit application. It only tells you if your application is running under emulation.

与其他人发布的内容相反:IsWow64Process 不会告诉您运行的是 32 位还是 64 位应用程序。在 32 位机器上,它会在运行时告诉你 32 位应用程序的 FALSE,而在 64 位机器上它会告诉你 32 位应用程序的 TRUE。它只会告诉您您的应用程序是否在仿真下运行。

回答by Walden Leverich

You want the CORFLAGS command-line app from Microsoft. http://msdn.microsoft.com/en-us/library/ms164699(VS.80).aspx

您需要来自 Microsoft 的 CORFLAGS 命令行应用程序。http://msdn.microsoft.com/en-us/library/ms164699(VS.80).aspx

回答by Chris Hynes

You could do this based on setting compile time constants and doing #ifs based on those to set a value that you can check to see which platform the application was compiled for. See Target platform/processor at compile time.

您可以基于设置编译时间常量并根据这些常量执行 #ifs 来设置一个值,您可以检查该值以查看应用程序是针对哪个平台编译的。请参阅编译时的目标平台/处理器

The easiest other way is to check the IntPtr.Size property to see if it's 4 (32 bit), or 8 (64 bit).

最简单的另一种方法是检查 IntPtr.Size 属性以查看它是 4(32 位)还是 8(64 位)。

回答by MarkusQ

Take a look at conditional methods.

看看条件方法

回答by Stefan

You can use the IsWow64Processfunction to determine at runtime whethehr your COM server is a x64 or win32 process.

您可以使用IsWow64Process函数在运行时确定您的 COM 服务器是 x64 还是 win32 进程。

If you just want to know this during compile time: the compiler sets the _WIN64 macro.

如果您只想在编译期间知道这一点:编译器设置 _WIN64 宏。

回答by Powerlord

While it seems like an odd route to go, you can tell whether you're running in 32-bit (or 64-bit in WOW64) or 64-bit .NET code by checking IntPtr.Size.

虽然这似乎是一条奇怪的路线,但您可以通过检查 IntPtr.Size 来判断您是在 32 位(或 WOW64 中的 64 位)还是 64 位 .NET 代码中运行。

On 32-bit/WOW64, IntPtr.Size is 4.

在 32 位/WOW64 上,IntPtr.Size 为 4。

On 64-bit, IntPtr.Size is 8.

在 64 位上,IntPtr.Size 为 8。

Source: Migrating 32-bit Managed Code to 64-bit Managed Codeon MSDN.

来源:将32 位托管代码迁移到MSDN 上的64 位托管代码

回答by Farmer

use GetBinaryType api function and you will have the type of the file

使用 GetBinaryType api 函数,您将获得文件的类型

this is the link to the api on msdn http://msdn.microsoft.com/en-us/library/aa364819(VS.85).aspx

这是 msdn http://msdn.microsoft.com/en-us/library/aa364819(VS.85).aspx 上api 的链接

the results are:

结果是:

SCS_32BIT_BINARY = 32 bit exe

SCS_32BIT_BINARY = 32 位 exe

SCS_64BIT_BINARY = 64 bit exe

SCS_64BIT_BINARY = 64 位 exe

SCS_DOS_BINARY = DOS

SCS_DOS_BINARY = DOS

SCS_OS216_BINARY = OS/2

SCS_OS216_BINARY = OS/2

SCS_WOW_BINARY = 16 bit

SCS_WOW_BINARY = 16 位

SCS_POSIX_BINARY = POSIX based

SCS_POSIX_BINARY = 基于 POSIX

SCS_PIF_BINARY = PIF file that execute on DOS

SCS_PIF_BINARY = 在 DOS 上执行的 PIF 文件

回答by Yochai Timmer

You can use the Predefined Macros to check compilation type

您可以使用预定义宏来检查编译类型

   #if (_WIN64) 
        const bool IS_64 = true;
    #else
       const bool IS_64 = false;
    #endif

回答by Lu55

Are you aware of Environment.Is64BitProcess? It determines whether the current running process is a 64-bit process.

你知道Environment.Is64BitProcess吗?它确定当前运行的进程是否为 64 位进程。