From f570e2f2685aa43aa518c2f8578391c1847cddbe Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:04:59 +0200 Subject: 3.3 --- shared/dllLoader.cpp | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'shared/dllLoader.cpp') 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 //includes "windows.h" +#include +#include 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 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(::GetProcAddress(KernelDllHandler::getInstance().getHandle(), functionName.c_str())); + const HINSTANCE libHandle = DllHandler::getInstance().getHandle(libraryName); + + if (libHandle != NULL) + return reinterpret_cast(::GetProcAddress(libHandle, functionName.c_str())); else return NULL; } -- cgit