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

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

问题描述

检查DLL文件是Win32 DLL还是CLR程序集的最佳方法是什么?目前我使用这个代码

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

//尝试加载程序集。
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;
}

如果(异常是BadImageFormatException)
{
return false;
}

但我喜欢在加载之前检查,因为我不想要这些例外)



有更好的方法吗?

解决方案

PE标题:


DOS标头从0x0开始,DWORD在
0x3c包含一个指向PE
签名的指针(通常为0x80)是4
字节,接下来的20个字节是COFF
头,然后是PE头
(在0x9,PE头是224字节
并且包含数据目录(在96
字节到PE头= 0xf
第15个条目(在0x16是CLR头
描述符(有时称为COM
描述符,但是没有
与COM有关)如果这是
为空(即从0x168
到0x16f的8个字节中为0),则该文件不是.NET
汇编如果你想检查
是一个COM DLL,那么你应该看看
看看是否导出s GetClassObject。


参考



更新:有一个更多的.NET方式来实现这一点:



使用 Module.GetPEKind 方法,并检查< a href =http://msdn.microsoft.com/en-us/library/system.reflection.portableexecutablekinds.aspx =noreferrer> PortableExecutableKinds 枚举:


NotAPortableExecutableImage 该文件不在便携式可执行文件
(PE)文件格式。



ILOnly 可执行文件仅包含Microsoft中间语言
(MSIL),因此中性与
相当于32位或64位平台。



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



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



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



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).

Is there a better way?

解决方案

Check the PE header:

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.

Ref.

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

Use Module.GetPEKind method and check the PortableExecutableKinds Enumeration:

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

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

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 The executable requires a 64-bit platform.

Unmanaged32Bit The executable contains pure unmanaged code.

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

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