Sunday, August 29, 2010

Demonstrating CLR Side-by-Side Execution

Starting from CLR 4.0, multiple versions of the CLR can be loaded into the same process. This doesn’t mean that you can mix the 1.1 and 2.0 runtimes, but you can have CLR 2.0 loaded in the same process with CLR 4.0. This is great news for managed shell extension developers, managed COM object developers, and other library developers whose libraries are consumed by unmanaged code.
The CLR Inside Out: In-Process Side-by-Side column contains all the nitty-gritty details. One fact you should be aware of is that if you load a managed library that targets CLR 2.0 from a managed app that targets CLR 4.0, the managed library will use the 4.0 runtime.
Anyway, the point of this post is to show you a quick demo of having two CLR versions loaded into the same process. One way to do this is using the new CLR hosting interfaces that give you the ability to load two versions of the CLR. But that’s cheating.
Instead, I wrote two managed COM libraries. One of them targets CLR 2.0 and the other targets CLR 4.0. Next, I wrote a simple C++ application that loads both COM objects. Here’s the result of the lm command in WinDbg:
0:009> lm vm clr
start    end        module name
63240000 638af000   clr        (deferred)            
   Image path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
   Image name: clr.dll
   Timestamp:        Thu Mar 18 09:44:47 2010 (4BA1D9EF)
   CheckSum:         00671FC0
   ImageSize:        0066F000
   File version:     4.0.30319.1
   Product version:  4.0.30319.1
   File flags:       8 (Mask 3F) Private
   File OS:          4 Unknown Win32
   File type:        2.0 Dll
   File date:        00000000.00000000
   Translations:     0409.04b0
   CompanyName:      Microsoft Corporation
   ProductName:      Microsoft® .NET Framework
   InternalName:     clr.dll
   OriginalFilename: clr.dll
   ProductVersion:   4.0.30319.1
   FileVersion:      4.0.30319.1 (RTMRel.030319-0100)
   PrivateBuild:     DDBLD766
   FileDescription:  Microsoft .NET Runtime Common Language Runtime - WorkStation
   LegalCopyright:   © Microsoft Corporation.  All rights reserved.
   Comments:         Flavor=Retail

Read more: All Your Base Are Belong To Us