| 299 | } |
| 300 | |
| 301 | DWORD StartCLR( |
| 302 | LPCWSTR dotNetVersion, |
| 303 | OUT ICLRMetaHost** ppClrMetaHost, |
| 304 | OUT ICLRRuntimeInfo** ppClrRuntimeInfo, |
| 305 | OUT ICorRuntimeHost** ppICorRuntimeHost, |
| 306 | OUT ICLRRuntimeHost** ppICLRRuntimeHost, |
| 307 | OUT ICLRControl** ppICLRControl, |
| 308 | OUT ICLRGCManager** ppICLRGCManager, |
| 309 | OUT AppDomain** ppAppDomain) { |
| 310 | |
| 311 | //Declare variables |
| 312 | HRESULT hr = NULL; |
| 313 | |
| 314 | //Get the CLRMetaHost that tells us about .NET on this machine |
| 315 | hr = Api.CLRCreateInstance(xCLSID_CLRMetaHost, xIID_ICLRMetaHost, (LPVOID*)ppClrMetaHost); |
| 316 | |
| 317 | if (hr == S_OK) |
| 318 | { |
| 319 | //Get the runtime information for the particular version of .NET |
| 320 | hr = (*ppClrMetaHost)->GetRuntime(dotNetVersion, xIID_ICLRRuntimeInfo, (LPVOID*)ppClrRuntimeInfo); |
| 321 | if (hr == S_OK) |
| 322 | { |
| 323 | /*Check if the specified runtime can be loaded into the process. This method will take into account other runtimes that may already be |
| 324 | loaded into the process and set fLoadable to TRUE if this runtime can be loaded in an in-process side-by-side fashion.*/ |
| 325 | BOOL fLoadable; |
| 326 | hr = (*ppClrRuntimeInfo)->IsLoadable(&fLoadable); |
| 327 | if ((hr == S_OK) && fLoadable) |
| 328 | { |
| 329 | |
| 330 | //Load the CLR into the current process and return a runtime interface pointer. |
| 331 | hr = (*ppClrRuntimeInfo)->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)ppICLRRuntimeHost); |
| 332 | IUnknown* pAppDomainThunk = NULL; |
| 333 | if (hr == S_OK) |
| 334 | { |
| 335 | // Get the GCManager interface so we can actually free stuff after unloading the appdomain via forcing GC |
| 336 | (*ppICLRRuntimeHost)->GetCLRControl(ppICLRControl); |
| 337 | (*ppICLRControl)->GetCLRManager(IID_ICLRGCManager, (LPVOID*)ppICLRGCManager); |
| 338 | |
| 339 | // Start it |
| 340 | (*ppICLRRuntimeHost)->Start(); |
| 341 | |
| 342 | // Apparently you can load COR Runtime after you start ICLRRuntime |
| 343 | hr = (*ppClrRuntimeInfo)->GetInterface(xCLSID_CorRuntimeHost, xIID_ICorRuntimeHost, (LPVOID*)ppICorRuntimeHost); |
| 344 | if (hr == S_OK) |
| 345 | { |
| 346 | // TODO: CHANGE |
| 347 | hr = (*ppICorRuntimeHost)->CreateDomain(L"yay", NULL, &pAppDomainThunk); |
| 348 | if (hr == S_OK) |
| 349 | { |
| 350 | hr = pAppDomainThunk->QueryInterface(xIID_AppDomain, (LPVOID*)ppAppDomain); |
| 351 | if (hr == S_OK) |
| 352 | { |
| 353 | return hr; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | DPRINT("[-] We could not query the AppDomain interface: 0x%llx\n", hr); |
| 358 | return hr; |