diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:04:59 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:04:59 +0200 |
commit | f570e2f2685aa43aa518c2f8578391c1847cddbe (patch) | |
tree | b9376b3a7e807c5e0c4cf3d5615c14034d9675d6 /shared/dllLoader.cpp | |
parent | 3.2 (diff) | |
download | FreeFileSync-f570e2f2685aa43aa518c2f8578391c1847cddbe.tar.gz FreeFileSync-f570e2f2685aa43aa518c2f8578391c1847cddbe.tar.bz2 FreeFileSync-f570e2f2685aa43aa518c2f8578391c1847cddbe.zip |
3.3
Diffstat (limited to 'shared/dllLoader.cpp')
-rw-r--r-- | shared/dllLoader.cpp | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/shared/dllLoader.cpp b/shared/dllLoader.cpp index e37ded54..fbfa5b11 100644 --- a/shared/dllLoader.cpp +++ b/shared/dllLoader.cpp @@ -1,44 +1,55 @@ #include "dllLoader.h" #include <wx/msw/wrapwin.h> //includes "windows.h" +#include <map> +#include <assert.h> namespace { -class KernelDllHandler //dynamically load "kernel32.dll" +class DllHandler //dynamically load "kernel32.dll" { public: - static const KernelDllHandler& getInstance() + static DllHandler& getInstance() { - static KernelDllHandler instance; + static DllHandler instance; return instance; } - HINSTANCE getHandle() const + HINSTANCE getHandle(const std::wstring& libraryName) { - return hKernel; + HandleMap::const_iterator foundEntry = handles.find(libraryName); + if (foundEntry == handles.end()) + { + HINSTANCE newHandle = ::LoadLibrary(libraryName.c_str()); + handles.insert(std::make_pair(libraryName, newHandle)); + + assert(handles.find(libraryName) != handles.end()); + return newHandle; + } + else + return foundEntry->second; } private: - KernelDllHandler() : - hKernel(NULL) - { - //get a handle to the DLL module containing required functionality - hKernel = ::LoadLibrary(L"kernel32.dll"); - } + DllHandler() {} - ~KernelDllHandler() + ~DllHandler() { - if (hKernel) ::FreeLibrary(hKernel); + for (HandleMap::const_iterator i = handles.begin(); i != handles.end(); ++i) + if (i->second != NULL) ::FreeLibrary(i->second); } - HINSTANCE hKernel; + typedef std::map<std::wstring, HINSTANCE> HandleMap; + HandleMap handles; }; } -void* Utility::loadSymbolKernel(const std::string& functionName) +void* Utility::loadSymbol(const std::wstring& libraryName, const std::string& functionName) { - if (KernelDllHandler::getInstance().getHandle() != NULL) - return reinterpret_cast<void*>(::GetProcAddress(KernelDllHandler::getInstance().getHandle(), functionName.c_str())); + const HINSTANCE libHandle = DllHandler::getInstance().getHandle(libraryName); + + if (libHandle != NULL) + return reinterpret_cast<void*>(::GetProcAddress(libHandle, functionName.c_str())); else return NULL; } |