2009-01-16 87 views

回答

9

您可以使用EnumProcessModules函數。像kaanbardak建議的託管API不會給你一個本地模塊列表。

例如見this page MSDN上

如果您需要靜態分析你的DLL,你必須深入到PE format,瞭解導入表。有關詳細信息,請參閱excellent tutorial

+0

沒錯,但只能在運行過程中使用它。 – sthiers 2009-01-16 12:37:12

+0

如果你想分析DLL而不加載它,那麼你需要閱讀它的導入表 – aku 2009-01-16 12:55:42

4

注意:基於從下面的文章的評論,我想是因爲它依賴於反射,這可能會錯過非託管的依賴關係爲好。

下面是bytes.com上.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(); 
    } 
} 
1

寫喬恩斯基特一個小C#程序要獲取本機模塊的依賴關係,我相信這應該是確定從PE文件的導入得到它表,這裏有2個鏈接,其解釋深入:

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

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

爲了獲得.NET依賴關係,我們可以使用.NET的API,如Assembly.Load。

爲了得到一個.NET模塊的所有依賴關係,怎麼樣結合這兩種方式 - .NET程序集只是帶有元數據的PE文件。

-1

要讀取正在運行的exe加載的DLL(模塊),請使用ToolHelp32函數 Tool help Documentation on MSDN

不知道它會展示.Net運行的exe(我從來沒有嘗試過)。但是,它確實顯示了DLL的加載位置的完整路徑。通常,這是我在嘗試解決DLL問題時需要的信息。 .Net應該已經刪除了使用這些函數的必要(查找DLL地獄瞭解更多信息)。

1

雖然這個問題已經有了一個可以接受的答案,但其他答案中引用的文檔在沒有被破壞的情況下是舊的。而不是讀通過這一切才發現它不包括Win32和x64或其他之差的,我的做法是這樣的:

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

這讓我用記事本++或GVIM或任何搜索dll仍然依賴於MS DLL在dll名稱末尾的120.dll,所以我可以找到需要更新的DLL。

這可以很容易地以您最喜歡的語言編寫腳本。

鑑於我對該信息的搜索是基於VS 2015的,而且這個問題是Google搜索的最高結果,我提供了這個答案,它可能對其他人一起使用,尋找一樣。