-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLibraryHelper.h
51 lines (42 loc) · 939 Bytes
/
LibraryHelper.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
43
44
45
46
47
48
49
50
51
#pragma once
#include <Windows.h>
HMODULE GetKernelModule();
HMODULE GetModuleHandle2(_In_ LPCTSTR libFileName);
HMODULE LoadLibrary2(_In_ LPCTSTR lpFileName);
HMODULE LoadLibraryEx2(_In_ LPCTSTR lpFileName, _Reserved_ HANDLE hFile, _In_ DWORD flags);
struct LibraryInstance
{
public:
LibraryInstance(LPCTSTR libFileName)
{
m_module = LoadLibrary2(libFileName);
}
LibraryInstance(LPCTSTR libFileName, DWORD flags)
{
m_module = LoadLibraryEx2(libFileName, NULL, flags);
}
~LibraryInstance()
{
FreeLibrary(m_module);
}
inline bool IsValid() const
{
return m_module != nullptr;
}
inline HMODULE GetHandle() const
{
return m_module;
}
template<typename T>
T GetMethod(LPCSTR methodName) const
{
return reinterpret_cast<T>(GetProcAddress(m_module, methodName));
}
template<typename T>
T GetMethod(DWORD offset) const
{
return reinterpret_cast<T>(m_module + offset);
}
private:
HMODULE m_module;
};