diff options
Diffstat (limited to 'zen/dll.h')
-rw-r--r-- | zen/dll.h | 20 |
1 files changed, 11 insertions, 9 deletions
@@ -1,7 +1,7 @@ // ************************************************************************** // * This file is part of the FreeFileSync project. It is distributed under * // * GNU General Public License: http://www.gnu.org/licenses/gpl.html * -// * Copyright (C) ZenJu (zenju AT gmx DOT de) - All Rights Reserved * +// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved * // ************************************************************************** #ifndef DLLLOADER_H_INCLUDED @@ -20,9 +20,13 @@ Manage DLL function and library ownership - full value semantics Usage: - typedef BOOL (WINAPI* IsWow64ProcessFun)(HANDLE hProcess, PBOOL Wow64Process); - const zen::SysDllFun<IsWow64ProcessFun> isWow64Process(L"kernel32.dll", "IsWow64Process"); - if (isWow64Process) ... use function ptr ... + typedef BOOL (WINAPI* IsWow64ProcessFun)(HANDLE hProcess, PBOOL Wow64Process); + const zen::SysDllFun<IsWow64ProcessFun> isWow64Process(L"kernel32.dll", "IsWow64Process"); + if (isWow64Process) ... use function ptr ... + + Usage 2: + #define DEF_DLL_FUN(name) DllFun<dll_ns::FunType_##name> name(getDllName(), dll_ns::funName_##name); + DEF_DLL_FUN(funname1); DEF_DLL_FUN(funname2); DEF_DLL_FUN(funname3); */ template <class Func> @@ -32,15 +36,13 @@ public: DllFun() : fun(nullptr) {} DllFun(const wchar_t* libraryName, const char* functionName) : - hLibRef(new HMODULE(::LoadLibrary(libraryName)), deleter), - fun(*hLibRef ? reinterpret_cast<Func>(::GetProcAddress(*hLibRef, functionName)) : nullptr) {} + hLibRef(::LoadLibrary(libraryName), ::FreeLibrary), + fun(hLibRef ? reinterpret_cast<Func>(::GetProcAddress(static_cast<HMODULE>(hLibRef.get()), functionName)) : nullptr) {} operator Func() const { return fun; } private: - static void deleter(HMODULE* ptr) { if (*ptr) ::FreeLibrary(*ptr); delete ptr; } - - std::shared_ptr<const HMODULE> hLibRef; + std::shared_ptr<void> hLibRef; //we would prefer decltype(*HMODULE()) if only it would work... Func fun; }; |