0

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.

This is my code: I try to convert C++ Dll to byte array and get it's methods.

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);
    }
}
  • 2
    A C++ DLL is not a .NET Assembly, so such Reflection will not work. – Remy Lebeau Aug 10 '19 at 17:31
  • 1
    `data.GetType()` is `typeof(byte[])`, so `result.Assembly` is the instance representing the CLR itself (`mscorlib.dll`) You can use reflection on that all day long but you'll only get .NET builtins, nothing related to ODSSwitchFunctions.dll – Ben Voigt Aug 10 '19 at 19:43
  • @RemyLebeau thanks. but how I can determine its methods? what is your solution? –  Aug 13 '19 at 04:49
  • @Sam see the answer I just posted – Remy Lebeau Aug 13 '19 at 06:21

2 Answers2

1

I don‘t think it is possible. I think a normal dll doesn‘t even contain Type Metadata.

But maybe I‘m wrong. Or maybe this helps: Import TLB into C#

Jochen Kühner
  • 1,385
  • 2
  • 18
  • 43
  • There is a way to enumerate all exported functions. Check this: https://stackoverflow.com/questions/437432/is-there-a-way-to-find-all-the-functions-exposed-by-a-dll – EylM Aug 10 '19 at 18:12
  • @EylM getting the function names is easy. Getting more detailed info about the functions requires reverse engineering the functions – Remy Lebeau Aug 13 '19 at 06:20
1

A C++ DLL is not a .NET Assembly, so the kind of Reflection you are looking for simply will not work.

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. 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. That info is simply not stored anywhere in the DLL itself, only the compiler that created the DLL knew those details. 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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770