Check if unmanaged DLL is 32-bit or 64-bit?

后端 未结 5 1732
一生所求
一生所求 2020-11-30 21:47

How can I programmatically tell in C# if an unmanaged DLL file is x86 or x64?

5条回答
  •  孤独总比滥情好
    2020-11-30 22:03

    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:

    Assembly assembly = Assembly.ReflectionOnlyLoadFrom(@"");
    PortableExecutableKinds kinds;
    ImageFileMachine imgFileMachine;
    assembly.ManifestModule.GetPEKind(out kinds, out imgFileMachine);
    

    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

提交回复
热议问题