3
  1. Hey, my question is simple but I can't find a way to do this.

What I try to do is: I try to get all the DLL Imports and the functions used from an EXE or DLL.

So let's say I make a program with the: SendMessage (DLL Import) Then the code would manage to read that.

And return like:

DLL: user32.dll

Function: SendMessage

I have tried using the: Assembly. But no luck getting correct data from it.

(I did look at: How to programatically read native DLL imports in C#? But didn't get it to work there either, I got 1 import, but nothing more)

Community
  • 1
  • 1
GhostHax
  • 33
  • 1
  • 3
  • Is this about the about _declspec(dllimport/export) the C++ attribute that tell what a unmanaged library imports/exports. Or the DLLImport attribute in a managed program? – Preet Sangha Oct 22 '12 at 01:37
  • I'm meaning like this: [DllImport("msvcrt.dll")] public static extern int puts(string c); – GhostHax Oct 25 '12 at 08:52

2 Answers2

3

The DUMPBIN program examines the DLL PE Header and lets you determine this info.

I don't know of any C# wrapper but these articles should show you how to examine the header and dump out the exports yourself

As a lateral thought - why not wrap dumpbin.exe /exports with a .net System.Process call and parse the results?

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
2

A pure reflection approach

    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);
            }
        }
    }
Robert Slaney
  • 3,712
  • 1
  • 21
  • 25
  • Thanks, just what I was looking for :) It really helped me out. Seems I was on the right track, just missed the part: GetMethods Just another quick thing: How safe is it to use? If I "check" a harmful file, will it harm my PC? Or will this only read from the EXE itself and not trigger anything. – GhostHax Oct 25 '12 at 08:37
  • You aren't executing the code or exe, unless someone has managed to find a vulnerability in the .NET reflection engine. – Robert Slaney Oct 29 '12 at 02:45