10

In a nutshell: I want to do the same thing "Dependency Walker" does.

Is there any Win32 API function which can enumerate the dependencies of a EXE and/or DLL file?

And is there any safe way to detect dependencies on ActiveX classes? (I doubt it is possible, but who knows ...)

EDIT: I'm aware of available tools which provide the same core functionality (Dependency Walker, ProcessExplorer, AQTime, ...) but I want to create my own program which dumps a text file containing the required modules.

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
  • You might consider changing your question's title. If you want to know how to do this via the Win32 API you should state that in the title. This is not just hair-splitting; others may try to find the same information later on and your title may tell them that your question addresses this issue. – Onorio Catenacci Dec 12 '08 at 13:51
  • Even with that, you still want to note that if you need to ensure that all utilized files are noted, executing all of the application's functionality may be required. – Kris Kumler Dec 12 '08 at 15:20

7 Answers7

9

The following commands dumps the direct dependencies of some.exe :

dumpbin /imports some.exe

It works on DLLs too.

This won't list dependencies such as plugins loaded at application launch (via LoadLibrary calls). Same for COM dependencies since they work the same way (as far as I know).

If you need to know all the DLLs used by a running program, use ProcessExplorer.

bltxd
  • 8,825
  • 5
  • 30
  • 44
  • 1
    dumpbin can be executed by opening up the Developer Command Prompt for VS 201x. This can be accessed from the Windows button, scrolling down to the Visual Studio 201x folder. – Richard Jessop Jan 10 '20 at 16:56
5

findstr -i .dll exe.exe | more | findstr -i .dll | more

rem :)

  • 1
    Complete bogus that produces false positives and false negatives, left and right. Will report dependencies for strings, that happen to contain *".dll"*, unilaterally, without determining, where that string is used. Fails to account for DLLs that are loaded at runtime (e.g. by calling `LoadLibrary`, are when instantiating an ActiveX control). – IInspectable Jul 25 '16 at 18:28
  • Wishful thinking on this answer's part... easily scuttled with a single search. – kayleeFrye_onDeck Apr 27 '17 at 20:57
4

Run up the application, with Process Explorer already running and set to filter for your applications .exe name.

There is no way to detect all COM dependencies that an executable has without running it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
2

It seems that Dependency Walker source code itself was given by Microsoft via MSJ. Please look at Re: [DUG]: Dependency Walker.

You need to refer some other site to download since the link given in this mail trail is not working.

Please check MSJ Source Code Updates: Since I don't have time, I have not checked whether it contains source code or only EXE foæes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lakshmanaraj
  • 4,145
  • 23
  • 12
  • 1
    Your first link is broken and your second one points to just an executable. Link-only answers are discouraged on stackoverflow. – David Grayson Apr 28 '15 at 07:14
2

User @blue... eluded to Dependency Walker. When using Dependency Walker, after opening the file you can see the base requirements that are used. Only when executing the program and exercising all of its functions can you find all of the dynamically-loaded DLLs.

Sometimes the best thing to do if you can is ask the developer what DLLs are required. An application may only load some DLLs when absolutely needed. e.g. Loading faultrep.dll, for custom Windows Error Reporting, when it is about to crash.

Kris Kumler
  • 6,307
  • 3
  • 24
  • 27
  • Not true re delay load dlls, they're in an import section of the exe just like regular dlls. But I agree, to find the dynamically loaded dlls you'd have to hook LoadLibrary() and exercise the required code paths in the exe. – Len Holgate Dec 13 '08 at 11:54
1

You probably need to walk the executable's file structure to work this out programatically. Therefore something like the 'PE Dump' program that's mentioned here: http://msdn.microsoft.com/en-gb/magazine/cc301808.aspx would be a good starting point. The actual code that you need can be found here: http://www.wheaty.net/downloads.htm

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
0

You can write a console app to wrap this up, create a PowerShell script with it or, like I usually end up doing since I only have to do it once in a blue moon, add the following to your code for a quick check:

    private static HashSet<string> ReferencedAssemblies = new HashSet<string>();

    ...
    OutputDependencies(Assembly.GetAssembly(typeof(Program)), 0);
    ...

    static void OutputDependencies(Assembly assembly, int indent)
    {
        if (assembly == null) return;

        Console.WriteLine(new String(' ', indent * 4) + assembly.FullName);
        if (!ReferencedAssemblies.Contains(assembly.FullName))
        {
            ReferencedAssemblies.Add(assembly.FullName);

            foreach (var childAssembly in assembly.GetReferencedAssemblies())
            {
                OutputDependencies(Assembly.Load(childAssembly.FullName), indent + 1);
            }
        }
    }
Ed Williams
  • 167
  • 2
  • 12