X
xfdsfsd
Guest
I have C++ dll which I want to inject to target program and I have written a wrapper for using this dll functions. I want to use injected dll functions from C# app. I tried CLR hosting it was succesful but problem is I can't unload C# app so I want to unload C# app and reload it in runtime without pulling injected DLL. It doesn't let me. Should I do this in C# app like using Reflection or in C++ dll ? Here my clr host code:
ICLRRuntimeHost* lpRuntimeHost = NULL;
ICLRRuntimeInfo* lpRuntimeInfo = NULL;
ICLRMetaHost* lpMetaHost = NULL;
FILE* file;
HRESULT hr;
DWORD WINAPI CreateDotNetRunTime(LPVOID lpParam)
{
LPWSTR AppPath = new WCHAR[_MAX_PATH];
::GetModuleFileNameW((HINSTANCE)&__ImageBase, AppPath, _MAX_PATH);
std::wstring tempPath = AppPath;
int index = tempPath.rfind('\\');
tempPath.erase(index, tempPath.length() - index);
tempPath += Assembly;
fopen_s(&file, Log, "a+");
hr = CLRCreateInstance(
CLSID_CLRMetaHost,
IID_ICLRMetaHost,
(LPVOID*)&lpMetaHost
);
if (FAILED(hr))
{
fprintf(file, "Failed to create CLR instance.\n");
fflush(file);
}
hr = lpMetaHost->GetRuntime(
L"v4.0.30319",
IID_PPV_ARGS(&lpRuntimeInfo)
);
if (FAILED(hr))
{
fprintf(file, "Getting runtime failed.\n");
fflush(file);
lpMetaHost->Release();
}
BOOL fLoadable;
hr = lpRuntimeInfo->IsLoadable(&fLoadable);
if (FAILED(hr) || !fLoadable)
{
fprintf(file, "Runtime can't be loaded into the process.\n");
fflush(file);
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
hr = lpRuntimeInfo->GetInterface(
CLSID_CLRRuntimeHost,
IID_PPV_ARGS(&lpRuntimeHost)
);
if (FAILED(hr))
{
fprintf(file, "Failed to acquire CLR runtime.\n");
fflush(file);
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
hr = lpRuntimeHost->Start();
if (FAILED(hr))
{
fprintf(file, "Failed to start CLR runtime.\n");
fflush(file);
lpRuntimeHost->Release();
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
DWORD dwRetCode = 0;
hr = lpRuntimeHost->ExecuteInDefaultAppDomain(
(LPWSTR)tempPath.c_str(),
Class,
Method,
Param,
&dwRetCode
);
if (FAILED(hr))
{
fprintf(file, "Unable to execute assembly.\n");
fflush(file);
lpRuntimeHost->Stop();
lpRuntimeHost->Release();
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
fclose(file);
return 0;
}
Continue reading...
ICLRRuntimeHost* lpRuntimeHost = NULL;
ICLRRuntimeInfo* lpRuntimeInfo = NULL;
ICLRMetaHost* lpMetaHost = NULL;
FILE* file;
HRESULT hr;
DWORD WINAPI CreateDotNetRunTime(LPVOID lpParam)
{
LPWSTR AppPath = new WCHAR[_MAX_PATH];
::GetModuleFileNameW((HINSTANCE)&__ImageBase, AppPath, _MAX_PATH);
std::wstring tempPath = AppPath;
int index = tempPath.rfind('\\');
tempPath.erase(index, tempPath.length() - index);
tempPath += Assembly;
fopen_s(&file, Log, "a+");
hr = CLRCreateInstance(
CLSID_CLRMetaHost,
IID_ICLRMetaHost,
(LPVOID*)&lpMetaHost
);
if (FAILED(hr))
{
fprintf(file, "Failed to create CLR instance.\n");
fflush(file);
}
hr = lpMetaHost->GetRuntime(
L"v4.0.30319",
IID_PPV_ARGS(&lpRuntimeInfo)
);
if (FAILED(hr))
{
fprintf(file, "Getting runtime failed.\n");
fflush(file);
lpMetaHost->Release();
}
BOOL fLoadable;
hr = lpRuntimeInfo->IsLoadable(&fLoadable);
if (FAILED(hr) || !fLoadable)
{
fprintf(file, "Runtime can't be loaded into the process.\n");
fflush(file);
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
hr = lpRuntimeInfo->GetInterface(
CLSID_CLRRuntimeHost,
IID_PPV_ARGS(&lpRuntimeHost)
);
if (FAILED(hr))
{
fprintf(file, "Failed to acquire CLR runtime.\n");
fflush(file);
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
hr = lpRuntimeHost->Start();
if (FAILED(hr))
{
fprintf(file, "Failed to start CLR runtime.\n");
fflush(file);
lpRuntimeHost->Release();
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
DWORD dwRetCode = 0;
hr = lpRuntimeHost->ExecuteInDefaultAppDomain(
(LPWSTR)tempPath.c_str(),
Class,
Method,
Param,
&dwRetCode
);
if (FAILED(hr))
{
fprintf(file, "Unable to execute assembly.\n");
fflush(file);
lpRuntimeHost->Stop();
lpRuntimeHost->Release();
lpRuntimeInfo->Release();
lpMetaHost->Release();
}
fclose(file);
return 0;
}
Continue reading...