I've seen a quite a few posts on messageboards and forums asking how to get "classes" working with DLLs (Dynamic Link Libraries). The issue is that DLLs use standard C-style linkage while C++ code generates C++ style linkage with different name decoration so you can't directly export member functions from a C++ class and then call them from client code. However it can be done by making use of interfaces which are shared between the DLL and the client. This method is very powerful and elegant since the client only sees the abstract interface, and the actual class object could be any class that implements that interface. This enables the client to load whatever DLL it wants and polymorphically treat the interfaces it receives from them as the same. Microsoft's COM (Component Object Model) uses the same idea with a lot of added functionality, however we usually don't need the all the features and overhead of COM in game oriented projects. This article will discuss how to use "classes" using COM style interfaces when using run-time linking or just as when using load-time linking. Before we start, you should be aware that all DLLs can have a DllMain() implementation. This is similar to WinMain, or main() in terms that its the entry point function for the DLL. The Operating System automatically calls this if its defined whenever the DLL is loaded, freed or when any threads attach or detach to it. If all you need is to make the DLL aware of any of these events, then this is all you should need. Usually however, this function doesn't lend itself to provide any other use apart from getting the DLL to handle the aforementioned events. To get more functionality out of a DLL, the programmer is mostly better off exporting his own set of functions. There are two methods to using DLLs from client code. The client can make use of loadtime linking or runtime linking. |
|||||||||||