Managed Code


using
System;

using System.Collections.Generic;

using System.Text;

 

namespace ManagedApp

{

    class Program

    {

        static void Main(string[] args)

        {         

        }

 

        static int SayHello(string yourArg)

        {

            Console.WriteLine("Hello From the Managed World! " + yourArg);

            Console.ReadLine();

            return 0;

        }

    }

}


Call the SayHello method by hosting the CLR 


#include
"stdafx.h"

#include <assert.h>

#include <Mscoree.h>

 

 

int _tmain(int argc, _TCHAR* argv[])

{

    ICLRRuntimeHost* clrHost = NULL;

    HRESULT hr = CorBindToRuntimeEx(0, L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&clrHost);

    assert(SUCCEEDED(hr));

    clrHost->Start();

 

    ICLRControl* clrControl = NULL;

    hr = clrHost->GetCLRControl(&clrControl);

    assert(SUCCEEDED(hr));

 

    DWORD* ret = NULL;

    hr = clrHost->ExecuteInDefaultAppDomain(L"ManagedApp.exe"

                                            , L"ManagedApp.Program"

                                            , L"SayHello"

                                            , L"Native Stuff"

                                            , ret );

    assert(SUCCEEDED(hr));

    return 0;

}


Reference:
MSDN Library: Hosting the Common Language Runtime 
MSDN Magazine: CLR Hosting APIs
Book: Customizing the Microsoft .NET Framework Common Language Runtime
Posted on 2007-06-21 17:02 Adrian H. 阅读(...) 评论(...) 编辑 收藏