本文翻译自:How to determine if a .NET assembly was built for x86 or x64?
I've got an arbitrary list of .NET assemblies. 我有一个.NET程序集的任意列表。
I need to programmatically check if each DLL was built for x86 (as opposed to x64 or Any CPU). 我需要以编程方式检查每个DLL是否针对x86构建(而不是针对x64或Any CPU)。 Is this possible? 这可能吗?
#1楼
参考:https://stackoom.com/question/18NP/如何确定-NET程序集是为x-还是x-构建的
#2楼
Just for clarification, CorFlags.exe is part of the .NET Framework SDK . 只是为了澄清起见,CorFlags.exe是.NET Framework SDK的一部分 。 I have the development tools on my machine, and the simplest way for me determine whether a DLL is 32-bit only is to: 我的机器上有开发工具,确定DLL是否仅是32位的最简单方法是:
Open the Visual Studio Command Prompt (In Windows: menu Start/Programs/Microsoft Visual Studio/Visual Studio Tools/Visual Studio 2008 Command Prompt) 打开Visual Studio命令提示符(在Windows中:菜单“开始” /“程序” /“ Microsoft Visual Studio” /“ Visual Studio工具” /“ Visual Studio 2008命令提示符”)
CD to the directory containing the DLL in question CD到包含相关DLL的目录
Run corflags like this:
corflags MyAssembly.dll
像这样运行corflags:corflags MyAssembly.dll
You will get output something like this: 您将获得如下输出:
Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.
Version : v2.0.50727
CLR Header: 2.5
PE : PE32
CorFlags : 3
ILONLY : 1
32BIT : 1
Signed : 0
As per comments the flags above are to be read as following: 根据评论,上面的标志应如下所示:
- Any CPU: PE = PE32 and 32BIT = 0 任何CPU:PE = PE32和32BIT = 0
- x86: PE = PE32 and 32BIT = 1 x86:PE = PE32和32BIT = 1
- 64-bit: PE = PE32+ and 32BIT = 0 64位:PE = PE32 +和32BIT = 0
#3楼
Try to use CorFlagsReader from this project at CodePlex . 尝试从CodePlex的此项目中使用CorFlagsReader。 It has no references to other assemblies and it can be used as is. 它没有引用其他程序集,可以按原样使用。
#4楼
cfeduke notes the possibility of calling GetPEKind. cfeduke指出了调用GetPEKind的可能性。 It's potentially interesting to do this from PowerShell. 从PowerShell执行此操作可能很有趣。
Here, for example, is code for a cmdlet that could be used: https://stackoverflow.com/a/16181743/64257 例如,此处是可使用的cmdlet的代码: https : //stackoverflow.com/a/16181743/64257
Alternatively, at https://stackoverflow.com/a/4719567/64257 it is noted that "there's also the Get-PEHeader cmdlet in the PowerShell Community Extensions that can be used to test for executable images." 或者,在https://stackoverflow.com/a/4719567/64257上注意到,“ PowerShell社区扩展中还有Get-PEHeader cmdlet,可用于测试可执行映像。”
#5楼
[TestMethod]
public void EnsureKWLLibrariesAreAll64Bit()
{
var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Where(x => x.FullName.StartsWith("YourCommonProjectName")).ToArray();
foreach (var assembly in assemblies)
{
var myAssemblyName = AssemblyName.GetAssemblyName(assembly.FullName.Split(',')[0] + ".dll");
Assert.AreEqual(ProcessorArchitecture.MSIL, myAssemblyName.ProcessorArchitecture);
}
}
#6楼
You can use the CorFlags CLI tool (for instance, C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Bin\\CorFlags.exe) to determine the status of an assembly, based on its output and opening an assembly as a binary asset you should be able to determine where you need to seek to determine if the 32BIT flag is set to 1 ( x86 ) or 0 ( Any CPU or x64 , depending on PE
): 您可以使用CorFlags CLI工具(例如C:\\ Program Files \\ Microsoft SDKs \\ Windows \\ v7.0 \\ Bin \\ CorFlags.exe)根据程序集的输出确定程序集的状态,并以二进制资产,您应该能够确定需要在哪里确定32BIT标志是设置为1( x86 )还是0( 任何CPU或x64 ,取决于PE
):
Option | PE | 32BIT
----------|-------|---------
x86 | PE32 | 1
Any CPU | PE32 | 0
x64 | PE32+ | 0
The blog post x64 Development with .NET has some information about corflags
. 博客文章x64使用.NET开发具有有关corflags
一些信息。
Even better, you can use Module.GetPEKind
to determine whether an assembly is PortableExecutableKinds
value PE32Plus
(64-bit), Required32Bit
(32-bit and WOW), or ILOnly
(any CPU) along with other attributes. 更好的是,您可以使用Module.GetPEKind
来确定程序集是否为PortableExecutableKinds
值,包括PE32Plus
(64位), Required32Bit
(32位和WOW)或ILOnly
(任何CPU)以及其他属性。