My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://perevoznyk.wordpress.com
and update your bookmarks.

Friday, November 20, 2009

Accessing managed code from native application

// CLRHost.cpp : Hosting managed code in native Windows application
// This demo shows how to access the managed code from the native application
// and call the C# class method

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


int _tmain(int argc, _TCHAR* argv[])
{
 CComPtr<iclrruntimehost> pHost;
 DWORD dRetVal;
 CorBindToRuntimeEx(NULL, NULL, 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (void**)&pHost);
 pHost->Start();
        // Once you've loaded and started the CLR, the easiest way to load and run
        // your managed code is to use the ExecuteInDefaultApp-Domain 
        //method on ICLRRuntimeHost. This will simply load a managed assembly 
        // and execute a method on it:
 pHost->ExecuteInDefaultAppDomain(L"TestAssembly.dll", L"Test.MyClass", L"Execute", L"Hello", &dRetVal);
 pHost->Stop();
 return 0;
}