Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically get DLL dependencies

How can I get the list of all DLL dependencies of a given DLL or EXE file?

In other words, I'd like to do the same as the "Dependency walker" tool, but programmatically.

What is the Windows (ideally .NET) API for that?

like image 794
sthiers Avatar asked Jan 16 '09 10:01

sthiers


People also ask

How do I view DLL dependencies?

Open Find Menu-> "Find Handle or DLL" option or Ctrl+F shortcut way. Show activity on this post. Try Dependencies it is described as "An open-source modern Dependency Walker". It has a GUI but it also works from the command line with optional JSON output!

How do I find the dependencies of a .NET DLL?

Load your DLL into it, right click, and chose 'Analyze' - you'll then see a "Depends On" item which will show you all the other dll's (and methods inside those dll's) that it needs.


2 Answers

You can use EnumProcessModules function. Managed API like kaanbardak suggested won't give you a list of native modules.

For example see this page on MSDN

If you need to statically analyze your dll you have to dig into PE format and learn about import tables. See this excellent tutorial for details.

like image 168
aku Avatar answered Sep 28 '22 05:09

aku


NOTE: Based on the comments from the post below, I suppose this might miss unmanaged dependencies as well because it relies on reflection.

Here is a small c# program written by Jon Skeet from bytes.com on a .NET Dependency Walker

using System;
using System.Reflection;
using System.Collections;

public class DependencyReporter
{
    static void Main(string[] args)
    {
        //change this line if you only need to run the code one:
        string dllToCheck = @"";

        try
        {
            if (args.Length == 0)
            {
                if (!String.IsNullOrEmpty(dllToCheck))
                {
                    args = new string[] { dllToCheck };
                }
                else
                {
                    Console.WriteLine
                        ("Usage: DependencyReporter <assembly1> [assembly2 ...]");
                }
            }

            Hashtable alreadyLoaded = new Hashtable();
            foreach (string name in args)
            {
                Assembly assm = Assembly.LoadFrom(name);
                DumpAssembly(assm, alreadyLoaded, 0);
            }
        }
        catch (Exception e)
        {
            DumpError(e);
        }

        Console.WriteLine("\nPress any key to continue...");
        Console.ReadKey();
    }

    static void DumpAssembly(Assembly assm, Hashtable alreadyLoaded, int indent)
    {
        Console.Write(new String(' ', indent));
        AssemblyName fqn = assm.GetName();
        if (alreadyLoaded.Contains(fqn.FullName))
        {
            Console.WriteLine("[{0}:{1}]", fqn.Name, fqn.Version);
            return;
        }
        alreadyLoaded[fqn.FullName] = fqn.FullName;
        Console.WriteLine(fqn.Name + ":" + fqn.Version);

        foreach (AssemblyName name in assm.GetReferencedAssemblies())
        {
            try
            {
                Assembly referenced = Assembly.Load(name);
                DumpAssembly(referenced, alreadyLoaded, indent + 2);
            }
            catch (Exception e)
            {
                DumpError(e);
            }
        }
    }

    static void DumpError(Exception e)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Error: {0}", e.Message);
        Console.WriteLine();
        Console.ResetColor();
    }
}
like image 36
Presidenten Avatar answered Sep 28 '22 05:09

Presidenten