65.9K
CodeProject 正在变化。 阅读更多。
Home

C#中的DLL分析器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.48/5 (18投票s)

2003年4月20日

2分钟阅读

viewsIcon

215315

downloadIcon

3378

用于调试使用多个 DLL 的应用程序的工具。

All Process and their loaded DLLs informations

图 1. 所有进程及其加载的 DLL 信息。

Filter against process name

图 2. 针对进程名称进行过滤。

Start a new application

图 3. 启动一个新应用程序。

Stop a running application

图 4. 停止一个正在运行的应用程序。

概述

在您的计算机中,可能存在数千个不同版本的 DLL。您正在使用哪个版本?您的应用程序实际加载了哪个 DLL?您必须检查路径、版本以及您的进程的当前工作目录等。如果您想快速获得答案,请使用 DLL Profiler。

DLL profiler 用于列出当前加载在您机器上的所有 DLL,包括它们的加载位置、版本号、大小、修改日期、产品名称和产品版本。它是一个用于查找启动应用程序时多个 DLL 错误的应用程序。

此外,您可以在此处直接启动和停止一个应用程序,并观察其加载的 DLL 及其错误。

您可以仅针对一个进程搜索已加载的 DLL。 DLL Profiler 具有过滤功能,仅查看特定进程的 DLL。

此类演示了以下命名空间的使用。

  • System.IO
  • System.Net
  • System.Diagnostics
  • System.Threading
  • System.Drawing.Drawing2D
  • Logger

安装步骤

只需下载并单击一个 .exe 文件即可运行该应用程序。

源代码

当您启动应用程序时,您是否曾经遇到过加载 DLL 时的错误? DLL 版本无效?或者 ???

如果是,这就是为您准备的。

如果您想在打开应用程序后监控它,可以使用此功能。

主要思路是获取本地机器上运行的所有进程。并获取所有模块和适当的详细信息,例如模块路径、文件版本、文件大小、文件修改日期、文件说明、产品名称和产品版本。

using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using Logger;

上述命名空间是 DLL Profiler 的必备项。在这里,我放置了代码,以向您展示如何获取正在运行的进程的名称和进程 ID。

//Get all processes

Process[] procList =  Process.GetProcesses(); 

for ( int i=0; i<procList.Length; i++) 
{ 

     MessgeBox.Show("Process Id=" +  procList[i].Id + 
           "\t" + " Process Name=" + procList[i].ProcessName); 

}

以下代码将解释如何获取相应进程的模块/DLL。

//Get all modules inside the process
Process[] ObjModulesList = Process.GetProcessesByName("devenv");

// Populate the module collection.
ProcessModuleCollection ObjModules = ObjModulesList[0].Modules;

// Iterate through the module collection.
foreach (ProcessModule objModule in ObjModules)
{
//Get valid module path
strModulePath =GetValidString(objModule.FileName.ToString());
//If the module exists
if (File.Exists(objModule.FileName.ToString()))
  {
    //Get version
    string strFileVersion = GetValidString(objModule.
                  FileVersionInfo.FileVersion.ToString()); 
    //Get File size
    string strFileSize    = GetValidString
                (objModule.ModuleMemorySize.ToString());
    //Get Modification date
    FileInfo objFileInfo = new 
                FileInfo(objModule.FileName.ToString());
    string strFileModificationDate = GetValidString
         (objFileInfo.LastWriteTime.ToShortDateString());  
    //Get File description
    string strFileDescription  = GetValidString
                  (objModule.FileVersionInfo.
                  FileDescription.ToString()); 
    //Get Product Name
    string strProductName  = GetValidString
           (objModule.FileVersionInfo.ProductName.ToString());
    //Get Product Version
    string strProductVersion  = GetValidString
          (objModule.FileVersionInfo.ProductVersion.ToString());
  }
}

启动和停止应用程序

START an Application

//Create a new process
Process myProcess = new Process();

//Process name notepad
myProcess.StartInfo.FileName = "Notepad";

//Set it's window style maximized
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;

//Start an exe
myProcess.Start();

STOP an Application

// Returns array containing all instances of Notepad.
Process[] myProcesses = Process.GetProcessesByName("Notepad");

foreach(Process myProcess in myProcesses){
    myProcess.CloseMainWindow();
}

要使用此类,只需将引用添加到您的项目中,并按照步骤操作,然后将 Logger 引用导入到您的项目中。

日志记录

我在这里使用了 Logger。您可以根据需要使用打开/关闭日志记录删除。不了解 Logger 的人请访问 C# 中的 Logger - 监控 .NET 应用程序的简单方法

此外,如果您查看此项目,您可以获得以下想法

  1. 启动画面
  2. 工具栏
  3. 根据 DataGrid 中的值对特定行进行着色。
  4. 通过程序运行和停止应用程序。

就是这样。

我很想听听您的意见。 提前感谢。

© . All rights reserved.