检查的最佳方式,如果一个DLL文件是在C#中的CLR程序集 [英] Best way to check if a DLL file is a CLR assembly in C#

查看:189
本文介绍了检查的最佳方式,如果一个DLL文件是在C#中的CLR程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是判断是否一个DLL文件是一个Win32 DLL的最佳方式,或者如果它是一个CLR组件。目前我使用此代码

What is the best way to check if a DLL file is a Win32 DLL or if it is a CLR assembly. At the moment I use this code

    try
    {
        this.currentWorkingDirectory = Path.GetDirectoryName(assemblyPath);

        //Try to load the assembly.
        assembly = Assembly.LoadFile(assemblyPath);

        return assembly != null;
    }
    catch (FileLoadException ex)
    {
        exception = ex;
    }
    catch (BadImageFormatException ex)
    {
        exception = ex;
    }
    catch (ArgumentException ex)
    {
        exception = ex;
    }
    catch (Exception ex)
    {
        exception = ex;
    }

    if (exception is BadImageFormatException)
    {
        return false;
    }



但我喜欢在装货前检查,因为我不希望这些异常(时间)。

But I like to check before loading because I do not want those exceptions (time).

有没有更好的办法?

推荐答案

检查PE头:

DOS头部开始于为0x0,在
的DWORD 0x3C符号包含一个指向PE
签名(通常为0x80的),这是4
字节,在接下来的20个字节是COFF
标题,然后还有就是PE头
(在0x9。PE头是224字节
和包含数据目录(96
字节到PE头部= 0xF的该
15项(在0x16是CLR头
描述符(有时称为COM
描述符但是这并没有
任何与COM),如果这是
空(即0从0x168
中的8个字节0x16f),那么该文件不是一个.NET
总成。如果你想检查是否
是COM DLL,那么你应该看看
看它是否出口GetClassObject。

DOS header starts at 0x0, the DWORD at 0x3c contains a pointer to the PE signature (usually 0x80) which is 4 bytes, the next 20 bytes is the COFF header and then there is the PE header (at 0x9. The PE header is 224 bytes and contains the data directory (at 96 bytes into the PE header = 0xf. The 15th entry (at 0x16 is the CLR header descriptor (sometimes called the COM descriptor, but this does not have anything to do with COM). If this is empty (ie 0 in the 8 bytes from 0x168 to 0x16f) then the file is not a .NET assembly. If you want to check if it is a COM DLL then you should look to see if it exports GetClassObject.

参考

更新:有完成这更多的'.NET'的方式:

UPDATE: there is a more '.NET' way of accomplishing this:

使用 Module.GetPEKind 法>,并检查 PortableExecutableKinds 枚举:

Use Module.GetPEKind method and check the PortableExecutableKinds Enumeration:

NotAPortableExecutableImage 该文件不是可移植可执行
(PE)文件格式。

NotAPortableExecutableImage The file is not in portable executable (PE) file format.

ILONLY 的可执行文件只包含Microsoft中间语言
(MSIL),因此与
对于32中性比特或64位平台。

ILOnly The executable contains only Microsoft intermediate language (MSIL), and is therefore neutral with respect to 32-bit or 64-bit platforms.

Required32Bit 的可执行文件可以在32位平台上运行,或者在
32位Windows在Windows(WOW)
环境在64位平台上。

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.

PE32Plus 的可执行文件需要一个64位平台。

PE32Plus The executable requires a 64-bit platform.

Unmanaged32Bit 的可执行文件包含纯非托管代码。

Unmanaged32Bit The executable contains pure unmanaged code.

这篇关于检查的最佳方式,如果一个DLL文件是在C#中的CLR程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆