How to list / check existance of Managed .NET DLL's in dllhost.exe

一笑奈何 提交于 2020-01-07 04:59:07

问题


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.


回答1:


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.");
    }
  }


来源:https://stackoverflow.com/questions/40803406/how-to-list-check-existance-of-managed-net-dlls-in-dllhost-exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!