views:

1306

answers:

6

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?

A: 

System.Reflection

Won't get unmanaged dependencies - or work at all for unmanaged exes.
James Ogden
also you don't need reflection to enumerate managed modules
aku
you are right guys, I misinterpreted the question.
+4  A: 

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.

aku
That's right, but you can use it only on a runnning process.
sthiers
If you want to analyze dll without loading it then you need to read it's import table
aku
A: 

Edit: Just read the comments from the post below, so I suppose this might miss unmanaged dependencies as well because it relies on reflection.

Here is a small c# program I found on another forum. I've tried it on some dll's and it seems to load all dependencies

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

public class DependencyReporter
{
    static void Main(string[] args)
    {
        try
        {
            if (args.Length == 0)
            {
                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)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }
    }

    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}]", fqn.Name);
            return;
        }
        alreadyLoaded[fqn.FullName] = fqn.FullName;
        Console.WriteLine(fqn.Name);

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

Enjoy!

Ps. Original thread here: http://bytes.com/groups/net/107335-net-dependency-walker

Presidenten
This code won't show unmanaged modules.Mono.Cecil would be a much better solution for managed code because it doesn't require you to load assemblies into AppDomain (and you can't unload assembly later)
aku
+1  A: 

To get native module dependencies, I believe it should be ok to get it from the PE file's import table, here are 2 links which explain that in-depth:

http://msdn.microsoft.com/en-us/magazine/bb985992.aspx

http://msdn.microsoft.com/en-us/magazine/cc301808.aspx

To get .NET dependencies, we can use .NET's API, like Assembly.Load.

To get a .NET module's all dependencies, How about combine the 2 ways - .NET assemblies are just PE file with meta data.

lz_prgmr
A: 

To read the DLL's (modules) loaded by a running exe, use the ToolHelp32 functions Tool help Documentation on MSDN.

Not sure what it will show for a .Net running exe (I've never tried it). But, it does show the full path from where the DLL's were loaded. Often, this was the information I needed when trying to sort out DLL problems. .Net is supposed to have removed the need to use these functions (look up DLL Hell for more information).

Aussie Craig
A: 

I still haven't figured out how to get other .NET dependencies. I am able to get dependencies only for .NET dll

Bill Weil