Question

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

Was it helpful?

Solution

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);
}

OTHER TIPS

If its a managed dll: Use reflection

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

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top