ICLRMetaHost* pMetaHost = null;
ICLRRuntimeInfo* pRuntimeInfo = null;
bool bLoadable;
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo);
hr = pRuntimeInfo->IsLoadable(&bLoadable);
ICorRuntimeHost* pCorRuntimeHost = null;
hr = pRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (LPVOID*)&pCorRuntimeHost);
hr = pCorRuntimeHost->Start();
IUnknown* pUnk = null;
_AppDomain* pAppDomain = null;
hr = pCorRuntimeHost->GetDefaultDomain(&pUnk);
hr = pUnk->QueryInterface(IID_PPV_ARGS(&pAppDomain));
HANDLE hFile = CreateFile(L"E:\\Sources\\CSharp_Test4\\bin\\Debug\\CSharp_Test4.exe", GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD nNumberOfBytesRead = 0;
DWORD nFileSize = 0;
PVOID pFileBuffer = null;
nFileSize = GetFileSize(hFile, null);
pFileBuffer = VirtualAlloc(null, nFileSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (ReadFile(hFile, pFileBuffer, nFileSize, &nNumberOfBytesRead, null))
{
SAFEARRAYBOUND bounds[1];
bounds[0].cElements = (ULONG)nFileSize;
bounds[0].lLbound = 0;
SAFEARRAY* psaBytes;
psaBytes = SafeArrayCreate(VT_UI1, 1, bounds);
SafeArrayLock(psaBytes);
memcpy(psaBytes->pvData, pFileBuffer, nFileSize);
SafeArrayUnlock(psaBytes);
_Assembly* pAssembly;
hr = pAppDomain->Load_3(psaBytes, &pAssembly);
SafeArrayDestroy(psaBytes);
_MethodInfo* pEntryPoint;
hr = pAssembly->get_EntryPoint(&pEntryPoint);
VARIANT v1, v2;
v1.vt = VT_NULL;
v1.plVal = null;
hr = pEntryPoint->Invoke_3(v1, null, &v2);
pEntryPoint->Release();
pAssembly->Release();
}
VirtualFree(pFileBuffer, 0, MEM_RELEASE);
CloseHandle(hFile);
}