C#获取/读取DLL导入使用

时间:2012-10-22 00:31:37

标签: c# dll import get

  1. 嘿,我的问题很简单,但我找不到办法做到这一点。
  2. 我尝试做的是: 我尝试获取所有DLL Imports和从EXE或DLL使用的函数。

    所以让我说我使用:SendMessage(DLL导入)创建一个程序 然后代码将设法读取它。

    然后回复:

    DLL: user32.dll

    功能: SendMessage

    我尝试过使用:Assembly。但没有运气从中获取正确的数据。

    (我确实看过:How to programatically read native DLL imports in C#? 但也没有让它在那里工作,我得到1导入,但没有更多)

2 个答案:

答案 0 :(得分:3)

DUMPBIN程序会检查DLL PE标头,并让您确定此信息。

我不知道任何C#包装器,但这些文章应该向您展示如何检查标头并自行转储导出

作为横向思考 - 为什么不用.net System.Process调用 dumpbin.exe /exports 并解析结果?

答案 1 :(得分:2)

纯粹的反思方法

    static void Main(string[] args)
    {
        DumpExports(typeof (string).Assembly);
    }

    public static void DumpExports( Assembly assembly)
    {

        Dictionary<Type, List<MethodInfo>> exports = assembly.GetTypes()
            .SelectMany(type => type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
                                    .Where(method => method.GetCustomAttributes(typeof (DllImportAttribute), false).Length > 0))
            .GroupBy(method => method.DeclaringType)
            .ToDictionary( item => item.Key, item => item.ToList())
            ;

        foreach( var item in exports )
        {
            Console.WriteLine(item.Key.FullName);

            foreach( var method in  item.Value )
            {
                DllImportAttribute attr = method.GetCustomAttributes(typeof (DllImportAttribute), false)[0] as DllImportAttribute;
                Console.WriteLine("\tDLL: {0}, Function: {1}", attr.Value, method.Name);
            }
        }
    }