3

Now that it is possible to have two CLRs running on the same box, how could they 'talk' to each other?

Let's say that the GUI is running under .NET 2.0 CLR and there is a script running on the .NET 4.0 CLR, for example, is there a way to modify the 2.0 based GUI from the 4.0 environment?

I have this exact issue when I use this technique to inject an .NET C# REPL environment into another .net process: Video: Injecting C# DLLs into Managed (C#) and Unmanaged (C++) processes


Note: I asked a similar question at Reddit and that version contains large number of references which will be useful if you are interested in the topic of Side-by-Side execution and CLR Hosting

Dinis Cruz
  • 4,161
  • 2
  • 31
  • 49

2 Answers2

7

Typically, in-process hosting of two separate CLRs is only done in the COM world. When you're using COM for extensibility, etc, the appropriate runtime is loaded. In this scenario, the CLR objects can "talk" to each other through COM, since COM is all about interoperability.

Pure managed applications will always end up running in the 4.0 CLR - so a 4.0 application loading a 2.0 assembly will end up executing the 2.0 assembly in CLR 4.

For details, see the CLR Inside Out: In-Process Side by Side article, which describes this in detail.

As for your specific examples:

Let's say that the GUI is running under .NET 2.0 CLR and there is a script running on the .NET 4.0 CLR, for example, is there a way to modify the 2.0 based GUI from the 4.0 environment?

If you try to load the 4.0 assembly directly, it'll fail. You'd have to use COM interop to load this, in which case all communication happens via COM. A 4.0 GUI application can load a 2.0 "script" -but it's loaded in the 4.0 runtime.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I saw that article (which I also linked on a similar question I asked at Reddit: http://www.reddit.com/r/dotnet/comments/vawqp/how_can_two_net_clr_loaded_in_the_same_process/) but there isn't a code sample in there. Do you know how in practice how that COM interop can happen? Note that I'm not trying to load assemblies in each of the CLRs, I'm trying to run code on the 2.0 CLR from the 4.0 CLR (for example change the title of a Form Control) – Dinis Cruz Jun 20 '12 at 01:12
  • @DinisCruz How are you loading the 2.0 assembly? (It matters...) – Reed Copsey Jun 20 '12 at 01:12
  • The 2.0 CLR is already running, what I do is inject a 4.0 dll into it (which will start the 4.0 CLR). The video linked from my question shows that in action – Dinis Cruz Jun 20 '12 at 02:54
  • @DinisCruz You're injecting into LINQPad in that video - which is CLR 4. You can inject .NET 2 assemblies into CLR 4, and they run under CLR 4 without issue... – Reed Copsey Jun 20 '12 at 15:29
  • Yes, that case works perfectly (when I inject into a .Net 4.0 CLR process) my issue is injecting into a .Net 2.0 CLR – Dinis Cruz Jun 21 '12 at 08:41
  • @DinisCruz You can't directly inject into 2.0 - you have to have the 2.0 project load it via com interop (or host, using native code, and entirely new CLR via the CLR hosting API) – Reed Copsey Jun 21 '12 at 14:55
  • Actually I can inject into the 2.0 CLR, since a 4.0 CLR is created to run my code. My issue is to talk/control the 2.0 CLR from the 4.0 one (since they are in the same process, I know it is possible, the 'only' issue is how hard it will be :) ) – Dinis Cruz Jun 21 '12 at 22:28
1

Theres two main ways, one is loosely coupled or tightly coupled.

In a loosely coupled system you would some form of bindings (JSON, XML, SOAP) to pass data back and forth. The benefit of this system is that you can use other programs in the future you may not have thought of to interact with the apps.

The other ways tightly coupled you can use plugins, or play around with the windows message pump to send messages back and forth. Also have a peek at the Windows Communication Framework

John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • Note that I don't have access to the 2.0 app, i.e. I cannot modify its code (i only have total control over the 4.0 code). That said, I could create that environment if I was able to run code under the 2.0 clr. – Dinis Cruz Jun 20 '12 at 01:07