CorFlags
.net Framework 的 CorFlags.exe 这个工具可以查看或修改 PE 可执行文件的 header 信息,例如 x86 / x64 架构等。安装 Visual Studio 时包含了这个工具。
Visual Studio 2022 Community Edition 里这个工具位置:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\CorFlags.exe
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\CorFlags.exe
CorFlags.exe Test.exe
Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.8.3928.0
Copyright (c) Microsoft Corporation. All rights reserved.
Version : v4.0.30319
CLR Header: 2.5 // 2.0/2.5 (Must have version of 2.5 or greater to run natively)
PE : PE32 // PE32 (32-bit)/PE32+ (64-bit)
CorFlags : 0x3 // Hexadecimal value, computed based on below 4 flags.
ILONLY : 1 // 1 if MSIL otherwise 0
32BITREQ : 1 // 1 if 32-bit x86 only assembly otherwise 0
32BITPREF : 0 // 1 if 32-bit x86 only preferred in Any CPU architecture otherwise 0
Signed : 0 // 1 if signed with strong name otherwise 0
EDITBIN
EDITBIN 可以修改 PE 可执行文件一些属性信息。
安装 Visual Studio 2022 后这个文件位置:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\Hostx64\x64\editbin.exe
editbin /LARGEADDRESSAWARE Test.exe
- LARGEADDRESSAWARE : 适用于 32 位(X86)程序,使程序支持使用超过 2GB 内存。注意 x86 系统单个进程虚拟内存空间最大 4GB。
检测 exe 文件是否支持 2GB,使用 dumpbin.exe 工具(同样是 Visual Studio 里自带)
dumpbin.exe /headers devenv.exe
Dump of file devenv.exe
PE signature found
File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
14C machine (x86)
4 number of sections
4BA1FAB3 time date stamp Thu Mar 18 12:04:35 2010
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
122 characteristics
Executable
Application can handle large (>2GB) addresses
32 bit word machine
.net Framework
对于 Visual Studio 构建的 .net C# 程序,如果项目 Build 配置里的 Target CPU 为 Any,构建的结果是一个 x86 可执行文件(dumpbin.exe /headers 为 14C machine (x86)),CorFlags.exe 显示 32BITREQ = 0,这个 exe 可以在 32 位或 64位 Windows 系统里工作。如果没有 32BITPREF 标识(注意默认开启,在项目 Build 配置里可以禁用),在 64 位 Windows 里会运行在原生 64 位模式(taskmgr 里进程名称后面没有 * x32 标识)。
Making Prefer 32-bit (32BITREQ) the default was done in VS2012. Making it also set the LARGE ADDRESS AWARE flag is only as of VS2015