Click here to Skip to main content
11,641,087 members (61,105 online)
Click here to Skip to main content
Add your own
alternative version

Creating a host application for the .NET Common Language Runtime.

, 22 Oct 2001 153.7K 1.6K 57
This article explains how easy it is to write custom host applications for the .NET Common Language Runtime to run managed code.
// SimpleCLRHost.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <atlbase.h>
#include <mscoree.h>

#pragma message("@@--Run BuildManaged.Bat to build the HelloMsgBox.dll(It copies the dll to the debug directory) and then run this sample--@@" )

#import "C:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2914\\Mscorlib.tlb" raw_interfaces_only	

using namespace mscorlib;

int main(int argc, char* argv[])
	CComPtr<ICorRuntimeHost>	spRuntimeHost;
	CComPtr<_AppDomain>			spDefAppDomain;
	//Retrieve a pointer to the ICorRuntimeHost interface
	HRESULT hr = CorBindToRuntimeEx(
											 NULL,	//Retrieve latest version by default
											 L"wks",	//Request a WorkStation build of the CLR

	if (FAILED(hr)) return hr;

	//Start the CLR
	hr = spRuntimeHost->Start();

	CComPtr<IUnknown> pUnk;
	//Retrieve the IUnknown default AppDomain
	hr = spRuntimeHost->GetDefaultDomain(&pUnk);
	if (FAILED(hr)) return hr;

	hr = pUnk->QueryInterface(&spDefAppDomain.p);
	if (FAILED(hr)) return hr;

	CComPtr<_ObjectHandle> spObjectHandle;

	//Creates an instance of the type specified in the Assembly
	hr = spDefAppDomain->CreateInstance(
	if (FAILED(hr)) return hr;

	CComVariant VntUnwrapped;
	hr = spObjectHandle->Unwrap(&VntUnwrapped);
	if (FAILED(hr)) return hr;

	//We know our .NET component exposes IDispatch
	if (VntUnwrapped.vt != VT_DISPATCH)	return E_FAIL;

	CComPtr<IDispatch> pDisp;
	pDisp = VntUnwrapped.pdispVal;
	OLECHAR FAR* szMember = L"Hi";
	DISPID dispid;
	DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};

	//Retrieve the DISPID
	pDisp->GetIDsOfNames (

	//Invoke the method on the Dispatch Interface
	pDisp->Invoke (


	return 0;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ranjeet Chakraborty
Web Developer
United States United States
I am originally from Mumbai, India, Currently living in Los Angeles doing Software Development. If there is something I miss here in LA, its the crazy Mumbai monsoon and delicious "wadapavs".
Apart from spending time writing software, I spend most of my time with XBox riding the Warthog and killing covenants in Halo.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 23 Oct 2001
Article Copyright 2001 by Ranjeet Chakraborty
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid