windows - Exporting a C++ class from a DLL -
most of c/c++ development involves monolithic module files , absolutely no classes whatsoever, when need make dll accessible functions export them using standard __declspec(dllexport)
directive. access them either dynamically via loadlibrary()
or @ compile time header , lib file.
how do when want export entire class (and it's public methods , properties)?
is possible dynamically load class @ runtime , if so, how?
how header , lib compile time linking?
what late-binding? in loading loadlibrary() , getprocaddress() ? i'm used being able load library @ run time , great if here.
so there 2 ways load dll. first reference 1 or more symbols dll (your classname, example), supply appropriate import .lib , let linker figure out.
the second explicitly load dll via loadlibrary.
either approach works fine c-level function exports. can either let linker handle or call getprocaddress noted.
but when comes exported classes, typically first approach used, i.e., implicitly link dll. in case dll loaded @ application start time, , application fails load if dll can't found.
if want link class defined in dll, , want dll loaded dynamically, sometime after program initiation, have 2 options:
create objects of class using special factory function, internally have use (a tiny bit of) assembler "hook up" newly created objects appropriate offsets. has done @ run-time after dll has been loaded, obviously. explanation of approach can found here.
use delay-load dll.
all things considered... better go implicit linking, in case want use preprocessor technique shown above. in fact, if create new dll in visual studio , choose "export symbols" option these macros created you.
good luck...
Comments
Post a Comment