Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have an injected C++ DLL, and, I'm trying to create a plugin system using C#. (I want to be able to load C# libs as plugins, and have the C# code trigger natives, kinda like a wrapper, or maybe trigger wrapped functions, whatever it takes to make this work.)

GTA IV Script Hook .Net, does exactly this, so, I know it's possible, but, I'm having trouble getting C++\C# to play nicely.

I have what is essentially a pure native C++ DLL, I created one .cpp\.h combo, which is compiled with CLR.

XML
#using <mscorlib.dll>
#using <HackNet.dll>
#include "DotNetPlugins.h"

using namespace HackNet;

void ExecuteNet()
{
    General::PrintFile("NetTest.log", "Net Test...");
}

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HackNet
{
    public class General
    {
        // Print File
        public static void PrintFile(string path, string text)
        {
            System.IO.File.WriteAllText(path, text);
        }
    }
}


My CLR cpp\h files, and C# lib are based on this article.

http://msdn.microsoft.com/en-us/magazine/cc188703.aspx[^]

But, it refuses to work(C++ runtime error R6033) on startup, telling me that I have a bug in my application, that I attempted to run it from a constructor, or DLLMain, neither of which is true. (The call is injected to a function that isn't invoked until I press a key, and that won't even work until the app is fully loaded, and considering it won't even launch..)

So, where am I going wrong, I know this is possible, I can name two projects that do this, unfortunately, no source code is available demonstrating how it was done. (My native DLL of course works fine, without the CLR stuff, etc,.)
Posted
Updated 30-May-12 10:08am
v4
Comments
[no name] 30-May-12 16:26pm    
Interestingly enough, I changed the class type of the C# lib to static, and rebuilt my C++ project, and now it works... (Not sure what fixed it.)

---

I've had some thoughts on accomplishing my goal, I believe I need to wrap my C++ functions, then for plugins, load the C++\CLI DLL in C#, use my wrapped functions, then inside my C++ project, load the C# lib, and execute them. (May be more to it than this, but, roughly, this seems about right.)
Sandeep Mewara 31-May-12 0:43am    
5!
[no name] 31-May-12 1:20am    
Thank you. :D

I just hope this information can help someone else in the future. :)
DelusionalLogic 1-Oct-12 16:46pm    
Would you mind getting me a BASIC example, i'm really struggling getting it to work. i don't even know where to start
[no name] 14-Oct-12 5:13am    
Check my other questions, there is a more in depth description of how to do this, I pretty much describe the whole process, should get you started. :)

1 solution

Answering it to just pop it out of unanswered question list.

OP solved it by himself. His updates:
Interestingly enough, I changed the class type of the C# lib to static, and rebuilt my C++ project, and now it works... (Not sure what fixed it.) --- I've had some thoughts on accomplishing my goal, I believe I need to wrap my C++ functions, then for plugins, load the C++\CLI DLL in C#, use my wrapped functions, then inside my C++ project, load the C# lib, and execute them. (May be more to it than this, but, roughly, this seems about right.)
 
Share this answer
 
Comments
[no name] 31-May-12 4:05am    
Yeah, sorry, should have done that myself, thanks. :)

Btw, I was correct, my native DLL now has managed classes, I can load the native DLL into C#, and use those exposed managed classes.

So, my native DLL also acts as a managed LIB, my C# lib uses resources from the managed LIB, my native DLL executes the C# code, which in turn is using managed resources from the native DLL. (Yeah, it's a bit on the confusing side, and was a pain to figure out.)

Regardless, I now have the core for a .Net based plugin system in a native DLL.
Sandeep Mewara 31-May-12 4:15am    
Not an issue at all. :)

you did great posting issue and then reporting it solved with 'how'. :thumbsup:

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