枚举 DLL 函数?

发布于 2024-07-20 17:28:04 字数 103 浏览 2 评论 0原文

是否可以枚举 DLL 中存在的每个函数? 得到它的签名怎么样? 我可以在 C# 中执行此操作吗? 或者我必须降低级别才能做到这一点?

问候和感谢, 何塞

Is it possible to enumerate every function present in a DLL ? How about getting its signature ?
Can I do this in C# ? Or do I have to go low level to do this?

Regards and tks,
Jose

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终陌 2024-07-27 17:28:04

如果它是 .NET DLL RedGate 的 Reflector 可以列出方法,甚至尝试反汇编代码。 对于任何开发人员来说,它都是一个很棒的工具箱,而且是免费的

编辑:如果您尝试在运行时读取类型和方法,您将需要使用反射。 您必须加载 AssemblyGetExportedTypes。 然后,迭代MembersMethodsProperties。 这是来自 MSDN 的一篇文章,其中有一个迭代 的示例MemberInfo 信息。 另外,这里还有一篇 MSDN 杂志文章,从 .NET 程序集中提取数据

最后,这是我编写的一个小测试方法,用于在加载的对象上执行方法。

在此示例中,ClassLibrary1 有一个 Class1 类:

public class Class1
{
    public bool WasWorkDone { get; set; }

    public void DoWork()
    {
        WasWorkDone = true;
    }
}

下面是测试:

[TestMethod]
public void CanExecute_On_LoadedClass1()
{
    // Load Assembly and Types
    var assm = Assembly.LoadFile(@"C:\Lib\ClassLibrary1.dll");
    var types = assm.GetExportedTypes();

    // Get object type informaiton
    var class1 = types.FirstOrDefault(t => t.Name == "Class1");
    Assert.IsNotNull(class1);

    var wasWorkDone = class1.GetProperty("WasWorkDone");
    Assert.IsNotNull(wasWorkDone);

    var doWork = class1.GetMethod("DoWork");
    Assert.IsNotNull(doWork);

    // Create Object
    var class1Instance = Activator.CreateInstance(class1.UnderlyingSystemType);

    // Do Work
    bool wasDoneBeforeInvoking = 
          (bool)wasWorkDone.GetValue(class1Instance, null);
    doWork.Invoke(class1Instance, null);
    bool wasDoneAfterInvoking = 
          (bool)wasWorkDone.GetValue(class1Instance, null);

    // Assert
    Assert.IsFalse(wasDoneBeforeInvoking);
    Assert.IsTrue(wasDoneAfterInvoking);
}

If it's a .NET DLL RedGate's Reflector can list the methods and even attempt to disassemble the code. It's a great item for any developer's toolbox and it's free

Edit: If you are trying to read the types and methods at runtime you'll want to use Reflection. You would have to load the Assembly and GetExportedTypes. Then, iterate over the Members to the the Methods and Properties. Here is an article from MSDN that has an example of iterating over the MemberInfo information. Also, here is an MSDN Magazine article, Extracting Data from .NET Assemblies.

Finally, Here is a little test method I wrote for executing a method on a loaded object.

In this example ClassLibrary1 has one class of Class1:

public class Class1
{
    public bool WasWorkDone { get; set; }

    public void DoWork()
    {
        WasWorkDone = true;
    }
}

And here is the test:

[TestMethod]
public void CanExecute_On_LoadedClass1()
{
    // Load Assembly and Types
    var assm = Assembly.LoadFile(@"C:\Lib\ClassLibrary1.dll");
    var types = assm.GetExportedTypes();

    // Get object type informaiton
    var class1 = types.FirstOrDefault(t => t.Name == "Class1");
    Assert.IsNotNull(class1);

    var wasWorkDone = class1.GetProperty("WasWorkDone");
    Assert.IsNotNull(wasWorkDone);

    var doWork = class1.GetMethod("DoWork");
    Assert.IsNotNull(doWork);

    // Create Object
    var class1Instance = Activator.CreateInstance(class1.UnderlyingSystemType);

    // Do Work
    bool wasDoneBeforeInvoking = 
          (bool)wasWorkDone.GetValue(class1Instance, null);
    doWork.Invoke(class1Instance, null);
    bool wasDoneAfterInvoking = 
          (bool)wasWorkDone.GetValue(class1Instance, null);

    // Assert
    Assert.IsFalse(wasDoneBeforeInvoking);
    Assert.IsTrue(wasDoneAfterInvoking);
}
迷鸟归林 2024-07-27 17:28:04

如果它是托管 dll:使用反射

如果它是非托管:您需要枚举 DLL 导出表

If its a managed dll: Use reflection

If its unmanaged: You need to enumerate the DLL export table

深陷 2024-07-27 17:28:04

您可以使用 Dependency Walker 查看 dll 中的所有导出,这是 Microsoft 的免费程序:http ://en.wikipedia.org/wiki/Dependency_walker

You can see all of the exports in a dll by using Dependency Walker, which is a free program from Microsoft: http://en.wikipedia.org/wiki/Dependency_walker

云之铃。 2024-07-27 17:28:04

对于常规 win32 DLL,请参阅Dumpbin 实用程序。 它包含在 Visual-C++ 中(我相信包括免费的“express”版本)。

例子:

c:\vc9\bin\dumpbin.exe /exports c:\windows\system32\kernel32.dll

For regular win32 DLLs, see the Dumpbin utility. It is included with Visual-C++ (including the free "express" version I believe).

example:

c:\vc9\bin\dumpbin.exe /exports c:\windows\system32\kernel32.dll
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文