Click here to Skip to main content
15,881,801 members
Articles / Desktop Programming / ATL

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

Rate me:
Please Sign up or sign in to vote.
4.64/5 (16 votes)
22 Oct 20017 min read 192.1K   1.9K   60  
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
											 STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC, 
											 CLSID_CorRuntimeHost,
											 IID_ICorRuntimeHost,
											 (void**)&spRuntimeHost
											 );

	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(
													_bstr_t("HelloMsgBox"), 
													_bstr_t("Ranjeet.SimpleCLRHost.HelloHostDemo"),
													&spObjectHandle
													);
	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 (
								IID_NULL, 
								&szMember,
								1,
								LOCALE_SYSTEM_DEFAULT,
								&dispid
								);

	//Invoke the method on the Dispatch Interface
	pDisp->Invoke (
					  dispid,
					  IID_NULL,
					  LOCALE_SYSTEM_DEFAULT,
					  DISPATCH_METHOD,
					  &dispparamsNoArgs,
					  NULL,
					  NULL,
					  NULL
					  );

	spRuntimeHost->Stop();

	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.

License

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


Written By
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.

Comments and Discussions