6

I would like to get a list of all the dlls loaded for a given Process. I am currently using .NET Framework 4.0. I am aware that there is a bug when trying to access all managed dlls through the Process.Modules property. (Only lists the unmanaged dlls). I need a way to programmatically retrieve all of these dlls.

 Process[] myProcess = Process.GetProcessesByName("MyProcess");
 if(myProcess.Count() > 0) 
 {
      foreach (ProcessModule processModule in myProcess[0].Modules)
      //get information
 }

EDIT: The process I am interested in is not in the current AppDomain.

Matthew
  • 366
  • 5
  • 21
  • 2
    check out : `AppDomain.GetAssemblies` – Parimal Raj Feb 13 '13 at 21:22
  • @AppDeveloper i think he wants to get the assemblies of another process and not the own process using AppDomain – Jehof Feb 13 '13 at 21:25
  • Thank you for the reply. If my process is not running in the current AppDomain however, I do not believe this will work. – Matthew Feb 13 '13 at 21:26
  • Do you know the name or Id of the process you want to inspect? – Raymond Saltrelli Feb 13 '13 at 21:29
  • @RaySaltrelli Yes I have a handle on the Process in question. I use Process.GetProcessesByName("MyProcess") method to retrieve it. – Matthew Feb 13 '13 at 21:32
  • @Matthew - but u can easily know how many app domain are there in your process! – Parimal Raj Feb 13 '13 at 21:35
  • So you're saying that when you call Process.GetProcessesByName("MyProcess").Modules you're only getting unmanaged DLLs but you want managed ones as well. Is this correct? – Raymond Saltrelli Feb 13 '13 at 21:36
  • I think the fact that your code snippet uses GetCurrentProcess() is causing some confusion. – Raymond Saltrelli Feb 13 '13 at 21:37
  • @AppDeveloper I am a bit confused as to what you mean. If I understand correctly, a Process may contain one or many AppDomains. Can you maybe elaborate on what you are suggesting? – Matthew Feb 13 '13 at 21:42
  • @RaySaltrelli yes that is exactly what the problem is. Sorry for the confusion, updated code to hopefully clear up the goal of the question. – Matthew Feb 13 '13 at 21:46

1 Answers1

6

I am aware that there is a bug

No, that's not a bug. It was an intentional design change in CLR v4, Microsoft did not keep that a secret. Previous versions of the CLR made an effort to emulate loaded assemblies as though they were unmanaged DLLs. But that just stopped making sense when they implemented the side-by-side in-process CLR versioning feature. It's gone and won't come back.

This isn't exactly a major problem, getting the list of loaded assemblies in another process is well supported by the debugging interface. ICorDebugAppDomain::EnumerateAssemblies() is the ticket. Well, not exactly as easy to use as Process.Modules. Use the MDbg sample to find out how to use it.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for the correction. Can you please look at my other question regarding MDbg? [link](http://stackoverflow.com/questions/14877213/mdbg-debuggers-protocol-is-incompatible-with-the-debuggee). – Matthew Feb 14 '13 at 14:41