HRESULT ExecuteScript(const wchar_t* scriptPath, const vector<wstring>& arguments) { // Transfer all arguments to CLR // SafeArray safeArray; bstr_t bstrScriptPath = scriptPath; safeArray.CreateVector(VT_BSTR, 0, (ULONG)arguments.size()); for (size_t i = 0; i < arguments.size(); i++) { // Intentionally allocating string because SafeArray will automatically dispose of it. // safeArray.PutElement((LONG)i, SysAllocString(arguments[i].c_str())); } // Execute script function // CHECKCOM(instance->ExecuteScript(bstrScriptPath, safeArray)); return S_OK; }
HRESULT Initialize(const wchar_t* csDebugScriptUI) { // We should figure out needed runtime version // CAutoComPtr<ICLRMetaHostPolicy> pClrHostPolicy; CHECKCOM(CLRCreateInstance( CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy, (LPVOID*)&pClrHostPolicy)); wchar_t queriedRuntimeVersion[100] = { 0 }; DWORD length = sizeof(queriedRuntimeVersion) / sizeof(wchar_t); CHECKCOM(pClrHostPolicy->GetRequestedRuntime( METAHOST_POLICY_HIGHCOMPAT, csDebugScriptUI, nullptr, queriedRuntimeVersion, &length, nullptr, nullptr, nullptr, IID_PPV_ARGS(&runtimeInfo))); // Set custom memory manager and start CLR // CHECKCOM(runtimeInfo->BindAsLegacyV2Runtime()); //CHECKCOM(runtimeInfo->SetDefaultStartupFlags(clrStartupFlags, nullptr)); CHECKCOM(runtimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&clrRuntimeHost))); CHECKCOM(clrRuntimeHost->Start()); // Create a new AppDomain that will contain application configuration. // CAutoComPtr<IUnknown> appDomainSetupThunk; CAutoComPtr<IAppDomainSetup> appDomainSetup; CAutoComPtr<IUnknown> appDomainThunk; CHECKCOM(runtimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&corRuntimeHost))); CHECKCOM(corRuntimeHost->CreateDomainSetup(&appDomainSetupThunk)); CHECKCOM(appDomainSetupThunk->QueryInterface(IID_PPV_ARGS(&appDomainSetup))); CHECKCOM(corRuntimeHost->CreateDomainEx(L"MyDomain", appDomainSetup, nullptr, &appDomainThunk)); CHECKCOM(appDomainThunk->QueryInterface(IID_PPV_ARGS(&appDomain))); // Load our assembly // CAutoComPtr<_Assembly> mscorlibAssembly; CAutoComPtr<_Type> reflectionAssemblyType; SafeArray loadFromArguments; variant_t loadFromResult; variant_t arg1(csDebugScriptUI); loadFromArguments.CreateVector(VT_VARIANT, 0, 1); loadFromArguments.PutElement(0, &arg1); CHECKCOM(GetAssemblyFromAppDomain(appDomain, L"mscorlib", &mscorlibAssembly)); CHECKCOM(mscorlibAssembly->GetType_2(bstr_t(L"System.Reflection.Assembly"), &reflectionAssemblyType)); CHECKCOM(reflectionAssemblyType->InvokeMember_3(bstr_t(L"LoadFrom"), (BindingFlags)(BindingFlags_InvokeMethod | BindingFlags_Public | BindingFlags_Static), nullptr, variant_t(), loadFromArguments, &loadFromResult)); // Create our extension CLR instance // CAutoComPtr<_Assembly> assembly = (_Assembly*)(IDispatch*)loadFromResult; variant_t variant; CHECKCOM(assembly->CreateInstance_2(bstr_t(L"CsDebugScript.Executor"), true, &variant)); CHECKCOM(variant.punkVal->QueryInterface(&instance)); return S_OK; }