Archived

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

krakrazor

Class exporting in DLLS

Recommended Posts

I noticed VC++ 2003 defines exported classes like this: class __declspec(dllimport) CPASM // or export, don''t wanna bunch it up with defines in this example { public: CPASM(void); ~CPASM(void); // TODO: add your methods here. }; i''m used to doing it with an interface and defining a struct with virtual functions that inherits into a class and use a getinterface() function in the dll to create a new object for me... should i go back to this method or does VC++ 2003 make it easier to use function calls in run time load libraries?

Share this post


Link to post
Share on other sites
Export the factory function only.
You dont have to export the class at all.
The factory creates a new instance of
implementor bur returns a pointer to the
interface the implementor implements.
eg.

//common hdr

struct Abstract
{
virtual void Foo()=0;
}
enum{I1,I2};

//dll


class Implementor1 :Public Abstract
{
public:
virtual void Foo(){cout<<"I'm Implementor1::Foo"}
};

class Implementor2 :Public Abstract
{
public:
virtual void Foo(){cout<<"I'm Implementor2::Foo"}
};

declspec(__dllexport) Abstract* Factory(UINT what)
{
if (what==I1)
return (Abstract*)(new Implementor1());
return (Abstract*)(new Implementor2());
}

//exe

...dll stuff

Abstract* pa1 = (P2_Factory)(I1);
Abstract* pa2 = (P2_Factory)(I2);

pa1->Foo();
pa2->Foo();


// is it ok ?






[edited by - marius on January 25, 2004 1:04:48 PM]

[edited by - marius on January 25, 2004 1:06:11 PM]

[edited by - marius on January 25, 2004 1:10:28 PM]

Share this post


Link to post
Share on other sites