本机功能连接到托管世界 [英] Native Function Hooks to The Managed World

查看:78
本文介绍了本机功能连接到托管世界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与确切答案无关,而与建议有关,而直截了当.我有一个复杂的场景(无论如何对我来说),我已经陷入困境,看不到解决方案.

1.我有一个本地.exe

2.我将一个本机DLL注入.exe中.
3.我已经在本机DLL中公开了托管资源

4.我在本机DLL中创建了一个插件系统,该系统可加载.Net(C#)插件,该插件又使用本机DLL中公开的托管资源

一切正常,我在这里没有问题.

我想做的是从.exe中获取数据,并将其一直移动到C#世界中,在此可以由插件制造商对其进行修改并发送回去. (我已经通过一些包装好的类等在某种程度上做到了这一点.)

问题出在这里:

我想从.exe中的函数挂钩触发托管事件.

我将这样的抽象类用于C#的使用,并作为我的插件系统主界面.

 公共 参考  class 脚本抽象
{
    公共:
        虚拟 无效 Update()=  0 ;
}; 



因此,任何编写插件的人都只会实现该类,并且.exe​​将通过已经存在的另一个钩子触发更新块.

因此,我希望我的事件可以从实现该基类的user(plugin)类进行预订. (我还需要能够传递数据,但是,让它保持简单,没有参数,等等.)

无论如何,以为我从.exe到DLL到C ++ \ CLI(托管),再到外部.Net插件,再到回头的方式,困扰着我,解决方案就不会出现在我眼前. (我确定我可以一起破解一些东西,但是,我希望得到一些专业人士的建议,因此我不会以某种怪异而告终,或者更糟的是重新发明任何轮子.)

(我将这个项目基于GTA IV Script Hook.Net,所以我知道我的所有目标都是可能的,但是我正在猜测实现方式,因为它没有源代码.) "h2_lin">解决方案

我想出了一个可行的解决方案.

我选择使用单独的C#库来管理我的插件,因为尝试在本机\受管世界之间传递它们的列表的难度约为此选项的两倍. :)

无论如何,这允许我创建一个基于CLR的.h \ .cpp,在其中定义了对C#PluginManager的调用,这些调用可以在任何地方进行,因为它们是外部的本机调用. (我从.exe内的函数挂钩触发加载,更新等)

由于我的插件管理器已经可以从本机访问,因此我认为可以使用它来发布事件,因此,我定义了一个新的本机-Event_TriggerxEvent,它在我的PluginManager中调用了相应的.Net函数.
显然,PluginManager已对我的插件进行了引用,因此,只需进行迭代即可,查看哪些插件实现了表示事件(反射)的虚函数,然后执行它.

到目前为止效果很好. :)

----

(自我报价,因为我的评论可能会在某些时候被删除.)

换句话说.

挂钩函数->本机触发器-> C#Lib PluginManager->迭代插件->执行目标方法

(出于所有意图和目的,这是一个事件系统.)


This is less about exact answers, and more about advice, and getting my head on straight. I have a complex scenario(for me anyways), and I''ve got myself twisted up, and can''t see the solution.

1. I have a native .exe

2. I have a native DLL injected into the .exe

3. I have managed resources exposed in my native DLL

4. I have created a plugin system in my native DLL that loads .Net(C#) plugins, which in turn uses the exposed managed resources from the native DLL

This all works fine, I have no problems here.

What I''m trying to do, is take data from the .exe, and move it all the way to the C# world, where it can be modified by plugin makers, and sent back. (I''ve already done this to an extent via some wrapped classes, etc,.)

The problem comes in here:

I want to trigger a managed event from a function hook in my .exe.

I use an abstract class like this for C# consumption, and as my plugin systems main interface.

public ref class Script abstract
{
    public:
        virtual void Update() = 0;
};



So anyone writing a plugin would just implement that class, and the .exe will trigger the update block via another hook already in place.

So, I want my events to be subscribe-able from user(plugin) classes that implement this base class. (I also need to be able to pass data through, but, let''s keep it simple, no params, etc,.)

Anyways, thinking my way from .exe to DLL to C++\CLI(managed), to external .Net plugins, and back, is frying my brain, and the solution just won''t come to me. (I''m sure I could hack something together, but, I wanted the advice of some pro''s, so I don''t end up with some monstrosity, or worse re-invent any wheels.)

(I''m basing this project on GTA IV Script Hook.Net, so I know that all of my goals are possible, but, I''m guessing at implementations, since no source code exists for it.)

解决方案

I''ve come up with a working solution.

I chose to use a separate C# lib to manage my plugins, because trying to pass a list of them between native\managed worlds looked about twice as hard as this option. :)

Anyways, this allowed me to create a CLR based .h\.cpp where I defined calls to my C# PluginManager, and these calls can made from anywhere, since they are native calls externally. (I trigger loading, updating, etc, from function hooks inside my .exe)

Since my plugin manager could already be accessed from natives, I figured that I could use it to issue events, so, I defined a new native - Event_TriggerxEvent, and it calls the corresponding .Net function in my PluginManager.

The PluginManager already has refs to my plugins, obviously, so it''s just a matter of iterating, seeing which plugins implement the virtual function representing the event(reflection), and executing it.

Works perfectly so far. :)

----

(Quoting myself, since my comment will likely be deleted at some point.)

To put that another way.

Hooked Function-> Native Trigger-> C# Lib PluginManager -> Iterate Plugins -> Execute Target Method

(It''s for all intents and purposes, an event system.)


这篇关于本机功能连接到托管世界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆