-
Notifications
You must be signed in to change notification settings - Fork 457
/
DllParser.hpp
78 lines (65 loc) · 1.31 KB
/
DllParser.hpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#include <Windows.h>
#include <string>
#include <map>
#include <functional>
using namespace std;
class DllParser
{
public:
DllParser():m_hMod(nullptr)
{
}
~DllParser()
{
UnLoad();
}
bool Load(const string& dllPath)
{
m_hMod = LoadLibraryA(dllPath.data());
if (nullptr == m_hMod)
{
printf("LoadLibrary failed\n");
return false;
}
return true;
}
bool UnLoad()
{
if (m_hMod == nullptr)
return true;
auto b = FreeLibrary(m_hMod);
if (!b)
return false;
m_hMod = nullptr;
return true;
}
template <typename T>
std::function<T> GetFunction(const string& funcName)
{
auto it = m_map.find(funcName);
if (it == m_map.end())
{
auto addr = GetProcAddress(m_hMod, funcName.c_str());
if (!addr)
return nullptr;
m_map.insert(std::make_pair(funcName, addr));
it = m_map.find(funcName);
}
return std::function<T>((T*) (it->second));
}
template <typename T, typename... Args>
typename std::result_of<std::function<T>(Args...)>::type ExcecuteFunc(const string& funcName, Args&&... args)
{
auto f = GetFunction<T>(funcName);
if (f == nullptr)
{
string s = "can not find this function " + funcName;
throw std::exception(s.c_str());
}
return f(std::forward<Args>(args)...);
}
private:
HMODULE m_hMod;
std::map<string, FARPROC> m_map;
};