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

0

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.

c#
.net
dll
asked on Stack Overflow Nov 25, 2016 by YvesR • edited May 23, 2017 by Community

1 Answer

0

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.");
    }
  }
answered on Stack Overflow Nov 25, 2016 by YvesR • edited Nov 25, 2016 by YvesR

User contributions licensed under CC BY-SA 3.0