Code:
// This is the actual host object we need to keep in memory.
ICLRRuntimeHost* pClrHost = NULL;
unsigned __stdcall ThreadMain( LPVOID pParam )
{
// Get the policy object, so we can determine which runtime to use.
ICLRMetaHostPolicy* pMetaHostPolicy = NULL;
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy, (LPVOID*)&pMetaHostPolicy);
if (FAILED(hr))
{
MessageBox(NULL, L"Could not create a ICLRMetaHostPolicy object!", L"Injection - Error", MB_OK);
return 1;
}
ICLRRuntimeInfo* pRuntimeInfo = NULL;
// Get the runtime info object. Allow the assembly to tell US what runtime to use.
DWORD pcchVersion = 0;
DWORD dwConfigFlags = 0;
hr = pMetaHostPolicy->GetRequestedRuntime(METAHOST_POLICY_HIGHCOMPAT,
dllLocation.c_str(), NULL,
NULL, &pcchVersion,
NULL, NULL, &dwConfigFlags,
IID_ICLRRuntimeInfo,
(LPVOID*)&pRuntimeInfo);
if (FAILED(hr))
{
MessageBox(NULL, L"Could not create an ICLRRuntimeInfo object.", L"Injection - Error", MB_OK);
return 1;
}
// Allow the runtime to load .NET 2.0 mixed-mode libraries. (This covers 2.0-3.5 SP1)
hr = pRuntimeInfo->BindAsLegacyV2Runtime();
if (FAILED(hr))
{
MessageBox(NULL, L"Could not bind as legacy v2 runtime.", L"Injection - Error", MB_OK);
return 1;
}
hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pClrHost);
if (FAILED(hr))
{
MessageBox(NULL, L"Could not get an instance of ICLRRuntimeHost!", L"Injection - Error", MB_OK);
return 1;
}
hr = pClrHost->Start();
if (FAILED(hr))
{
MessageBox(NULL, L"Failed to start the CLR!", L"Injection - Error", MB_OK);
return 1;
}
DWORD dwRet = 0;
// Execute the Main func in the domain manager, this will block indefinitely.
// (Hence why we're in our own thread!)
hr = pClrHost->ExecuteInDefaultAppDomain(dllLocation.c_str(), NAMESPACE_AND_CLASS, MAIN_METHOD, MAIN_METHOD_ARGS, &dwRet);
if (FAILED(hr))
{
MessageBox(NULL, L"Failed to execute in the default app domain!", L"Injection - Error", MB_OK);
return 1;
}
return 0;
}
Edit: Note to self: when cutting out old code, make sure to leave the REQUIRED STUFF IN THERE