2011-09-02 16 views
6

He inyectado satisfactoriamente las DLL administradas en una aplicación .NET 3.5 utilizando un dll de cargador de arranque (en C++) y luego mi dll de "carga útil" en (C#).Inyección de DLL administrado en la aplicación .net 4.0

Cuando intento y hago esto para una aplicación .net 4.0 siempre se cuelga.

cargador de arranque 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(); 
    } 

carga útil 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; 
     } 

     } 
    } 

he intentado uso de la corrección de abajo, pero en vano, alguna idea? solución ??:

hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&lpRuntimeInfo); 

Respuesta

11

Las interfaces cambiaron con .NET 4.0. En lugar de usar CorBindToRuntimeEx, debe usar el nuevo ICLRMetaHostinterface.

código podría ser algo como lo siguiente (sin comprobación de errores):

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

Veo varias "peculiaridades" con su código - por ejemplo CorBindToRuntimeEx está de acuerdo con la EM en desuso para .NET 4.

El .NET 4 runtime trae por primera vez la capacidad de cargar varias versiones de tiempo de ejecución una al lado de la otra en el mismo proceso, así que sospecho que MS tuvo que hacer algunos cambios especialmente. al servidor CLR para que esto suceda ...

Puede encontrar las nuevas Interfaces recomendadas here.

Cuestiones relacionadas