I have a C++ header declaring a class composed of pure virtual methods only. I have two DLLs using that header, (with one implementing that interface) but not linked at compile time. One DLL is loading the other one dynamically, passing a pointer of the implemented interface to the other. Are these DLL’s sharing the same virtual table structure?
Share
You’re safe.
The order in which the methods appear in the
vftableis determined by the base class structure, and that’s all you should care about. But this is compiler specific, so use the same compiler for generating the dll’s. Don’t rely on them being backward-compatible (or at least check the documentation).Assume you have the following header:
And you have
B.dllwith the following class:Now, in
X.dll, you receive a pointer to anAthat is aBobject created inB.dll.The call
will invoke
B::foo().One neat experiment you can try is to compile
B.dllwithheader.hand when you compileX.dll, invert the order of the methods inheader.h:In this case, although you should never do this, the same call to
test()inX.dllwill probably call the methodB::goo(). That is becauseX.dllassumes thevftablepresent in the header. It’s undefined behavior though; I just wrote this example to make a point.