将托管DLL注入.net 4.0应用程序

将托管DLL注入.net 4.0应用程序,.net,dll,c#-4.0,code-injection,.net,Dll,C# 4.0,Code Injection,我已成功地使用引导加载程序DLL(在c++中)和我的“有效负载”DLL(在c#中)将托管DLL注入.NET3.5应用程序 当我尝试对.NET4.0应用程序执行此操作时,总是会导致崩溃 引导加载程序C++: #include "MSCorEE.h" void StartTheDotNetRuntime() { // Bind to the CLR runtime.. ICLRRuntimeHost *pClrHost = NULL;

我已成功地使用引导加载程序DLL(在c++中)和我的“有效负载”DLL(在c#中)将托管DLL注入.NET3.5应用程序

当我尝试对.NET4.0应用程序执行此操作时,总是会导致崩溃

引导加载程序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#:

我尝试过使用下面的修复程序,但没有效果,有什么想法吗? 修正??:

我看到您的代码中有几个“怪癖”——例如,MS针对.NET 4提出了弃用建议

NET 4运行时首次能够将多个运行时版本并行加载到同一进程中,因此我怀疑MS必须进行一些更改,尤其是对CLR宿主进行更改才能实现这一点


您可以找到推荐的新接口。

这些接口随.NET 4.0而更改。您应该使用新的
ICLRMetaHost
而不是使用
CorBindToRuntimeEx

代码可能如下所示(无错误检查):

    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;
         }

       }
    }
  hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&lpRuntimeInfo); 
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();