load dll
-
How to load dll in pure C++ class?
-
How to load dll in pure C++ class?
What do you really mean? You know there's a function, namely LoadLibrary[^] to (explicitely) load a dynamic library.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
How to load dll in pure C++ class?
The question isn't very clear, I'm afraid. If you use "pure C++" to mean only standards-compliant language elements/libraries, the answer is that you can't. To start with, DLL files are specific for the Windows platform, so any solution would not work on Unix/Linux/MacOS etc. If you are concerned that the Windows function to load a DLL (
LoadLibrary
, as already noted) is a C function, and therefore should not for some reason be used in "pure C++", don't worry - a large reason for making C++ backwards compatible with C was that many operating systems offer their native APIs as C functions, and calling them is a perfectly valid use of C++. -
How to load dll in pure C++ class?
Here is an exception-free way :) :
class CSafeDllHolder {
HMODULE m_hModule;public:
CSafeDllHolder(LPCTSTR lpszLib) : m_hModule(::LoadLibrary(lpszLib)) {}
~CSafeDllHolder() { if (m_hModule) ::FreeLibrary(m_hModule); }HMODULE GetHandle() const { return m_hModule; }
//..
};They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)