static int _getdomain(DWORD *pDomain) { *pDomain=0; ICLRRuntimeHost *pClrHost = NULL; HRESULT hr = CorBindToRuntimeEx( NULL, // version of the runtime to request NULL, // flavor of the runtime to request 0, // runtime startup flags CLSID_CLRRuntimeHost, // clsid of ICLRRuntimeHost IID_ICLRRuntimeHost, // IID of ICLRRuntimeHost (PVOID*)&pClrHost); // a pointer to our punk that we get back if (FAILED(hr)) { if (pClrHost != NULL) { pClrHost->Release(); } return false; } DWORD domain=0; hr=pClrHost->GetCurrentAppDomainId(&domain); pClrHost->Release(); pClrHost=NULL; if (FAILED(hr)) { return false; } *pDomain=domain; return true; }
STDAPI CorBindToRuntime(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor, REFCLSID rclsid, REFIID riid, LPVOID FAR *ppv) { return CorBindToRuntimeEx (pwszVersion, pwszBuildFlavor, 0, rclsid, riid, ppv); }
// Try to get the CLR 2.0 running - .Net 4+ MetaHost stuff not present. HRESULT LoadClr20(ICorRuntimeHost **ppHost) { HRESULT hr = E_FAIL; HMODULE hMscoree = NULL; hMscoree = LoadLibrary(L"mscoree.dll"); if (hMscoree == 0) { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_LOADMSCOREE, IDS_MSG_FOOTER_ENSURECLR20 ); hr = E_FAIL; } else { // Load the runtime pfnCorBindToRuntimeEx CorBindToRuntimeEx = (pfnCorBindToRuntimeEx)GetProcAddress(hMscoree, "CorBindToRuntimeEx"); if (CorBindToRuntimeEx == 0) { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_NOCORBIND, IDS_MSG_FOOTER_UNEXPECTED ); hr = E_FAIL; } else { // Attempt to load a runtime that is compatible with the release version of .Net 2.0. hr = CorBindToRuntimeEx(L"v2.0.50727", L"wks", NULL, CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (LPVOID*)ppHost); if (FAILED(hr)) { // Could not load the right version // Check whether version 2 is installed if (!DetectFxIsNet20Installed()) { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_NONET20, IDS_MSG_FOOTER_ENSURECLR20, hr); hr = E_FAIL; } else { // Check whether a version is already running if (GetModuleHandle(L"mscorwks") != NULL) { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_OLDVERSION, IDS_MSG_FOOTER_OLDVERSION); hr = E_FAIL; } else { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_CORBINDFAILED, IDS_MSG_FOOTER_ENSURECLR20ANDLOAD, hr); //// Unknown load failure //ShowMessage(IDS_MSG_HEADER_NEEDCLR20, // IDS_MSG_BODY_UNKNOWNLOADFAIL, // IDS_MSG_FOOTER_UNEXPECTED); hr = E_FAIL; } } //hr = E_FAIL; } else { // Check the version that is now loaded ... pfnGetCORVersion GetCORVersion = (pfnGetCORVersion)GetProcAddress(hMscoree, "GetCORVersion"); if (GetCORVersion == 0) { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_NOCORVERSION, IDS_MSG_FOOTER_UNEXPECTED ); hr = E_FAIL; } else { // Display current runtime loaded WCHAR szVersion[MAX_PATH + 1]; DWORD dwLength = MAX_PATH; hr = GetCORVersion(szVersion, dwLength, &dwLength); if (FAILED(hr)) { ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_CORVERSIONFAILED, IDS_MSG_FOOTER_UNEXPECTED, hr); hr = E_FAIL; } else { if ( DetectFxReadMajorVersion(szVersion) < 2 ) { // The version is no good. ShowMessage(IDS_MSG_HEADER_NEEDCLR20, IDS_MSG_BODY_WRONGVERSIONLOADED, IDS_MSG_FOOTER_REVIEWADDINS); hr = E_FAIL; } else { hr = S_OK; } } } } } FreeLibrary(hMscoree); } return hr; }
int _tmain(int argc, _TCHAR* argv[]) { // Bind to the runtime. ICLRRuntimeHost *pClrHost = NULL; HRESULT hrCorBind = CorBindToRuntimeEx( NULL, // Load the latest CLR version available L"wks", // Workstation GC ("wks" or "svr" overrides) 0, // No flags needed CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pClrHost); CheckFail(hrCorBind, "Bind to runtime failed (0x%x)"); // Construct our host control object. DHHostControl *pHostControl = new DHHostControl(pClrHost); if (!pHostControl) Fail("Host control allocation failed"); pClrHost->SetHostControl(pHostControl); // Now, begin the CLR. HRESULT hrStart = pClrHost->Start(); if (hrStart == S_FALSE) _ASSERTE(!L"Runtime already started; probably OK to proceed"); else CheckFail(hrStart, "Runtime startup failed (0x%x)"); // Construct the shim path (i.e. shim.exe). WCHAR wcShimPath[MAX_PATH]; if (!GetCurrentDirectoryW(MAX_PATH, wcShimPath)) CheckFail(HRESULT_FROM_WIN32(GetLastError()), "GetCurrentDirectory failed (0x%x)"); wcsncat_s(wcShimPath, sizeof(wcShimPath) / sizeof(WCHAR), L"\\shim.exe", MAX_PATH - wcslen(wcShimPath) - 1); // Gather the arguments to pass to the shim. LPWSTR wcShimArgs = NULL; if (argc > 1) { SIZE_T totalLength = 1; // 1 is the NULL terminator for(int i = 1; i < argc; i++) { // TODO: add characters for quotes around args w/ spaces inside them if (i != 1) totalLength++; // add a space between args totalLength += _tcslen(argv[i]) + 1; } wcShimArgs = new WCHAR[totalLength]; wcShimArgs[0] = '\0'; for(int i = 1; i < argc; i++) { if (i != 1) wcscat_s(wcShimArgs, totalLength, L" "); wcsncat_s(wcShimArgs, totalLength, argv[i], wcslen(argv[i])); } } if (wcShimArgs == NULL) Fail("Missing program path (host.exe <exePath>)\r\n"); // And execute the program... DWORD retVal; HRESULT hrExecute = pClrHost->ExecuteInDefaultAppDomain( wcShimPath, L"Shim", L"Start", wcShimArgs, &retVal); CheckFail(hrExecute, "Execution of shim failed (0x%x)\r\n"); if (wcShimArgs) delete wcShimArgs; // Stop the CLR and cleanup. pHostControl->ShuttingDown(); pClrHost->Stop(); pClrHost->Release(); return retVal; }