locked
Finding the default appdomain of a process from child appdomain RRS feed

  • Question

  • Could anybody suggest me how to find out the default app domain of a process from a child app domain?

    My framework spans child domains and my code runs in one of the child domain, where I want to access some static data available in the default appdomain.

    Here, I have no clues about the default app domain/parent domain at all. Any suggestions to resolve this?

     


    Kiran Chand Palakkattiri
    [Life is not a personality contest]
    kiran.chand@live.in
    Friday, April 30, 2010 12:33 PM

Answers

  • Given an appdomain you can use the IsDefaultAppDomain property to determine if it is the default.  But to get the default domain without having it already you'll have to call out to the host.  It is not available inside .NET proper.  To get to the host you'll need to add a COM reference to the Common Language Runtime Execution Engine (mscoree.tlb).  Then you can create an instance of the CorHostRuntimeClass and use the GetDefaultDomain method.

    Here's the boilerplate code.

    var

     

    host = new mscoree.CorRuntimeHostClass();

     

    object obj;

    host.GetDefaultDomain(

    out obj);

    AppDomain domain = (AppDomain)obj;

    Note that this code doesn't appear to compile properly under VS2010.  It probably has something to do with the COM support changes they made.  If you want to use VS2010 you'll either have to figure out why it doesn't compile or use an alternative approach.

    Michael Taylor - 4/30/2010
    http://msmvps.com/blogs/p3net

    Friday, April 30, 2010 2:23 PM
  • In 4.0 you could use the interface to instantiate the class. Here is the updated code.

        static void Main(string[] args)
        {
          var host = new mscoree.CorRuntimeHost();
          object defaultAppDomain;
          host.GetDefaultDomain(out defaultAppDomain);
    
          Console.WriteLine((defaultAppDomain as AppDomain).FriendlyName);
          Console.ReadLine();
    
        }
    
    Friday, April 30, 2010 3:58 PM

All replies

  • Given an appdomain you can use the IsDefaultAppDomain property to determine if it is the default.  But to get the default domain without having it already you'll have to call out to the host.  It is not available inside .NET proper.  To get to the host you'll need to add a COM reference to the Common Language Runtime Execution Engine (mscoree.tlb).  Then you can create an instance of the CorHostRuntimeClass and use the GetDefaultDomain method.

    Here's the boilerplate code.

    var

     

    host = new mscoree.CorRuntimeHostClass();

     

    object obj;

    host.GetDefaultDomain(

    out obj);

    AppDomain domain = (AppDomain)obj;

    Note that this code doesn't appear to compile properly under VS2010.  It probably has something to do with the COM support changes they made.  If you want to use VS2010 you'll either have to figure out why it doesn't compile or use an alternative approach.

    Michael Taylor - 4/30/2010
    http://msmvps.com/blogs/p3net

    Friday, April 30, 2010 2:23 PM
  • In 4.0 you could use the interface to instantiate the class. Here is the updated code.

        static void Main(string[] args)
        {
          var host = new mscoree.CorRuntimeHost();
          object defaultAppDomain;
          host.GetDefaultDomain(out defaultAppDomain);
    
          Console.WriteLine((defaultAppDomain as AppDomain).FriendlyName);
          Console.ReadLine();
    
        }
    
    Friday, April 30, 2010 3:58 PM