Dynamically loading a DLL within another DLL
I need to load a dll within another dll (Visual C++ both), so I can use the classes embedded in the first one in the second. I have the code of both, since I created both dll's, but I have开发者_运维知识库 never dynamically loaded a library so I'm not sure how this is done...
Besides, I want to make this multi-platform, I already compiled both as .dll and .so, so it would be great a platform independent method to do this...otherwise, I can use macros to include windows or linux specific code.
Best regards and many thanks
The functions to load the library dynamically are: LoadLibrary (Windows), dlopen (Linux). To get the symbols: GetProcAddress (Windows), dlsym (LInux). Close the Open Library: FreeLibrary (Windows), dlclose(Linux). There is an article of how to load classes dynamically on windows: http://www.codeproject.com/KB/DLL/classesexportedusingLL.aspx and Linux: http://www.linuxjournal.com/article/3687?page=0,0. And there is code to load the libraries dynamically on windows and linux: http://www.sview.ru/sources/libexample/loadLibrary.h. I hope this could help you.
There is additional information about load classes dynamically on windows and linux:
http://www.codeguru.com/cpp/w-p/win32/article.php/c1443 (Windows). http://www.faqs.org/docs/Linux-mini/C++-dlopen.html#loadingclasses (Linux).
Typically just compile and link the lowest-level DLL. That will create the DLL itself and a .LIB file. Compile and link the next DLL up the chain, linking against that .LIB file. Continue up the chain until you reach the .EXE that (typically) nothing else links against.
精彩评论