2011-09-02 20 views
6

Ho eseguito correttamente l'iniezione di DLL gestite in un'applicazione .net 3.5 utilizzando una dll del bootloader (in C++) e quindi la mia dll "payload" in (C#).Injecting DLL gestita nell'applicazione .net 4.0

Quando provo a fare questo a un'applicazione .net 4.0 è sempre in crash.

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

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

     } 
    } 

Ho provato con la correzione di seguito, ma senza alcun risultato, tutte le idee? fix ??:

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

risposta

11

Le interfacce sono state modificate con .NET 4.0. Invece di usare CorBindToRuntimeEx dovresti usare il nuovo ICLRMetaHostinterface.

codice potrebbe essere simile a quanto segue (senza il controllo degli errori):

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

vedo diverse "stranezze" con il codice - per esempio CorBindToRuntimeEx è secondo a MS deprecato for .NET 4.

Il runtime .NET 4 offre per prima la possibilità di caricare più versioni di runtime parallelamente nello stesso processo quindi ho il sospetto che MS abbia dovuto apportare alcune modifiche esp. al CLR hosting per fare in modo che questo accada ...

È possibile trovare le nuove interfacce consigliate here.