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...
#include "add.h"
#ifdef _WIN32
bool APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
#else
bool DllMain( void * hModule,
unsignedlong ul_reason_for_call,
void *lpReserved
)
#endif
{
switch (ul_reason_for_call)
{
case 1://DLL_PROCESS_ATTACH:
returntrue;
case 2://DLL_THREAD_ATTACH:
break;
case 3://DLL_THREAD_DETACH:
break;
case 0://DLL_PROCESS_DETACH:
break;
}
returntrue;
}
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.
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.