1

Pretty much what the question states. I have a .net exe that runs, and then loads my library (in c++). Is there any way to get the ICLRMetaHost, or similar interface, that mscoree loads when it starts up the .net exe? I want to do this primarily so I can access the gc and memory related settings from my native library for debugging purposes.

Bonus points for point out a similar implementation using the mono hosting api.

Programmdude
  • 551
  • 5
  • 22
  • You don't get to tinker with "gc and memory related settings" from the hosting api. There's not enough here to take a guess at what you are *really* trying to do. Books have been written about it, Steven Pratschner's is pretty good. – Hans Passant Dec 15 '13 at 10:51
  • IGCHost::GetThreadStats or ICLRGCManager::GetStats seems like the function that would be useful. I want to access how much memory the CLR is using, so I am able to display debug output for how much memory each part of my application is using. Being able to get the number of collections would be nice too, both of this functionality is provided by the two functions I mentioned earlier. – Programmdude Dec 15 '13 at 13:08
  • 1
    The .NET PerformanceCounters return this info too. Very simple to use and no need for custom hosting. If you don't think about or document what you *really* need then custom hosting is just a hammer that can seemingly strike any nail. – Hans Passant Dec 15 '13 at 13:16
  • Exactly what I was looking for. I didn't know about this class. – Programmdude Dec 15 '13 at 13:22

1 Answers1

2

I don't see why the normal way of obtaining ICLRMetaHost wouldn't work from within a DLL that's been loaded by the CLR...

  1. LoadLibrary on mscoree.dll
  2. Find the address of CLRCreateInstance.
  3. call CLRCreateInstance as shown here to get hold of ICLRMetaHost.
  4. call EnumerateLoadedRuntimes and hope that there's only one loaded...
  5. from your enumerated runtime info obtain ICLRRuntimeHost.
  6. from ICLRRuntimeHost obtain ICLRControl
  7. from ICLRControl obtain ICLRGCManager
Len Holgate
  • 21,282
  • 4
  • 45
  • 92