Using a class from a dll is easy when the dll is statically (or load-time) linked to your executable. The compiler gets the information it needs from the dll's header and the linker gets all information it needs from the import library. Periodically, someone posts to our newsgroups asking how to newsgroups load a class from a dll completely at run-time. The following is a simple answer to this question, using some of the basic techniques behind COM and CORBA. Create the class dll:
To use the class in your executable:
Make calls to the object using the interface pointer and these calls will foreward to the derived object. Don't forget to delete the pointer when done with the object. Depending on the complexity of the applicaiton, you may have to deal with reference counting issues to determine when to delete the pointer you received from the dll. This example does not take these considerations into account. |
Abstract: Includes a short explaination and simple example of using virtual base classes as interfaces across an exe and a run-time loaded dll.