How can I programmatically tell in C# if an unmanaged DLL file is x86 or x64?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Using a Visual Studio command prompt, dumpbin /headers dllname.dll works too. On my machine the beginning of the output stated:
Instead of
Assembly.LoadFile
, useAssembly.ReflectionOnlyLoadFrom
. This will let you work around the "Bad Image Format" exceptions.Refer to the specifications. Here's a basic implementation:
The
MachineType
enum is defined as:I only needed three of these, but I included them all for completeness. Final 64-bit check:
Even easier: check out the System.Reflection.Module class. It includes the GetPEKind method, which returns 2 enums that describe the type of code and the CPU target. No more hex!
(the rest of this very informative post was copied shamelessly from http://www.developersdex.com/vb/message.asp?p=2924&r=6413567)
Sample code:
PortableExecutableKinds can be used to check what kind of the assembly. It has 5 values:
ILOnly: The executable contains only Microsoft intermediate language (MSIL), and is therefore neutral with respect to 32-bit or 64-bit platforms.
NotAPortableExecutableImage: The file is not in portable executable (PE) file format.
PE32Plus: The executable requires a 64-bit platform.
Required32Bit: The executable can be run on a 32-bit platform, or in the 32-bit Windows on Windows (WOW) environment on a 64-bit platform.
Unmanaged32Bit: The executable contains pure unmanaged code.
Following are the links:
Module.GetPEKind Method: http://msdn.microsoft.com/en-us/library/system.reflection.module.getpekind.aspx
PortableExecutableKinds Enumeration: 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