Iam trying to write a RunPe but for .net exe in c++.I know how to do it from unmanaged exe (fix iat, reloc etc), but Iam completely unexperienced in managed code. The concept is that the whole .net will be in an byte-array adn then call the Main function.The only working example I could find online is this, which loads from the disk and then invoke a function from that exe:
#include <iostream>
#include <metahost.h>
#include <corerror.h>
#pragma comment(lib, "mscoree.lib")
int main()
{
ICLRMetaHost* metaHost = NULL;
ICLRRuntimeInfo* runtimeInfo = NULL;
ICLRRuntimeHost* runtimeHost = NULL;
DWORD pReturnValue;
CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&metaHost);
metaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&runtimeInfo);
runtimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&runtimeHost);
runtimeHost->Start();
HRESULT res = runtimeHost->ExecuteInDefaultAppDomain(L"C:\labs\CLRHello1\CLRHello1\CLRHello1\bin\Debug\CLRHello1.exe", L"CLRHello1.Program", L"spotlessMethod", L"test", &pReturnValue);
if (res == S_OK)
{
std::cout << "CLR executed successfullyn";
}
runtimeInfo->Release();
metaHost->Release();
runtimeHost->Release();
r
eturn 0;
}
Will the clr take care of the relocations, fixing the IAT etc? If program’s namespace is needed I think it can be manually resolved from the clr Directory of the exe as well as the Runtime Version.