How to programmatically get DLL dependencies

2019-01-11 03:39发布

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?

5条回答
Anthone
2楼-- · 2019-01-11 04:19

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();
    }
}
查看更多
一夜七次
3楼-- · 2019-01-11 04:23

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).

查看更多
Explosion°爆炸
4楼-- · 2019-01-11 04:35

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.

查看更多
闹够了就滚
5楼-- · 2019-01-11 04:36

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.

查看更多
看我几分像从前
6楼-- · 2019-01-11 04:38

While this question already has an accepted answer, the documentation referenced in the other answers, where not broken, is old. Rather than reading through all of it only to find it doesn't cover differences between Win32 and x64, or other differences, my approach was this:

C:\UnxUtils\usr\local\wbin>strings.exe E:\the-directory-I-wanted-the-info-from\*.dll > E:\TEMP\dll_strings.txt

This allowed me to use Notepad++ or gvim or whatever to search for dlls that were still depending on MS dlls with 120.dll at the end of the dll name so I could find the ones that needed updating.

This could easily be scripted in your favorite language.

Given that my search for this info was with VS 2015 in mind, and this question was the top result for a Google search, I supply this answer that it may perhaps be of use to someone else who comes along looking for the same thing.

查看更多
登录 后发表回答