简体   繁体   English

我们如何在 C# 中从 C++ DLL 中获取所有方法?

[英]How do we get all methods from C++ DLL in C#?

I have a C++ DLL and I need to get it's methods in my C# app, but I am not able to get the methods.我有一个 C++ DLL,我需要在我的 C# 应用程序中获取它的方法,但我无法获取这些方法。

This is my code: I try to convert C++ Dll to byte array and get it's methods.这是我的代码:我尝试将 C++ Dll 转换为字节数组并获取它的方法。

byte[] data = File.ReadAllBytes(@"C:\test.dll");
var result = data.GetType();

foreach (Type type in result.Assembly.GetTypes())
{
    if (!type.IsPublic)
    {
        continue;
    }

    MemberInfo[] members = type.GetMethods();

    foreach (MemberInfo member in members)
    {
        Console.WriteLine(member.Name);
    }
}

I don't think it is possible.我不认为这是可能的。 I think a normal dll doesn't even contain Type Metadata.我认为普通的 dll 甚至不包含类型元数据。

But maybe I'm wrong.但也许我错了。 Or maybe this helps: Import TLB into C#或者这可能有帮助:将TLB 导入 C#

A C++ DLL is not a .NET Assembly, so the kind of Reflection you are looking for simply will not work. C++ DLL 不是 .NET 程序集,因此您正在寻找的那种反射根本行不通。

Unless the DLL has an embedded COM type library in it, the only thing you can obtain with 100% certainty is the names/ordinals of the DLL's exported functions, since that info is freely available in the DLL's EXPORTS table.除非 DLL 中有嵌入的 COM 类型库,否则您唯一可以 100% 确定的是 DLL 导出函数的名称/序号,因为该信息在 DLL 的 EXPORTS 表中是免费提供的。 But to get any more detailed info about the functions (return values, calling conventions, parameter lists), you would have to reverse engineer the DLL's raw machine code, which is not trivial to do.但是要获得有关函数的更多详细信息(返回值、调用约定、参数列表),您必须对 DLL 的原始机器代码进行逆向工程,这并非易事。 That info is simply not stored anywhere in the DLL itself, only the compiler that created the DLL knew those details.该信息根本没有存储在 DLL 本身的任何位置,只有创建 DLL 的编译器知道这些细节。 Reverse engineering tools like IDA can make the task a bit easier, but there is still a lot of manual work involved to interpret and tweak the results.像 IDA 这样的逆向工程工具可以使任务更容易一些,但仍然需要大量的手动工作来解释和调整结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM