Hosting the CLR: 在Native程序中调用Managed Code

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
原文地址:https://www.cnblogs.com/Dah/p/792070.html