2

I have a byte array of a .NET application inside c++ code. I want to execute this .NET application without writing those bytes on the disk. ICLRRuntimeHost::ExecuteInDefaultAppDomain expects a path to the assembly so it's out of the equation here. I'm looking for a possible way (or hack) to pass the binary directly to the clr.

So what i can do ?

1
  • 2
    You can use the _AppDomain interface in native C++. Which lets you call its Load(byte[]) method. Use ICorRuntimeHost::GetDefaultDomain(). Commented Feb 11, 2017 at 1:18

1 Answer 1

1
//todo error checks/cleanup HRESULT hr; ICLRMetaHost *pMetaHost = NULL; ICLRRuntimeInfo *pRuntimeInfo = NULL; ICorRuntimeHost *pCorRuntimeHost = NULL; IUnknownPtr spAppDomainThunk = NULL; _AppDomainPtr spDefaultAppDomain = NULL; bstr_t bstrAssemblyName(L""); _AssemblyPtr spAssembly = NULL; bstr_t bstrClassName(L""); _TypePtr spType = NULL; variant_t vtEmpty; bstr_t bstrStaticMethodName(L"Main"); variant_t vtLengthRet; hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost)); const wchar_t* pszVersion = L"v2.0.50727"; hr = pMetaHost->GetRuntime(pszVersion, IID_PPV_ARGS(&pRuntimeInfo)); BOOL fLoadable; hr = pRuntimeInfo->IsLoadable(&fLoadable); if (!fLoadable) { wprintf(L".NET runtime %s cannot be loaded\n", pszVersion); return; } hr = pRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&pCorRuntimeHost)); hr = pCorRuntimeHost->Start(); hr = pCorRuntimeHost->GetDefaultDomain(&spAppDomainThunk); hr = spAppDomainThunk->QueryInterface(IID_PPV_ARGS(&spDefaultAppDomain)); SAFEARRAYBOUND bounds[1]; bounds[0].cElements = array_len; bounds[0].lLbound = 0; SAFEARRAY* arr = SafeArrayCreate(VT_UI1, 1, bounds); SafeArrayLock(arr); memcpy(arr->pvData, bytearray, array_len); SafeArrayUnlock(arr); hr = spDefaultAppDomain->Load_3(arr, &spAssembly); hr = spAssembly->GetType_2(bstrClassName, &spType); hr = spType->InvokeMember_3(bstrStaticMethodName, static_cast<BindingFlags>(BindingFlags_InvokeMethod | BindingFlags_Static | BindingFlags_Public), NULL, vtEmpty, nullptr, &vtLengthRet); SafeArrayDestroy(arr); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.