polymorphism and dlls

Started by
3 comments, last by hplus0603 19 years, 4 months ago
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) ?
Advertisement
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.
Quote:Original post by Koshmaar
There'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
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.
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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement