我有一个任意的.NET程序集列表。
我需要以编程方式检查每个DLL是否都是为x86 (而不是x64或任何CPU)构建的。这个是可能的吗?
发布于 2012-03-19 17:31:38
你就写你自己的怎么样?PE体系结构的核心自从在Windows95中实现以来,并没有发生重大的变化。下面是一个C#示例:
public static ushort GetPEArchitecture(string pFilePath)
{
ushort architecture = 0;
try
{
using (System.IO.FileStream fStream = new System.IO.FileStream(pFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
using (System.IO.BinaryReader bReader = new System.IO.BinaryReader(fStream))
{
if (bReader.ReadUInt16() == 23117) //check the MZ signature
{
fStream.Seek(0x3A, System.IO.SeekOrigin.Current); //seek to e_lfanew.
fStream.Seek(bReader.ReadUInt32(), System.IO.SeekOrigin.Begin); //seek to the start of the NT header.
if (bReader.ReadUInt32() == 17744) //check the PE\0\0 signature.
{
fStream.Seek(20, System.IO.SeekOrigin.Current); //seek past the file header,
architecture = bReader.ReadUInt16(); //read the magic number of the optional header.
}
}
}
}
}
catch (Exception) { /* TODO: Any exception handling you want to do, personally I just take 0 as a sign of failure */}
//if architecture returns 0, there has been an error.
return architecture;
}
}
现在,当前常量为:
0x10B - PE32 format.
0x20B - PE32+ format.
但是使用这种方法,它允许新常量的可能性,只要在你认为合适的时候验证返回即可。
发布于 2017-08-22 15:20:44
JetBrains的DotPeek提供了查看msil(anycpu)、x86、x64的快捷方式。
发布于 2013-02-18 20:45:49
尝试使用CorFlagsReader from this project at CodePlex。它没有对其他程序集的引用,可以按原样使用。
https://stackoverflow.com/questions/270531
复制相似问题