yoy*_*sef 46
请参阅规格.这是一个基本的实现:
public static MachineType GetDllMachineType(string dllPath)
{
// See http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
// Offset to PE header is always at 0x3C.
// The PE header starts with "PE\0\0" = 0x50 0x45 0x00 0x00,
// followed by a 2-byte machine type field (see the document above for the enum).
//
FileStream fs = new FileStream(dllPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
fs.Seek(0x3c, SeekOrigin.Begin);
Int32 peOffset = br.ReadInt32();
fs.Seek(peOffset, SeekOrigin.Begin);
UInt32 peHead = br.ReadUInt32();
if (peHead!=0x00004550) // "PE\0\0", little-endian
throw new Exception("Can't find PE header");
MachineType machineType = (MachineType) br.ReadUInt16();
br.Close();
fs.Close();
return machineType;
}
Run Code Online (Sandbox Code Playgroud)
该MachineType
枚举定义为:
public enum MachineType : ushort
{
IMAGE_FILE_MACHINE_UNKNOWN = 0x0,
IMAGE_FILE_MACHINE_AM33 = 0x1d3,
IMAGE_FILE_MACHINE_AMD64 = 0x8664,
IMAGE_FILE_MACHINE_ARM = 0x1c0,
IMAGE_FILE_MACHINE_EBC = 0xebc,
IMAGE_FILE_MACHINE_I386 = 0x14c,
IMAGE_FILE_MACHINE_IA64 = 0x200,
IMAGE_FILE_MACHINE_M32R = 0x9041,
IMAGE_FILE_MACHINE_MIPS16 = 0x266,
IMAGE_FILE_MACHINE_MIPSFPU = 0x366,
IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466,
IMAGE_FILE_MACHINE_POWERPC = 0x1f0,
IMAGE_FILE_MACHINE_POWERPCFP = 0x1f1,
IMAGE_FILE_MACHINE_R4000 = 0x166,
IMAGE_FILE_MACHINE_SH3 = 0x1a2,
IMAGE_FILE_MACHINE_SH3DSP = 0x1a3,
IMAGE_FILE_MACHINE_SH4 = 0x1a6,
IMAGE_FILE_MACHINE_SH5 = 0x1a8,
IMAGE_FILE_MACHINE_THUMB = 0x1c2,
IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169,
}
Run Code Online (Sandbox Code Playgroud)
我只需要其中的三个,但为了完整性我将它们全部包括在内.最后的64位检查:
// Returns true if the dll is 64-bit, false if 32-bit, and null if unknown
public static bool? UnmanagedDllIs64Bit(string dllPath)
{
switch (GetDllMachineType(dllPath))
{
case MachineType.IMAGE_FILE_MACHINE_AMD64:
case MachineType.IMAGE_FILE_MACHINE_IA64:
return true;
case MachineType.IMAGE_FILE_MACHINE_I386:
return false;
default:
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ith 21
使用Visual Studio命令提示符,dumpbin/headers dllname.dll也可以.在我的机器上输出的开头说明:
FILE HEADER VALUES
8664 machine (x64)
5 number of sections
47591774 time date stamp Fri Dec 07 03:50:44 2007
Run Code Online (Sandbox Code Playgroud)
更简单:查看System.Reflection.Module类.它包含GetPEKind方法,该方法返回2个枚举,用于描述代码类型和CPU目标.不再是十六进制!
(这篇内容丰富的帖子的其余部分是从http://www.developersdex.com/vb/message.asp?p=2924&r=6413567无耻地复制的)
示例代码:
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(@"<assembly Path>");
PortableExecutableKinds kinds;
ImageFileMachine imgFileMachine;
assembly.ManifestModule.GetPEKind(out kinds, out imgFileMachine);
Run Code Online (Sandbox Code Playgroud)
PortableExecutableKinds可用于检查哪种程序集.它有5个值:
ILOnly:可执行文件仅包含Microsoft中间语言(MSIL),因此对于32位或64位平台而言是中性的.
NotAPortableExecutableImage:该文件不是可移植可执行(PE)文件格式.
PE32Plus:可执行文件需要64位平台.
Required32Bit:可执行文件可以在32位平台上运行,也可以在64位平台上的32位Windows Windows(WOW)环境中运行.
Unmanaged32Bit:可执行文件包含纯非托管代码.
以下是链接:
Module.GetPEKind方法:http: //msdn.microsoft.com/en-us/library/system.reflection.module.getpekind.aspx
PortableExecutableKinds枚举:http://msdn.microsoft.com/en-us/library/system.reflection.portableexecutablekinds( VS.80) .aspx
ImageFileMachine Enumeration:http: //msdn.microsoft.com/en-us/library/system.reflection.imagefilemachine.aspx
归档时间: |
|
查看次数: |
33065 次 |
最近记录: |