Archived

This topic is now archived and is closed to further replies.

StiNKy

Problem with classes and dlls

Recommended Posts

Hi. What I''m trying to do is be able to let my dll call functions of a class (which is in a different project file). I''m using VC++ 6 btw. So I want to pass the location in memory of the class to the dll function, but when I try and call some functions of the class I get linking problems. Anyone?

Share this post


Link to post
Share on other sites
To allow the DLL function to call the class methods (that''s what we OOP geeks call them ), you need to tell te compiler what kind of a class it is, by including the .h header file. But you also have to tell the linker about the functionality of the class, and this is done by including the .cpp source file in the DLL project, so the linker can find it.

Dormeur

Share this post


Link to post
Share on other sites
you can pass object-pointers to the dlls and like the dlls with abstract interface classes

e.g.

class IObject
{
virtual void Render() =NULL;
}

so you cannot create a object IObject, but you can use a IObject* to point to a class with just a pointer of the class

of course, you have to write 2 seperate classes, on CObject and one IObject and I... has to be abstract.

but be carefull, all virtual functions have to be in the same order.

rapso->greets();

Share this post


Link to post
Share on other sites
hey, it''S work, I do it all the time this way.
it''S easy;

the dll has an interface class.
class IObject
{
virtual void Render() =NULL;
virtual void Load() =NULL;
virtual void Save() =NULL;
}


and the exe has the real class
class CObject
{
virtual void Render();
virtual void Load();
virtual void Save();
}

somewhere in the exe
.
.
.
CObject *obj=new CObject;
.
.
.

then pass it to the dll with a functioncall
.
.
.
Render(obj);
.
.
.

in the dll:
void Render(void* object)
{
((IObject*)object)->Render();
}

rapso->greets("It really works and with virtual calls you don''t have to be worry about base and child classes");

Share this post


Link to post
Share on other sites
if a class has a virtual function it save a pointer to a list of functions which is saved in the class as a member.

class bla
{
virtual void run();
virtual void sit();
virtual void sleep();
}

sizeof(bla)==4BYTES cause there is a pointer to a functionlist.
so if you give a classpointer to a dll it recives the pointer to the functionlist.

so you need the same virtual-function order in the interface class and the real class.

hope it''s comprehensibly

rapso->greets();

Share this post


Link to post
Share on other sites