在 C# 中检查 DLL 文件是否为 CLR 程序集的最佳方法 [英] Best way to check if a DLL file is a CLR assembly in C#

查看:30
本文介绍了在 C# 中检查 DLL 文件是否为 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 有任何关系).如果这是空(即从 0x168 开始的 8 个字节中的 0到 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 位平台上运行,也可以在Windows 上的 32 位 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.

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

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