summaryrefslogtreecommitdiff
path: root/zen/dll.h
diff options
context:
space:
mode:
Diffstat (limited to 'zen/dll.h')
-rw-r--r--zen/dll.h39
1 files changed, 17 insertions, 22 deletions
diff --git a/zen/dll.h b/zen/dll.h
index e25e6916..f80a5f45 100644
--- a/zen/dll.h
+++ b/zen/dll.h
@@ -21,7 +21,7 @@ Manage DLL function and library ownership
Usage:
typedef BOOL (WINAPI* IsWow64ProcessFun)(HANDLE hProcess, PBOOL Wow64Process);
- const zen::DllFun<IsWow64ProcessFun> isWow64Process(L"kernel32.dll", "IsWow64Process");
+ const zen::SysDllFun<IsWow64ProcessFun> isWow64Process(L"kernel32.dll", "IsWow64Process");
if (isWow64Process) ... use function ptr ...
*/
@@ -29,11 +29,11 @@ template <class Func>
class DllFun
{
public:
- DllFun() : fun(NULL) {}
+ 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)) : NULL) {}
+ fun(*hLibRef ? reinterpret_cast<Func>(::GetProcAddress(*hLibRef, functionName)) : nullptr) {}
operator Func() const { return fun; }
@@ -50,10 +50,13 @@ template <class Func>
class SysDllFun
{
public:
- SysDllFun() : fun(NULL) {}
+ SysDllFun() : fun(nullptr) {}
- SysDllFun(const wchar_t* systemLibrary, const char* functionName) :
- fun(reinterpret_cast<Func>(::GetProcAddress(::GetModuleHandle(systemLibrary), functionName))) {}
+ SysDllFun(const wchar_t* systemLibrary, const char* functionName)
+ {
+ HMODULE mod = ::GetModuleHandle(systemLibrary);
+ fun = mod ? reinterpret_cast<Func>(::GetProcAddress(mod, functionName)) : nullptr;
+ }
operator Func() const { return fun; }
@@ -88,32 +91,24 @@ std::string getResourceStream(const std::wstring& libraryName, size_t resourceId
-//---------------Inline Implementation---------------------------------------------------
+//--------------- implementation---------------------------------------------------
inline
std::string getResourceStream(const wchar_t* libraryName, size_t resourceId)
{
- std::string output;
- HMODULE module = ::LoadLibrary(libraryName);
- if (module)
+ if (HMODULE module = ::LoadLibrary(libraryName))
{
- ZEN_ON_BLOCK_EXIT(::FreeLibrary(module));
+ ZEN_ON_SCOPE_EXIT(::FreeLibrary(module));
- const HRSRC res = ::FindResource(module, MAKEINTRESOURCE(resourceId), RT_RCDATA);
- if (res != NULL)
+ if (HRSRC res = ::FindResource(module, MAKEINTRESOURCE(resourceId), RT_RCDATA))
{
- const HGLOBAL resHandle = ::LoadResource(module, res);
- if (resHandle != NULL)
+ if (HGLOBAL resHandle = ::LoadResource(module, res))
{
- const char* stream = static_cast<const char*>(::LockResource(resHandle));
- if (stream)
- {
- const DWORD streamSize = ::SizeofResource(module, res);
- output.assign(stream, streamSize);
- }
+ if (const char* stream = static_cast<const char*>(::LockResource(resHandle)))
+ return std::string(stream, static_cast<size_t>(::SizeofResource(module, res))); //size is 0 on error
}
}
}
- return output;
+ return std::string();
}
}
bgstack15