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;
}
}