Click here to Skip to main content
13,898,182 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 24 Jan 2006
Licenced CPOL

Hosting Common Language Runtime in Unmanaged Codes without the Dependency on .NET Framework

, 24 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Hosting Common Language Runtime in Unmanaged Codes without the Dependency on .NET Framework


The Hosting APIs of Common Language Runtime (CLR) provide an easy way to dynamically launch an application written in managed code in the unmanaged world. The topic has been well discussed and is out of the scope of this article.

This article will talk about an issue that you will face when using CLRs Hosting APIs. Suppose you want to write an EXE launcher that can launch executables written in managed and unmanaged codes. It is expected that the launcher can run without the .NET Framework if it does not launch any managed codes; the .NET Framework is required only when trying to launch a managed application.

The commonly used method to host CLR in an unmanaged program is to call CorBindToRuntimeEx. However, in doing so, you will quickly find out the problem that your application cannot work without mscoree.dll, an unexpected dependency on the .NET Framework.

The cause of the issue is that CorBindToRuntimeEx is exported from mscoree.dll. It will link to mscoree.dll at runtime. To resolve the dependency issue, we must avoid any functions provided by mscoree.dll.

The purpose of calling CorBindToRuntimeEx is to create an instance of the ICorRuntimeHost interface pointer. So we use CoCreateInstance to create an ICorRuntimeHost instance instead of using CorBindToRuntimeEx.

The following code explains the details:

CComPtr<ICorRuntimeHost> spRuntimeHost;
CComPtr<_AppDomain> spAppDomain;
CComPtr<IUnknown> spUnk;
/* Code has the .NET dependency 
if ( FAILED( CorBindToRuntimeEx( NULL, // Latest Version by Default
    L"wks", // Workstation build
    CLSID_CorRuntimeHost ,
    IID_ICorRuntimeHost ,
    (void**)&spRuntimeHost) ) ) {
    return false;
/* Code without .NET dependency 
HRESULT hr = spRuntimeHost.CoCreateInstance(__uuidof(CorRuntimeHost));
if (FAILED(hr)) {
    return false;

Additionally, CoInitialize() and CoUninitialize() must be called before and after the call to CoCreateInstance().


  • 24th January, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jason Gao
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralComparative Advantage Pin
mbaocha6-May-09 16:38
membermbaocha6-May-09 16:38 
QuestionExcellent, but a question to clarify why this happens? Pin
emailforjan26-Aug-08 12:03
memberemailforjan26-Aug-08 12:03 
GeneralWhy not just.... Pin
leppie25-Jan-06 6:33
memberleppie25-Jan-06 6:33 
GeneralRe: Why not just.... Pin
Jason Gao25-Jan-06 8:40
memberJason Gao25-Jan-06 8:40 
GeneralRe: How to protect CLRHost-&gt;Start() Pin
txALI18-Jan-08 4:28
membertxALI18-Jan-08 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by Jason Gao
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid