2011-09-02 3 views
6

Je succčs Injecté géré DLL dans un NET 3.5 application en utilisant un dll bootloader (en C++) et mon dll "charge utile" dans (C#).DLL dans Managed INJECTION DE .net 4.0 Application

Quand j'essayer de faire cela à une application .NET 4.0 est toujours tombe en panne.

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

     } 
    } 

J'ai essayé d'utiliser la solution ci-dessous, mais en vain, des idées? fix ??:

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

Répondre

11

Les interfaces ont été modifiées avec .NET 4.0. Au lieu d'utiliser CorBindToRuntimeEx, vous devez utiliser le nouveau ICLRMetaHostinterface.

Code pourrait ressembler à quelque chose comme ce qui suit (sans contrôle d'erreur):

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

Je vois plusieurs "bizarreries" avec votre code - par exemple CorBindToRuntimeEx est selon MS dépréciée pour 4 .NET.

Le temps d'exécution .NET 4 apporte pour la première la possibilité de charger côte à côte les versions d'exécution multiples dans le même processus, donc je soupçonne MS a dû faire quelques changements esp. au CLR hébergement pour y arriver ...

Vous pouvez trouver les nouvelles interfaces recommandées here.

Questions connexes