C++手动加载CLR运行托管程序(CLR Hosting)(2)

                                                 L"Test"

                                                 L"Hello World!"

                                                 &dwRet); 

 

    hr = pRuntimeHost->Stop(); 

 

cleanup: 

    if(pRuntimeInfo != nullptr) 

    { 

        pRuntimeInfo->Release(); 

        pRuntimeInfo = nullptr; 

    } 

 

    if(pRuntimeHost != nullptr) 

    { 

        pRuntimeHost->Release(); 

        pRuntimeHost = nullptr; 

    } 

 

    if(pMetaHost != nullptr) 

    { 

        pMetaHost->Release(); 

        pMetaHost = nullptr; 

    } 

#include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include <windows.h> #include <metahost.h> #include <mscoree.h> #pragma comment(lib, "mscoree.lib") int _tmain(int argc, _TCHAR* argv[]) { ICLRMetaHost *pMetaHost = nullptr; ICLRMetaHostPolicy *pMetaHostPolicy = nullptr; ICLRRuntimeHost *pRuntimeHost = nullptr; ICLRRuntimeInfo *pRuntimeInfo = nullptr; HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); if(FAILED(hr)) { goto cleanup; } hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost)); hr = pRuntimeHost->Start(); DWORD dwRet = 0; hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"SampleManagedApp.exe", L"SampleManagedApp.Program", L"Test", L"Hello World!", &dwRet); hr = pRuntimeHost->Stop(); cleanup: if(pRuntimeInfo != nullptr) { pRuntimeInfo->Release(); pRuntimeInfo = nullptr; } if(pRuntimeHost != nullptr) { pRuntimeHost->Release(); pRuntimeHost = nullptr; } if(pMetaHost != nullptr) { pMetaHost->Release(); pMetaHost = nullptr; } }

相应的托管代码如下,

using System; 

 

namespace SampleManagedApp 

    class Program 

    { 

        static void Main(string[] args) 

        { 

        } 

 

        public static int Test(string s) 

        { 

            Console.WriteLine(s); 

            return 0; 

        } 

    } 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/20299fee26faec5d3ae6bff0af8d7f0c.html