2011-09-02 27 views
6

我已成功將受管DLL注入到使用bootloader dll(C++)的.net 3.5應用程序中,然後在(c#)中使用我的「有效內容」dll。將受管DLL注入到.net 4.0應用程序

當我嘗試這樣做到.net 4.0應用程序總是崩潰。

Bootloader的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#:

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

回答

11

接口隨.NET 4.0改變。您應該使用新的ICLRMetaHostinterface而不是使用CorBindToRuntimeEx

代碼可能看起來像以下(不包括錯誤檢查):

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(); 
4

我看到幾個「怪癖」與您的代碼 - 例如CorBindToRuntimeEx根據MS棄用.NET 4。這個.NET 4運行時首次能夠將多個運行時版本並行加載到同一個進程中,所以我懷疑MS必須做一些改變。到CLR託管以實現此目的...

您可以找到推薦的新接口here

相關問題