-
Notifications
You must be signed in to change notification settings - Fork 42
/
dl.h
42 lines (38 loc) · 951 Bytes
/
dl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef DL_H
#define DL_H
#include <string>
#include <stdexcept>
#include "win32util.h"
#include "strutil.h"
class AutoCast {
FARPROC m_pointer;
public:
AutoCast(FARPROC p): m_pointer(p) {}
template <typename U>
operator U*() { return reinterpret_cast<U*>(m_pointer); }
};
class DL {
std::shared_ptr<HINSTANCE__> m_module;
public:
DL() {}
DL(HMODULE handle, bool own=true)
{
if (own)
m_module.reset(handle, FreeLibrary);
else
m_module.reset(handle, [](HMODULE){});
}
bool load(const std::wstring &path)
{
HMODULE handle = LoadLibraryW(path.c_str());
if (handle) m_module.reset(handle, FreeLibrary);
return loaded();
}
bool loaded() const { return m_module.get() != 0; }
void reset() { m_module.reset(); }
AutoCast fetch(const char *name)
{
return AutoCast(GetProcAddress(m_module.get(), name));
}
};
#endif