【问题标题】:C# Get/Read DLL Imports UsedC# 获取/读取使用的 DLL 导入
【发布时间】:2012-10-22 00:31:37
【问题描述】:
  1. 嘿,我的问题很简单,但我找不到解决方法。

我尝试做的是: 我尝试从 EXE 或 DLL 获取所有 DLL 导入和使用的函数。

假设我制作了一个程序:SendMessage (DLL Import) 然后代码会设法读取它。

然后返回:

DLL: user32.dll

功能:发送消息

我尝试过使用:Assembly.但是不能从中获得正确的数据。

(我确实看过:How to programatically read native DLL imports in C#? 但也没有让它在那里工作,我有 1 个导入,但仅此而已)

【问题讨论】:

  • 这是关于关于 _declspec(dllimport/export) 的 C++ 属性,它告诉非托管库导入/导出什么。还是托管程序中的 DLLImport 属性?
  • 我的意思是这样的:[DllImport("msvcrt.dll")] public static extern int puts(string c);

标签: c# dll import get


【解决方案1】:

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

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

作为一个横向的想法 - 为什么不使用 .net System.Process 调用包装 dumpbin.exe /exports 并解析结果?

【讨论】:

    【解决方案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);
                }
            }
        }
    

    【讨论】:

    • 谢谢,正是我想要的 :) 它真的帮了我大忙。似乎我在正确的轨道上,只是错过了部分:GetMethods 另一个快速的事情:使用它有多安全?如果我“检查”一个有害文件,它会损害我的电脑吗?或者这只会从 EXE 本身读取而不触发任何东西。
    • 除非有人设法在 .NET 反射引擎中找到漏洞,否则您不会执行代码或 exe。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2013-08-17
    • 2023-03-27
    相关资源
    最近更新 更多