检查.net dll构建的目标平台是any cpu、x86、x64

有时候,需要检查构建的dll是否针对正确的平台

可以使用CorFlags.exe(它是.NET Framework SDK的一部分)从dll中查找此信息。运行CorFlags.exe将产生以下输出:

>> CorFlags "C:\example.dll"

Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.6.1590.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 0x3
ILONLY    : 1
32BITREQ  : 1
32BITPREF : 0
Signed    : 0

我们需要关注的两个参数是“PE”和“32BITREQ”​​

Any CPU PE: PE32, 32BITREQ: 0
x86 PE: PE32, 32BITREQ: 1
x64 PE: PE32+, 32BITREQ: 0

 

 

 

 

要以编程方式确定目标平台,我们可以使用Module.GetPEKind()

Assembly a = Assembly.ReflectionOnlyLoadFrom(@"C:\example.dll");
 
PortableExecutableKinds peKind;
ImageFileMachine machine;
 
a.ManifestModule.GetPEKind(out peKind, out machine);
 
Console.WriteLine(peKind);

peKind的结果可以解释为:

Any CPU ILOnly
x86 ILOnly, Required32Bit
x64 ILOnly, PE32Plus

 

 

 

 

 示例

翻译:https://malvinly.com/2016/11/16/check-whether-a-net-dll-is-built-for-any-cpu-x86-or-x64/

posted @ 2018-11-13 13:16  无式!  阅读(603)  评论(0编辑  收藏  举报