summaryrefslogtreecommitdiff
path: root/shared/dll_loader.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:14:37 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:14:37 +0200
commit8bf668665b107469086f16cb8ad23e47d479d2b4 (patch)
tree66a91ef06a8caa7cd6819dcbe1860693d3eda8d5 /shared/dll_loader.h
parent3.21 (diff)
downloadFreeFileSync-8bf668665b107469086f16cb8ad23e47d479d2b4.tar.gz
FreeFileSync-8bf668665b107469086f16cb8ad23e47d479d2b4.tar.bz2
FreeFileSync-8bf668665b107469086f16cb8ad23e47d479d2b4.zip
4.0
Diffstat (limited to 'shared/dll_loader.h')
-rw-r--r--shared/dll_loader.h69
1 files changed, 59 insertions, 10 deletions
diff --git a/shared/dll_loader.h b/shared/dll_loader.h
index 252e7598..e34fc4a9 100644
--- a/shared/dll_loader.h
+++ b/shared/dll_loader.h
@@ -7,7 +7,9 @@
#ifndef DLLLOADER_H_INCLUDED
#define DLLLOADER_H_INCLUDED
+#include <memory>
#include <string>
+#include "loki\ScopeGuard.h"
#ifdef __WXMSW__ //we have wxWidgets
#include <wx/msw/wrapwin.h> //includes "windows.h"
@@ -20,13 +22,35 @@
namespace util
{
-
/*
-load function from a DLL library, e.g. from kernel32.dll
-NOTE: you're allowed to take a static reference to the return value! :)
+Manage DLL function and library ownership
+ - thread safety: like built-in type
+ - full value semantics
+
+ Usage:
+ typedef BOOL (WINAPI *IsWow64ProcessFun)(HANDLE hProcess, PBOOL Wow64Process);
+ const util::DllFun<IsWow64ProcessFun> isWow64Process(L"kernel32.dll", "IsWow64Process");
+ if (isWow64Process)
*/
-template <typename FunctionType>
-FunctionType getDllFun(const std::wstring& libraryName, const std::string& functionName);
+
+template <class Func>
+class DllFun
+{
+public:
+ DllFun() : fun(NULL) {}
+
+ DllFun(const wchar_t* libraryName, const char* functionName) :
+ hLibRef(new HMODULE(::LoadLibrary(libraryName)), deleter),
+ fun(*hLibRef ? reinterpret_cast<Func>(::GetProcAddress(*hLibRef, functionName)) : NULL) {}
+
+ operator Func() const { return fun; }
+
+private:
+ static void deleter(HMODULE* ptr) { if (*ptr) ::FreeLibrary(*ptr); delete ptr; }
+
+ std::shared_ptr<HMODULE> hLibRef;
+ Func fun;
+};
/*
extract binary resources from .exe/.dll:
@@ -48,13 +72,38 @@ std::string getResourceStream(const std::wstring& libraryName, size_t resourceId
-//---------------Inline Implementation---------------------------------------------------
-FARPROC loadSymbol(const std::wstring& libraryName, const std::string& functionName);
-template <typename FunctionType> inline
-FunctionType getDllFun(const std::wstring& libraryName, const std::string& functionName)
+
+
+
+
+
+//---------------Inline Implementation---------------------------------------------------
+inline
+std::string getResourceStream(const wchar_t* libraryName, size_t resourceId)
{
- return reinterpret_cast<FunctionType>(loadSymbol(libraryName, functionName));
+ std::string output;
+ HMODULE module = ::LoadLibrary(libraryName);
+ if (module)
+ {
+ LOKI_ON_BLOCK_EXIT2(::FreeLibrary(module));
+
+ const HRSRC res = ::FindResource(module, MAKEINTRESOURCE(resourceId), RT_RCDATA);
+ if (res != NULL)
+ {
+ const HGLOBAL resHandle = ::LoadResource(module, res);
+ if (resHandle != NULL)
+ {
+ const char* stream = static_cast<const char*>(::LockResource(resHandle));
+ if (stream)
+ {
+ const DWORD streamSize = ::SizeofResource(module, res);
+ output.assign(stream, streamSize);
+ }
+ }
+ }
+ }
+ return output;
}
}
bgstack15