Dynamically loading a class from a dll.
Abstract: Includes a short explaination and simple example of using virtual base classes as interfaces across an exe and a run-time loaded dll.
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:
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. Example:/* This example consists of two projects and six source files: header files: "FooInterface.h" -- definition for IFoo, the base class/interface "FooClass.h" -- definition for FooClass, deriving from IFoo "DllExports.h" -- dll's exported functions dll project: "DllMain.cpp" -- main cpp file for the dll project "FooClass.cpp" -- contains implementation for FooClass exe project: "ExeMain.cpp" -- main cpp file for exe project */ //-------- FooInterface.h --------// #ifndef FOOINTERFACE_H #define FOOINTERFACE_H class IFoo { public: int GetNumber() = 0; void SetNumber( int & ) =0; }; #endif // FOOINTERFACE_H //-------- FooClass.h --------// #ifndef FOOCLASS_H #define FOOCLASS_H #include "FooInterface.h" class FooClass :public IFoo { public: FooClass(); const int& GetNumber(); void SetNumber( int & ); private: int number; }; #endif // FOOCLASS_H //-------- FooClass.cpp --------// FooClass::FooClass() { number = 0; } int FooClass::GetNumber() { return number; } void FooClass::SetNumber(int &arg) { number = arg; } //-------- DllExports.h --------// #ifndef DLLEXPORTS_H #defind DLLEXPORTS_H #ifdef __dll__ #define IMPEXP __declspec(dllexport) #else #define IMPEXP __declspec(dllimport) #endif // __dll__ #include "FooClass.h" extern "C" void* IMPEXP CreateFooClassInstance(); #endif // DLLEXPORTS_H //-------- DllMain.cpp --------// #define __dll__ #include "DllExports.h" int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*) { return 1; } void* IMPEXP CreateFooClassInstance(); { return static_cast< void* > (new FooClass); } //-------- ExeMain.cpp --------// #include "FooInterface.h" #include reference- http://edn.embarcadero.com/article/20165 |
No comments:
Post a Comment