如何列出/检查dllhost.exe中托管的.NET DLL的存在 [英] How to list / check existance of Managed .NET DLL's in dllhost.exe

查看:379
本文介绍了如何列出/检查dllhost.exe中托管的.NET DLL的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可用于COM +的组件。
在这种情况下,它将被加载到 dllhost.exe (COM代理)中。



为了维护原因,我想创建一个.EXE文件来停止dllhost.exe的所有实例来停止组件的使用。



所以我这样做: / p>

  foreach(Process.GetProcesses()中的var进程)其中(pr => pr.ProcessName.ToLower()== dllhost))
{
var modules = process.Modules;
foreach(模块中的ProcessModule模块)
{
//Console.WriteLine(module.ModuleName);
如果(!module.ModuleName.ToLower()。包含(tqsoft))继续;
process.Kill();
}
}

不幸的是 process.Modules 只列出.dll和.exe文件的非托管代码。



我在MSDN,SO等目前还没有找到任何解决方案。 b $ b这里提到的Hans Passant MDbg - Debugger的协议与Debugge不兼容关于一个解决方案。



我查看了第4版示例,并引用了 mdbgeng.dll corapi.dll 在我的项目中。



以下代码应该给我过程的程序集,但是它失败一个例外。

  MDbgEngine mDbgEngine = new MDbgEngine(); 
var dbgProcess = mDbgEngine.Attach(process.Id);
foreach(在dbgProcess.AppDomains中的CorAppDomain appDomain)
{
foreach(appDomain.Assemblies中的CorAssembly程序集)
{
Console.WriteLine(assembly.Name);
//获取汇编信息
}
}

异常:

  System.Runtime.InteropServices.COMException(0x8007012B):只有部分ReadProcessMemory或WriteProcessMemory请求
已完成。 (来自HRESULT的异常:0x8007012B)
在Microsoft.Samples.Debugging.CorDebug.ICLRMetaHost.EnumerateLoadedRuntime(ProcessSafeHandle hndProcess)
在Microsoft.Samples.Debugging.CorDebug.CLRMetaHost.EnumerateLoadedRuntimes(Int32 processId)
在Microsoft.Samples.Debugging.MdbgEngine.MdbgVersionPolicy.GetDefaultAttachVersion(Int32 processId)
在Microsoft.Samples.Debugging.MdbgEngine.MDbgEngine.Attach(Int32 processId)
在TQsoft.Windows.Products.Sake。 Kernel.StopInformer(Boolean fullstop)
在TQsoft.Windows.Products.Sake.Program.Main(String [] args)

不要责怪我,我毕竟只是人类:)但是这里还是我错过了什么?



更新



我的错误。该异常来自于尝试从32位进程访问64位dllhost.exe。我确定我只访问dllhost.exe与32位进程。



但是我仍然没有得到附件进程的程序集列表。

解决方案

解决方案:我挖掘了Hans Passant提到的MDbgEngine,发现我做错了什么。



对于那些遇到同样问题的人,这里是我的代码。

  // dllhost.exe com + instances 
foreach(Process.GetProcesses()中的var process)其中(pr => pr.ProcessName.ToLower()==dllhost))
{
//更好地检查32位或64位,在我的测试中,我只是捕获异常
try
{
MDbgEngine mDbgEngine = new MDbgEngine();
var dbgProcess = mDbgEngine.Attach(process.Id);
dbgProcess.Go()。WaitOne();
foreach(dbgProcess.AppDomains中的MDbgAppDomain appDomain)
{
var corAppDomain = appDomain.CorAppDomain;
foreach(corAssembly assembly in corAppDomain.Assemblies)
{
if(assembly.Name.ToLower()。包含(tqsoft)
{
dbgProcess。分离();
process.Kill();
}
}
}
}
catch
{
Console.WriteLine(64bit call by 32bit application);
}
}


I have some components that I made available for COM+. In this case it will be loaded in dllhost.exe (COM Surrogate) when it is used.

For maintenance reasons I want to create a .EXE file that stops all instances of the dllhost.exe to stop usage of the components.

So I made this:

  foreach (var process in Process.GetProcesses().Where(pr => pr.ProcessName.ToLower() == "dllhost"))
  {
    var modules = process.Modules;
    foreach (ProcessModule module in modules)
    {
      //Console.WriteLine(module.ModuleName);
      if (!module.ModuleName.ToLower().Contains("tqsoft")) continue;
      process.Kill();
    }
  }

Unfortunately process.Modules do only list unmanaged code of .dll and .exe files.

I did not find any solution so far in MSDN, SO, etc. Hans Passant mentioned here MDbg - Debugger's Protocol Is Incompatible With The Debuggee about a solution.

I checked out the Version 4 sample and referenced mdbgeng.dll and corapi.dll in my project.

The following code should give me the assemblies of the process, but it fails with a exception.

    MDbgEngine mDbgEngine = new MDbgEngine();
    var dbgProcess = mDbgEngine.Attach(process.Id);
    foreach (CorAppDomain appDomain in dbgProcess.AppDomains)
    {
      foreach (CorAssembly assembly in appDomain.Assemblies)
      {
        Console.WriteLine(assembly.Name);
        //get assembly information
      }
    }

Exception:

System.Runtime.InteropServices.COMException (0x8007012B): Only part of a ReadProcessMemory or WriteProcessMemory request
was completed. (Exception from HRESULT: 0x8007012B)
  at Microsoft.Samples.Debugging.CorDebug.ICLRMetaHost.EnumerateLoadedRuntimes(ProcessSafeHandle hndProcess)
  at Microsoft.Samples.Debugging.CorDebug.CLRMetaHost.EnumerateLoadedRuntimes(Int32 processId)
  at Microsoft.Samples.Debugging.MdbgEngine.MdbgVersionPolicy.GetDefaultAttachVersion(Int32 processId)
  at Microsoft.Samples.Debugging.MdbgEngine.MDbgEngine.Attach(Int32 processId)
  at TQsoft.Windows.Products.Sake.Kernel.StopInformer(Boolean fullstop)
  at TQsoft.Windows.Products.Sake.Program.Main(String[] args)

Don't blame on me, I am only human after all :) But what is wrong here or what I miss?

UPDATE

My mistake. The exception comes from try to access 64bit dllhost.exe from a 32bit process. I fixed that I only access dllhost.exe with 32bit processes.

But still I do not get a list of assemblies of the attached process.

解决方案

Solved: I dig into the MDbgEngine mentioned by Hans Passant and found out what I did wrong.

For those who run into same problems, here is my code.

  // dllhost.exe com+ instances
  foreach (var process in Process.GetProcesses().Where(pr => pr.ProcessName.ToLower() == "dllhost"))
  {
    // better check if 32 bit or 64 bit, in my test I just catch the exception
    try
    {
      MDbgEngine mDbgEngine = new MDbgEngine();
      var dbgProcess = mDbgEngine.Attach(process.Id);
      dbgProcess.Go().WaitOne();
      foreach (MDbgAppDomain appDomain in dbgProcess.AppDomains)
      {
        var corAppDomain = appDomain.CorAppDomain;
        foreach (CorAssembly assembly in corAppDomain.Assemblies)
        {
          if (assembly.Name.ToLower().Contains("tqsoft"))
          {
            dbgProcess.Detach();
            process.Kill();
          }
        }
      }
    }
    catch
    {
      Console.WriteLine("64bit calls not supported from 32bit application.");
    }
  }

这篇关于如何列出/检查dllhost.exe中托管的.NET DLL的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆