-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.cpp
More file actions
87 lines (66 loc) · 1.74 KB
/
Loader.cpp
File metadata and controls
87 lines (66 loc) · 1.74 KB
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
#include <Windows.h>
#include <vector>
#include <iostream>
using namespace std;
class Plugin {
typedef char*(*f_GetName)();
typedef void(*f_DoWork)();
public:
HINSTANCE handle;
f_GetName GetName;
f_DoWork DoWork;
bool Load(string path) {
handle = LoadLibraryA(path.c_str());
if (!handle) return false;
GetName = (f_GetName)GetProcAddress(handle, "GetName");
if (!GetName) return false;
DoWork = (f_DoWork)GetProcAddress(handle, "DoWork");
if (!DoWork) return false;
return true;
}
void Unload() {
FreeLibrary(handle);
}
};
string GetPath(bool withoutName = true) {
char buffer[MAX_PATH];
GetModuleFileNameA(NULL, buffer, MAX_PATH);
if (withoutName) {
string::size_type pos = string(buffer).find_last_of("\\/");
return string(buffer).substr(0, pos);
}
else return string(buffer);
}
vector<Plugin*> loadedPlugins = vector<Plugin*>();
int main() {
SetConsoleTitle("PluginSystem");
HANDLE hFind;
WIN32_FIND_DATA data;
hFind = FindFirstFile((GetPath() + "\\plugins\\*.*").c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
string name = data.cFileName;
if (name.length() > 3) {
string extension = name.substr(name.length() - 3, 3);
if (strcmp(extension.c_str(), "dll") == 0) {
Plugin* plugin = new Plugin();
if (plugin->Load(GetPath() + "\\plugins\\" + data.cFileName)) {
cout << "Plugin '" << plugin->GetName() << "' loaded from File '" << name.c_str() << "'." << endl;
loadedPlugins.push_back(plugin);
}
else {
cerr << "Plugin could not be loaded" << endl;
}
}
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
while (true) {
for (Plugin* plugin : loadedPlugins) {
plugin->DoWork();
}
Sleep(1000);
}
return 0;
}