Click here to Skip to main content
6,594,932 members and growing! (15,616 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

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

By Jason Gao

Hosting Common Language Runtime in Unmanaged Codes without the Dependency on .NET Framework
C++, Windows, .NET, Visual Studio, Dev
Posted:24 Jan 2006
Views:20,751
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 2.75 Rating: 2.88 out of 5
3 votes, 33.3%
1
1 vote, 11.1%
2
1 vote, 11.1%
3
2 votes, 22.2%
4
2 votes, 22.2%
5

Introduction

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
    STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN,
    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().

History

  • 24th January, 2006: Initial post

License

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

About the Author

Jason Gao


Member

Location: Canada Canada

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralComparative Advantage Pinmembermbaocha17:38 6 May '09  
QuestionExcellent, but a question to clarify why this happens? Pinmemberemailforjan13:03 26 Aug '08  
GeneralWhy not just.... Pinmemberleppie7:33 25 Jan '06  
GeneralRe: Why not just.... PinmemberJason Gao9:40 25 Jan '06  
GeneralRe: How to protect CLRHost->Start() PinmemberMember 46648465:28 18 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jan 2006
Editor: Deeksha Shenoy
Copyright 2006 by Jason Gao
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project