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

前端之家收集整理的这篇文章主要介绍了将托管DLL注入.net 4.0应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经成功地使用bootloader dll(在c中)将托管DLL注入到.net 3.5应用程序中,然后在(c#)中将我的“payload”dll注入.

当我尝试这样做.net 4.0应用程序总是崩溃.

Bootloader C:

  1. #include "MscoreE.h"
  2.  
  3. void StartTheDotNetRuntime()
  4. {
  5. // Bind to the CLR runtime..
  6. ICLRRuntimeHost *pClrHost = NULL;
  7. HRESULT hr = CorBindToRuntimeEx(
  8. NULL,L"wks",CLSID_CLRRuntimeHost,IID_ICLRRuntimeHost,(PVOID*)&pClrHost);
  9.  
  10. hr = pClrHost->Start();
  11.  
  12. // Okay,the CLR is up and running in this (prevIoUsly native) process.
  13. // Now call a method on our managed C# class library.
  14. DWORD dwRet = 0;
  15. hr = pClrHost->ExecuteInDefaultAppDomain(
  16. L"payload.dll",L"MyNamespace.MyClass",L"MyMethod",L"MyParameter",&dwRet);
  17.  
  18. // Optionally stop the CLR runtime (we could also leave it running)
  19. hr = pClrHost->Stop();
  20.  
  21. // Don't forget to clean up.
  22. pClrHost->Release();
  23. }

有效载荷C#:

  1. using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;
  2.  
  3. namespace MyNamespace
  4. {
  5. public class MyClass
  6. {
  7. // This method will be called by native code inside the target process...
  8. public static int MyMethod(String pwzArgument)
  9. {
  10. MessageBox.Show("Hello World");
  11. return 0;
  12. }
  13.  
  14. }
  15. }

我试过使用下面的修复程序,但没有用,有什么想法吗?
固定??:

  1. hr = pMetaHost->GetRuntime(L"v4.0.30319",IID_ICLRRuntimeInfo,(LPVOID*)&lpRuntimeInfo);
接口随.NET 4.0改变.您应该使用新的ICLRMetaHost interface,而不是使用CorBindToRuntimeEx.

代码可能类似于以下内容(没有错误检查):

  1. ICLRMetaHost *pMetaHost = NULL;
  2. CLRCreateInstance(CLSID_CLRMetaHost,IID_ICLRMetaHost,(LPVOID*)&pMetaHost);
  3.  
  4. ICLRRuntimeInfo *pRuntimeInfo = NULL;
  5. pMetaHost->GetRuntime(L"v4.0.30319",(LPVOID*)&pRuntimeInfo);
  6.  
  7. ICLRRuntimeHost *pClrRuntimeHost = NULL;
  8. pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost,(LPVOID*)&pClrRuntimeHost);
  9.  
  10. pClrRuntimeHost->Start();

猜你在找的Windows相关文章