locked
Where is ICLRRuntimeHost::GetDefaultDomain? RRS feed

  • Question

  • I'm trying .NET Beta 2 CLR hosting. Why is there no ICLRRuntimeHost::GetDefaultDomain? QI for IID_ICorRuntimeHost on ICLRRuntimeHost returns E_NOINTERFACE. Calling CorBindToRuntimeEx twice is not allowed to get both pointers. Is this just for beta2? How would I create/manipulate AppDomains from unmanaged code otherwise? Thanks

    Wednesday, July 27, 2005 3:53 PM

Answers

  • Hi Leon,

       With v2.0 of the CLR, we've tried to move as much of the hosting process into managed code as possible.  In this case, one way to get the ability to manipulate AppDomains in your host would be to provide an AppDomainManager.  Once inside your AppDomainManager you could set the InitializationFlags property of your AppDomainManager instance to AppDomainManagerInitializationOptions.RegisterWithHost inside yoru InitializeNewDomain method.
       That has the effect of causing the CLR to call your host's IHostControl::SetAppDomainManager callback with a pointer to the AppDomainManager object itself.
       Since you can easily get a COM pointer to an AppDomainManager in this way, you could define an interface that it supports, and in your host QI for that interface to make calls back into it.  One of these methods can easily be "CreateNewDomain" or similar.
       If you think of the hosting process of consisting of two halves now, the traditional unmanaged half, and the new managed half which is rooted in the AppDomainManager object model, things become a little more clear. Smile

    Hope that helped.
    -Shawn
    Wednesday, July 27, 2005 8:42 PM
  • In the post-Beta 2 builds you should also be able to CorBindToRuntimeEx multiple times to get both the V1 & V2 interfaces if you find this too cumbersome.

    The one "gotcha" here is that if you want to use the extended V2 managers (starting w/ a SetHostControl call on ICLRRuntimeHost) then you'll need to bind to the v2 APIs first and then to the v1 APIs after that.
    Monday, August 15, 2005 6:54 PM

All replies

  • Hi Leon,

       With v2.0 of the CLR, we've tried to move as much of the hosting process into managed code as possible.  In this case, one way to get the ability to manipulate AppDomains in your host would be to provide an AppDomainManager.  Once inside your AppDomainManager you could set the InitializationFlags property of your AppDomainManager instance to AppDomainManagerInitializationOptions.RegisterWithHost inside yoru InitializeNewDomain method.
       That has the effect of causing the CLR to call your host's IHostControl::SetAppDomainManager callback with a pointer to the AppDomainManager object itself.
       Since you can easily get a COM pointer to an AppDomainManager in this way, you could define an interface that it supports, and in your host QI for that interface to make calls back into it.  One of these methods can easily be "CreateNewDomain" or similar.
       If you think of the hosting process of consisting of two halves now, the traditional unmanaged half, and the new managed half which is rooted in the AppDomainManager object model, things become a little more clear. Smile

    Hope that helped.
    -Shawn
    Wednesday, July 27, 2005 8:42 PM
  • This is the way we use it in v1.1 in any case. We created and managed AppDomains on .NET side and just gave back COM interface to unmanaged host. Maybe it would be nice to get a hold of unmanaged interface without creating AppDomainManager because I think it's most common scenario. The way we did it in v1.1 is ICorRuntimeHost/GetDefaultDomain/CreateDomain/CreateInstance/Unwrap/QueryInterface.
    Maybe AppDomainManager is better. I'll try using it. Thank you!
    Wednesday, July 27, 2005 9:02 PM
  • Basically if I summarize the steps:

    1. Implement AppDomainManager derived class.

    2. Override InitializationFlags and return AppDomainManagerInitializationOptions.RegisterWithHost

    3. Define COM interface and implement it in AppDomainManager derived class

    4. Register the AppDomainManager through ICLRControl::SetAppDomainManagerType

    5. QueryInterface for custom COM interface the instance returned in IHostControl::SetAppDomainManager for custom interface.

    I haven't tried it yet. But it seems to much for what could have been done much easier before and in predictable way. Now I have to deal with callback from CLR.

    My main question now is: Is this callback that gives me AppDomainManager instance synchronous with CLR Start()?
    How do I know when I'll be handed the AppDomainManager instance? I need it right after I start the CLR. Thanks a gain!

    Wednesday, July 27, 2005 9:23 PM
  • The summary of steps you provided is correct.

    You'll get one callback each time an AppDomain is created, with the AppDomainManager for the particular domain.  As soon as your code forces the creation of the default domain, the first code that's loaded in will be your AppDomainManager, which will then do the callback to your host before other code gets to run.

    -Shawn
    Friday, July 29, 2005 12:18 AM
  • In the post-Beta 2 builds you should also be able to CorBindToRuntimeEx multiple times to get both the V1 & V2 interfaces if you find this too cumbersome.

    The one "gotcha" here is that if you want to use the extended V2 managers (starting w/ a SetHostControl call on ICLRRuntimeHost) then you'll need to bind to the v2 APIs first and then to the v1 APIs after that.
    Monday, August 15, 2005 6:54 PM
  • Thanks!

    Binding to both interfaces is much more acceptable than using AppDomainManager for this.
    Tuesday, August 16, 2005 12:41 AM