I have an application in C++ that uses several types of objects. The application can interact with a plugin via C interfaces, which means that objects cannot be shared.
My question is:
knowing that the application and the plugin have the exact same "class myClass" definition, how would I share that object in a simple way between the application and plugin? (i.e. no memory allocation or such, just reading the class members for instance).
As an example, following class:
class myClass
{
public:
myClass();
myClass(int a value);
virtual ~myClass();
void doSomething();
myClass* child1;
myClass* child2;
int someValue;
char* someBuffer;
}
I can create an instance of above class in my application, then send it to the plugin in following way:
myClass* myObject=new myClass(42); pluginEntryFunction_doSomething((void*)myObject);
On the plugin side, I would have:
extern "C" __declspec(dllexport) void pluginEntryFunction(void* _theObject)
{
// Following is wrong and dangerous:
myClass* theObject=(myClass*)_theObject;
}
But that would be wrong and dangerous since the plugin might have used a different compiler or arranged the class structure differently (e.g. the member variable "someValue" might be located in the memory after "someBuffer")
But there is probably a way to handle this situation, by first telling the plugin how the data arrangement is made in the application. Something like:
myObject* myObject=new myClass(42); int member_someValueOffset=((void*)(&someValue))-((void*)myObject); int member_someBufferOffset=((void*)(&someBuffer))-((void*)myObject); pluginEntryFunction_initialize(member_someValueOffset,member_someBufferOffset);
Or is there a better way of doing this?
Thanks for any insight!







