2008-10-14 23 views
1

我已經繼承了一個.NET應用程序,它將兩個團隊構建的100個DLL合併在一起,或者從供應商處購買。我想快速確定一個給定的dll是一個.NET程序集還是一個COM組件。我意識到我可以在每個dll上單獨調用ildasm,並且在dll沒有有效的CLR頭部時做一個記錄,但是這種方法看起來笨拙而且很難自動化。在.NET應用程序中識別COM組件

回答

3

如果您想從COM端接近,則在DLL中測試COM對象歸結爲查找名爲「DllGetClassObject」的導出。這是因爲COM運行時通過調用該DLL上的DllGetClassObject()來訪問進程內COM對象。

你可以使用DUMPBIN.EXE一個批處理文件,它附帶了Visual Studio的具體步驟如下:

dumpbin unknown.dll /exports | find "DllGetClassObject" 

上面的命令行會產生一個文本行,如果它是一個包含COM對象的非託管DLL,否則輸出零字節。

您可以通過加載每個DLL並嘗試在該入口點上執行GetProcAddress()來以編程方式執行此操作。這裏是一個使用這種技術的經過測試和工作的C#命令行程序:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

static class NativeStuff 
{ 
    [DllImport("kernel32.dll")] 
    public static extern IntPtr LoadLibrary(string dllToLoad); 

    [DllImport("kernel32.dll")] 
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); 

    [DllImport("kernel32.dll")] 
    public static extern bool FreeLibrary(IntPtr hModule); 
} 

namespace IsComDLL 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if ((args.Length == 0) || String.IsNullOrEmpty(args[0])) 
      { 
       Console.WriteLine("Give DLL name on command line"); 
       Environment.Exit(255); 
      } 

      IntPtr pDll = NativeStuff.LoadLibrary(args[0]); 
      if (pDll == IntPtr.Zero) 
      { 
       Console.WriteLine("DLL file {0} not found", args[0]); 
       Environment.Exit(256); 
      } 

      IntPtr pFunction = NativeStuff.GetProcAddress(pDll, "DllGetClassObject"); 
      int exitValue = 0; 
      if (pFunction == IntPtr.Zero) 
      { 
       Console.WriteLine("DLL file {0} does NOT contain COM objects", args[0]); 
      } 
      else 
      { 
       Console.WriteLine("DLL file {0} does contain COM objects", args[0]); 
       exitValue = 1; 
      } 

      NativeStuff.FreeLibrary(pDll); 

      Environment.Exit(exitValue); 
     } 
    } 
} 
3

您可以隨時嘗試將「Assembly Version」列添加到資源管理器窗口中,並注意哪些元素爲空以查找非.NET程序集。

0
System.Reflection.Assembly.ReflectionOnlyLoadFrom("mydll.dll") 

將返回一個有效的.NET DLL的程序集引用,但會爲COM DLL引發錯誤。

相關問題