将托管 DLL 注入 .net 4.0 应用程序
我已经使用引导加载程序 DLL(在 C++ 中)成功地将托管 DLL 注入到 .net 3.5 应用程序中,然后在(C#)中使用我的“有效负载”DLL。
当我尝试对 .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();
}
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;
}
}
}
我已尝试使用以下修复,但无济于事,有什么想法吗? 使固定??:
hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&lpRuntimeInfo);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 4.0 中的接口发生了变化。您应该使用新的
ICLRMetaHost
CorBindToRuntimeEx ="nofollow noreferrer">界面。代码可能如下所示(没有错误检查):
The interfaces changed with .NET 4.0. Instead of using
CorBindToRuntimeEx
you should use the newICLRMetaHost
interface.Code could look something like the following (without error checking):
我发现您的代码有几个“怪癖” - 例如
CorBindToRuntimeEx
根据 MS 的说法,.NET 4 已弃用。.NET 4 运行时首次提供了将多个运行时版本并排加载到同一进程中的能力,因此我怀疑 MS 必须进行一些更改,尤其是。到 CLR 托管来实现这一点...
您可以在 此处找到推荐的新接口。
I see several "quirks" with your code - for example
CorBindToRuntimeEx
is according to MS deprecated for .NET 4 .The .NET 4 runtime brings for the first the ability to load multiple runtime versions side-by-side into the same process so I suspect MS had to do some changes esp. to the CLR hosting to make this happen...
You can find the recommended new Interfaces here.