harakirinox

Basics for a plugin with C++ DLL

Recommended Posts

Hello everyone,

I am currently starting the process of coding a small plugin for Overwolf that will let me display onscreen information that I gather from a DLL that I write in C++ (e.g. some information on the system's state, CPU, memory, etc... network status and more).

I had a look at the up to date plugin sample (https://github.com/overwolf/overwolf-plugin-sample), unfortunately this is in C# and I do not practice nor clearly understand this programming language (yet!).

To get started I would like to simply modify the sample plugin so when it loads it retrieves an integer from my DLL, I think if I have that working I can manage the rest.

I tried to recreate the C# DLL in C++ but it fails miserably, the plugin simply does not even load.
First I had to create a main.cpp with the DllMain entry point, otherwise my compiler was screaming to death (perhaps that might be a problem), then I recreated the general structure of the DLL but it still does not work.
Here is what I attempted:

namespace overwolf {
	namespace samples {
		namespace plugins {
			class SamplePlugin {
			public:
				int SampleProperty = 55;
				//int SampleProperty(); // Also tried to return it from a member function, didn't work
			};
		}
	}
}

I tried without the namespaces too, no more luck.
In the JavaScript files that are supposed to load the plugin (main.js) the first lines are:

// create an instance of the plugin
var plugin = new OverwolfPlugin("OverwolfSamplePlugin", true);

// initialize it
plugin.initialize(function(status) {
  // ...

Nothing past plugin.initialize gets executed so I deduce that the plugin just fails to load (the dll is in the right directory with the right name, if I place the compiled C# DLL then it does work).

Is there the equivalent of the C# sample plugin DLL in C++ somewhere?
Otherwise could anyone guide me on how to make my C++ DLL work and being able to call its functions from the HTML/JavaScript please?

I am sure that for someone used to the Overwolf's plugin system this will be easy, as there are several things that I don't quite get (I have only done standalone executable and DLL loaded "classically" from their entry point, never done a plugin before.

Thank you very much for your help.

Share this post


Link to post
Share on other sites

You need to export methods from your c++ library, so that they are accessible from outside world.
Create an DLL export definition file:

LIBRARY "YourNameHere"

EXPORTS
    Start	@1
    Stop	@2
    OnError	@3


Then you need a c# plugin, just use the one from example and add this:

        [DllImport("YourNameHere.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Start();

        [DllImport("YourNameHere.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Stop();

        [DllImport("YourNameHere.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void OnError(IntPtr pfCallback);

       // Only needed for c++ callbacks
       public delegate void Register([MarshalAs(UnmanagedType.LPWStr)] string Base64Stream);
       Register FuncPtrCallbackOnError;

...
      // Called every time you start the c# plugin to register callbacks
      FuncPtrCallbackOnError = new Register(yourfuinctionhere); // Define that function it is the one that will be called from c++
      OnError(Marshal.GetFunctionPointerForDelegate(FuncPtrCallbackOnError));
...
     // No callbacks needed, then just call the function you need
     Start();
     Stop();


Like you can see OnError has a parameter, it is a callback function, you need to define this in your c++ library and use in callback function, for example like this:

typedef void(__stdcall * _ManagedCallbackOnError)(wchar_t Data[]);
static _ManagedCallbackOnError ManagedCallbackOnError;

// This is the exported method (available to call from c# plugin)
void OnError(_ManagedCallbackOnError ManagedCallback)
{
	ManagedCallbackOnError = ManagedCallback;
}

void Stop()
{
}

void Start()
{
}

...

// Call c# callback function
std::wstring Err(L"0x0001");
ManagedCallbackOnError((wchar_t *)Err.c_str());

Besides that, everything is same as in c# plugin so just define your callback function "yourfuinctionhere" and use info received from the callback - for example by sending it back to javascript.

Hope that helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now