• 14
• 12
• 9
• 10
• 13

polymorphism and dlls

This topic is 4864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

my question is simple : i've read in an old book that you cant use polymorphism from dll , but the book is too old. is it still the same? does calling functions from dll support polymorphism ? and if yes is there any difference between it and polymorphism in the same exe (.c++ file) ?

Share on other sites
I assume you meant 'calling polymorphic _class_ functions from DLL'. Well, I don't have much experience with DLL's but in my Game Programming Gems #2 there's a chapter about exporting clasess from DLL, which doesn't say anything that's NOT possible.
Also, it wouldn't make really sense, think about how many DLL's provide implementation of interfaces, in this case it's a must for polymorphism.
Concluding, AFAIK that book is wrong.

Quote:
 and if yes is there any difference between it and polymorphism in the same exe (.c++ file) ?

There's only one type of polymorphism in C++ :-> and I don't think for DLL's it would be any different.

Share on other sites
Quote:
 Original post by KoshmaarThere's only one type of polymorphism in C++

Actually, C++ supports more than one kind of polymorphism, in particular it supports both statically typed dynamically bound polymorphism (using virtual functions) and dynamically typed statically bound polymorphism (using templates).

- Neophyte

Share on other sites
IMHO templates don't count becouse they don't use _dynamic_ polymporphism, compared to virtual functions. In case you want to argue, OK, but then we should call macro definitons also polymorphic :-> which really doesn't make sense.

Share on other sites
If the DLL and the using EXE were compiled with the same compiler (or with compilers that use compatible vtable layouts) then polymorphism works between a DLL and an EXE (or another DLL).

Thus, you can declare a pure virtual interface in a header:

class MyPlugin {  public:    virtual void initialize( Parameters const & p ) = 0;    virtual void doStuff( int what ) = 0;    virtual void release() = 0;};

Then, declare a factory function in your DLL:

class MyConcretePlugin : public MyPlugin {  public:    ...};__declspec(dllexport) MyPlugin * MakePlugin(){  return new MyConcretePlugin;}

Then, in your EXE, you can walk through a folder (call it "plugins") using FindFirtFile("plugins\\*.dll"), and LoadLibrary() each of the files you find, and GetProcAddress() on the function MakePlugin. Then call it, to create an implementation of the MyPlugin interface from each of the DLLs you find.