Plugin fun

Ok, I am relatively new to C++, but not to General OOP, so I thought this would be a more appropriate place to post. First off, heres the sources:
Pluginable app: http://entityreborn.co.cc/PluginSDK.zip
Plugin: http://entityreborn.co.cc/add.zip

The idea is to load a plugin that uses a well known interface (by passing a string for the DLL/SO's name), and call a method of that class. Unfortunately, it aint working for me. Heck, I'll post most of the code here for those that want it...

main program file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "os_call.h"
#include <iostream>
#include <exception>
#include "Intrface.h"

using namespace std;

int main(int argc, char* argv[])
{
	Intrface *PlugObject;
	void *hDLL;
	cout << "Attempting to load library 'add'\n";
	hDLL = LoadSharedLibrary("add");

	if(hDLL)
	{
	    cout << "Attempting object retrieval\n";
            PlugObject = (Intrface*)GetFunction(hDLL,"GetPluginObject");
            if(PlugObject){
                cout << "Attempting 'add' call\n";
                int iTmp = PlugObject->Add(8,5);
                cout << "8 + 5 = " << iTmp;
            }
            else
            {
                cout << "Bad implementation\n";
            }
            FreeSharedLibrary(hDLL);
        }
	else
	{
	    cout << "Could not find library\n";
		return 1;
	}
	return 0;
}


interface:
1
2
3
4
class Intrface {
    public:
        virtual int Add (int, int)=0;
};


plugin .cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "add.h"

#ifdef _WIN32
	bool APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
				 )
#else
	 bool DllMain( void * hModule,
                       unsigned long  ul_reason_for_call,
                       void *lpReserved
				 )
#endif
{
    switch (ul_reason_for_call)
	{
		case 1://DLL_PROCESS_ATTACH:
            return true;
		case 2://DLL_THREAD_ATTACH:
            break;
		case 3://DLL_THREAD_DETACH:
            break;
		case 0://DLL_PROCESS_DETACH:
			break;
    }
    return true;
}

extern "C" ADD_API Intrface* GetPluginObject()
{
    PluginObject = new CAdd;
    return PluginObject;
}

extern "C" int CAdd::Add(int a, int b)
{
    return (a + b);
}


plugin header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef _WIN32
    #include <windows.h>
    #define ADD_API __declspec(dllexport)
#else
	#include "dlfcn.h"
	#define ADD_API
#endif

#include "Intrface.h"
class CAdd : public Intrface {
    public:
        int Add(int, int);
};

CAdd* PluginObject;


os_call.h is not relavent here, but download the PluginSDK.zip mentioned above to see it.

Both the plugin project and the app project have their own copy of the interface class, so that may be a problem, I dunno. Basically the problem is when PlugObject->Add(x,x); is called, my app segfaults. any ideas?
This is aimed at pure C++, and multi platform use.
Last edited on
I could further this with a question: as I will be using DLLs/SOs, I would assume global variables will not cause problems in my main app, so could I theoretically use classless libraries, and call exported methods from them to access data? The only drawback is I wanted to use interfaces to simplify the deal, but if this works and remains simple, I'll go for it. I use this design to get the PluginObject in the above code (GetPluginObject();), but didn't know if it would work in a full setup/environment.
Topic archived. No new replies allowed.