Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2007

Tip: Looking for answers? Try searching our database.

Interacting with managed objects in a hosted CLR

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Todor Todorov - 04 Jul 2007 01:51 GMT
I read the book "Customizing the MS .NET Framework CLR" By Steven
Pratschner. It describes how to host the CLR in an application for example
the way SQL Server hosts the CLR. It also describes in great details how to
control the CLR, the way it behaves.

What I am missing is, how does the host interact with the managed code in
the hosted CLR? How does the host create instances and invoke methods on
those instances? How does the host gets reply from the CLR? And how does the
CLR communicates to the host, if a callback is needed?

Thanks!

PS: I am not sure if this is the right group.
Steven Cheng[MSFT] - 04 Jul 2007 08:17 GMT
Hi Todor,

As for custom CLR hosting scenario, if you want to communicate between your
unmanaged hosting application and the managed CLR runtime, you should make
use of the "AppDomainManager" (application domain manager). In .NET 2.0,
each appdomain can have an AppDomainManager and we can get the reference to
the AppDomainManager after it is created(or at initialize time). And for
custom CLR hosting scenario, you can use some certain interface method to
query the AppDomainManager interface pointer of the default AppDomain of
your managed CLR runtime.  

#AppDomainManager Class
http://msdn2.microsoft.com/en-us/library/system.appdomainmanager(VS.80).aspx
#Mtps_DropDownFilterText

for detailed description on this, you can refer to the "Application Domain
Managers" section in the <Customizing the Microsoft .NET Framework Common
language Runtime> book (by Steven Pratschner). It is in the Chapter 5(using
application domain effectively).

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Günter Prossliner - 04 Jul 2007 08:27 GMT
Hello Todor!

> I read the book "Customizing the MS .NET Framework CLR" By Steven
> Pratschner.

You too? ;-)

> What I am missing is, how does the host interact with the managed
> code in the hosted CLR? How does the host create instances

Starting from the ICorRuntimeHost interface, you have to get the _AppDomain
instance. The Domain may be the DefaultDomain
(ICodeRuntimeHost.GetDefaultDomain), or you may start a new AppDomain
(CreateDomain(Ex), ...)

[_AppDomain]
http://msdn2.microsoft.com/en-us/library/system._appdomain.aspx

When you have the _AppDomain instance, you can use all Methods like
"CreateInstance", or "CreateInstanceFrom" to create Objects.

[_AppDomain.CreateInstance]
http://msdn2.microsoft.com/en-us/library/system._appdomain.createinstance.aspx

[_AppDomain.CreateInstanceFrom]
http://msdn2.microsoft.com/en-us/library/system._appdomain.createinstancefrom.aspx

> and invoke
> methods on those instances?

Maybe there are other methods, but I am only aware of one way to do this:
You should make the Objects that you have to call from the unmanaged world
COM-Visible. Then you can call methods using COM (as done within the CLR
itself). I do not know how the SQL-Server integration has been managed.

> How does the host gets reply from the
> CLR?

If I understand you correclty, you meen how the unmanaged Code gets
Methodcalls, Data, ... from the managed Code? Well, it's just a matter of
the Object-Model. If you have the ability to create objects, and call
methods (with parameters) from this object, you have everything you need.

If the managed Code should invoke unmanaged methods, you sould implement an
COM-based "Context" Object which is passed to the managed Object as an
Parameter. When the managed Code calls Methods from the Context-Object, it
calls the unmanaged Code via COM Interop.

With a proper Object-Model on the managed side, you can hide awai all this
unamanged / COM Stuff from the implementor of the managed Components (if
this is part of a bigger project).

> And how does the CLR communicates to the host, if a callback is
> needed?

What kind of callback to you meen?

:: GP
Todor Todorov - 05 Jul 2007 01:03 GMT
Se inline.

>> methods on those instances?
>
> Maybe there are other methods, but I am only aware of one way to do this:
> You should make the Objects that you have to call from the unmanaged world
> COM-Visible. Then you can call methods using COM (as done within the CLR
> itself). I do not know how the SQL-Server integration has been managed.

It would be nice to head how MSSQL have implemented this.

COM is apparently the way to communicate to the CLR and to the host when it
comes to objects. I have to questions here:

1. If a have a class X with method Y, do I have to register the class as COM
server (gacutil.exe on the assembly, regasm.exe to make a typelib and
register it in the registry) or can I access the class in-place, meaning
that the COM interfaces are private to the class and the host only and the
rest of the world has no knowledge of those. The idea is to keep the host
machine clean and simplify deployment.

2. If I want to use an existing .Net class (from the host), let's say
System.Text.StringBuilder, how would that class have to be registered with
COM?

>> How does the host gets reply from the
>> CLR?
[quoted text clipped - 3 lines]
> the Object-Model. If you have the ability to create objects, and call
> methods (with parameters) from this object, you have everything you need.

I ment the return value.

> If the managed Code should invoke unmanaged methods, you sould implement
> an COM-based "Context" Object which is passed to the managed Object as an
[quoted text clipped - 4 lines]
> unamanged / COM Stuff from the implementor of the managed Components (if
> this is part of a bigger project).

Yes, so the way to go is let the host implement a COM server and have the
CLR call the COM menthods in the host. Correct?
Steven Cheng[MSFT] - 06 Jul 2007 04:42 GMT
Hi Todor,

For custom CLR host scenario, the main approach to communicate between
unmanaged world to the hosting CLR is using the AppDomainManager, you get
the default domain's AppDomainmanager reference through certain interface,
and then query some custom interfaces(you've defined in your custom
AppDomainManager class).  So what you need to do here is:

** define your own AppDomainManager class(managed class)

** in the AppDomainManager class, define some methods that can be correctly
called from unmanaged code through COM interop interfaces

At runtime, CLR will load your AppDomainManager class(you do not need to
explicitly host it as a COM server). And at unmanaged part, you need to get
reference to the AppDomainManager and query the certain interface(you've
defined in your AppDomainManger class).

As I mentioned in the previous message,  you can refer to the "Application
Domain Managers" section in the <Customizing the Microsoft .NET Framework
Common language Runtime> book (by Steven Pratschner). It is in the Chapter
5(using application domain effectively).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 10 Jul 2007 16:21 GMT
Hi Todor,

Have you got any further progress or does the infornmation mentioned in
previous message also helps a little? If there is anything else we can
help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Todor Todorov - 13 Jul 2007 10:14 GMT
Hi Steven,

no further questions for the moment. I'll have to spend some time playing
with the technology.

Take care!

-- Todor

> Hi Todor,
>
[quoted text clipped - 10 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Steven Cheng[MSFT] - 16 Jul 2007 02:29 GMT
Thanks for your followup Todor,

Welcome to post any of your progress or result here.

Good luck!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
   

This posting is provided "AS IS" with no warranties, and confers no rights.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.