Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Demonstrating CLR Side-by-Side Execution

DZone's Guide to

Demonstrating CLR Side-by-Side Execution

·
Free Resource

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

0:009> lm vm mscorwks
start    end        module name
72890000 72e21000   mscorwks   (deferred)            
    Image path: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
    Image name: mscorwks.dll
    Timestamp:        Thu May 13 13:29:46 2010 (4BEBD49A)
    CheckSum:         0059AAE3
    ImageSize:        00591000
    File version:     2.0.50727.4952
    Product version:  2.0.50727.4952
    File flags:       0 (Mask 3F)
    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:     mscorwks.dll
    OriginalFilename: mscorwks.dll
    ProductVersion:   2.0.50727.4952
    FileVersion:      2.0.50727.4952 (win7RTMGDR.050727-4900)
    FileDescription:  Microsoft .NET Runtime Common Language Runtime - WorkStation
    LegalCopyright:   © Microsoft Corporation.  All rights reserved.
    Comments:         Flavor=Retail

And here’s the output from the managed COM objects—all they do is output the CLR version using Environment.Version:

Hello World from .NET 2.0
.NET version: 2.0.50727.4952
Hello World from .NET 4.0
.NET version: 4.0.30319.1

You can download the Visual Studio 2010 solution with the three projects I described above.

Topics:

Published at DZone with permission of Sasha Goldshtein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}