Embedding a managed DLL in a .net 4.0 application

I successfully injected the managed DLL into the .net 3.5 application using the loader DLL (in C ++) and then my "payload" in the dll (C #).

When I try to do this in a .net 4.0 application, always crash.

Boot C ++:

#include "MSCorEE.h" void StartTheDotNetRuntime() { // Bind to the CLR runtime.. ICLRRuntimeHost *pClrHost = NULL; HRESULT hr = CorBindToRuntimeEx( NULL, L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pClrHost); hr = pClrHost->Start(); // Okay, the CLR is up and running in this (previously native) process. // Now call a method on our managed C# class library. DWORD dwRet = 0; hr = pClrHost->ExecuteInDefaultAppDomain( L"payload.dll", L"MyNamespace.MyClass", L"MyMethod", L"MyParameter", &dwRet); // Optionally stop the CLR runtime (we could also leave it running) hr = pClrHost->Stop(); // Don't forget to clean up. pClrHost->Release(); } 

C # payload:

  using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms; namespace MyNamespace { public class MyClass { // This method will be called by native code inside the target process... public static int MyMethod(String pwzArgument) { MessageBox.Show("Hello World"); return 0; } } } 

I tried to use the following fix, but to no avail, any ideas? fix ??:

  hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&lpRuntimeInfo); 
+6
source share
2 answers

Interfaces changed using .NET 4.0. Instead of CorBindToRuntimeEx you should use the new ICLRMetaHost interface .

The code may look something like this (without error checking):

 ICLRMetaHost *pMetaHost = NULL; CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); ICLRRuntimeInfo *pRuntimeInfo = NULL; pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo); ICLRRuntimeHost *pClrRuntimeHost = NULL; pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pClrRuntimeHost); pClrRuntimeHost->Start(); 
+11
source

I see a few "quirks" with your code - for example, CorBindToRuntimeEx corresponds to MS deprecated for .NET 4.

The .NET 4 script brings for the former the ability to simultaneously download multiple versions of the runtime into the same process, so I suspect that MS had to make some changes to esp. for the CLR host to make this happen ...

Here you can find recommended new interfaces.

+4
source

Source: https://habr.com/ru/post/896455/


All Articles