Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
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.

C#
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.)
Posted
Updated 1-Jun-12 0:19am
v5
Comments
Sandeep Mewara 1-Jun-12 8:57am    
5!

1 solution

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.)
 
Share this answer
 
v2
Comments
[no name] 7-Feb-13 4:02am    
I love how people vote things down, but, fail to provide alternatives, or any information regarding what they disliked about my solution.

I'm sure you do complex hacking all the time, and make events that span across several assemblies, crossing the native\managed boundaries multiple times... /rolls eyes

The fact of the matter is, this isn't easy, it's not a common scenario, and there certainly isn't some readily available source of answers for this type of problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900