-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.h
97 lines (77 loc) · 1.7 KB
/
library.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
** Çá¶È·â×°DLLµ÷ÓÃÏà¹Øº¯Êý
** author
** taoabc@gmail.com
*/
#ifndef ULT_LIBRARY_H_
#define ULT_LIBRARY_H_
#include <string>
#include <windows.h>
#include <vector>
namespace ult {
class Library {
public:
Library(void) : module_(NULL) {
};
~Library(void) {
Free();
}
operator HMODULE() const {
return module_;
}
HMODULE* operator&() {
return &module_;
}
bool IsLoaded(void) const {
return (module_ != NULL);
}
void Attach(HMODULE m) {
Free();
module_ = m;
}
HMODULE Detach(void) {
HMODULE m = module_;
module_ = NULL;
return m;
}
bool Free(void) {
if (module_ == NULL) {
return true;
}
if (FALSE == ::FreeLibrary(module_)) {
return false;
}
module_ = NULL;
return true;
}
bool Load(const std::wstring& filename) {
return LoadOperations(::LoadLibrary(filename.c_str()));
}
bool LoadEx(const std::wstring& filename, DWORD flags) {
return LoadOperations(::LoadLibraryEx(filename.c_str(), NULL, flags));
}
FARPROC GetProc(const std::string& procname) const {
return ::GetProcAddress(module_, procname.c_str());
}
FARPROC GetProc(const std::wstring& procname) const {
int len = (int)procname.length();
std::vector<char> buffer(len * 3);
int outlen = ::WideCharToMultiByte(CP_ACP, 0, procname.c_str(), len, buffer.data(), len*3, NULL, NULL);
std::string ansi_procname(buffer.data(), outlen);
return GetProc(ansi_procname);
}
private:
bool LoadOperations(HMODULE new_module) {
if (new_module == NULL) {
return false;
}
if (!Free()) {
return false;
}
module_ = new_module;
return true;
}
HMODULE module_;
};
} //namespace ult
#endif