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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <windows.h>
#include <iostream>
//#include <tchar.h>
#include <list>
#include <pluginapi.h>
#include "plugin.h"
#include "service.h"
using namespace std;
list<plugin*> plugins;
list<service*> services;
bool shutdown_called = false;
int LoadModules();
INT_PTR CallService(const char *name, WPARAM w, LPARAM l);
HANDLE CreateServiceFunction(const char *name, SERVICE pService);
DWORD WINAPI OnModulesLoadedThread( LPVOID lpParam );
int ServiceExists(const char *name);
SERVICE Test(WPARAM, LPARAM);
SERVICE Shutdown(WPARAM, LPARAM);
PLUGINLINK link = {&CreateServiceFunction, &CallService, &ServiceExists};
int main(int argc, char *argv[])
{
if(LoadModules())
return 1; //something wrong
CreateServiceFunction("Core/Test", (SERVICE)Test);
CreateServiceFunction("Core/Shutdown", (SERVICE)Shutdown);
for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++) //initializing plugins
{
(*p)->getFuncs().load(&link);
}
for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++)
{
if((*p)->getFuncs().loaded)
{
CreateThread(NULL, 0, OnModulesLoadedThread, (LPVOID)(*p)->getFuncs().loaded, 0, 0);
}
}
for(;;)
{
if(shutdown_called)
{
plugin *db;
for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++)
{
if((*p)->getPluginInfo()->flags == F_DB_PLUGIN)
db = (*p);
else
delete (*p); //is it right? , at least do not crasing...
}
delete db;
plugins.clear();
break;
}
Sleep(1000);
}
return 0;
}
int LoadModules()
{
WIN32_FIND_DATAA findFileData;
HANDLE hFile = 0;
if(!(hFile = FindFirstFileA(".\\modules\\*", &findFileData)))
return 1; //failed to find any plugins in directory
HMODULE hPlugin = 0;
char tmp[MAX_PATH] = {0};
plugin::exported_funcs_s funcs;
while(hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_NO_MORE_FILES)
{
strcpy(tmp, ".\\modules\\");
strcat(tmp, findFileData.cFileName);
hPlugin = LoadLibraryA(tmp);
funcs.info = (SetPluginInfo)GetProcAddress(hPlugin, "SetPluginInfo");
funcs.load = (Load)GetProcAddress(hPlugin, "Load");
funcs.loaded = (OnModulesLoaded)GetProcAddress(hPlugin, "OnModulesLoaded");
funcs.unload = (Unload)GetProcAddress(hPlugin, "Unload");
if(funcs.info && funcs.load)
{
PLUGININFO *pi = funcs.info();
plugins.push_back(new plugin(hPlugin, funcs, pi));
}
else
FreeLibrary(hPlugin);
FindNextFileA(hFile, &findFileData);
}
return 0;
}
const HMODULE plugin::getHmodule()
{
return hModule;
}
int plugin::setHandle(const HMODULE &hMod)
{
if(!hMod)
return 1;
hModule = hMod;
return 0;
}
const plugin::exported_funcs_s plugin::getFuncs()
{
return funcs;
}
PLUGININFO *plugin::getPluginInfo()
{
return pluginInfo;
}
plugin::plugin(const HMODULE hMod, const exported_funcs_s fnct, PLUGININFO *info)
{
hModule = hMod;
funcs = fnct;
pluginInfo = info;
}
plugin::~plugin()
{
if(funcs.unload)
funcs.unload();
FreeLibrary(hModule);
}
service::service(const char *name, SERVICE service)
{
szName = new char [strlen(name)+1];
strcpy(szName, name);
pService = service;
}
const char *service::getName()
{
return szName;
}
const SERVICE service::getService()
{
return pService;
}
INT_PTR CallService(const char *name, WPARAM w, LPARAM l)
{
for(list<service*>::iterator p = services.begin(); p != services.end(); p++)
{
if(!strcmp((*p)->getName(), name))
{
SERVICE s = *(*p)->getService();
return s(w, l);
}
}
return 0;
}
HANDLE CreateServiceFunction(const char *name, SERVICE pService)
{
if(!ServiceExists(name))
services.push_back(new service(name, pService));
}
int ServiceExists(const char *name)
{
for(list<service*>::iterator p = services.begin(); p != services.end(); p++)
if(!strcmp((*p)->getName(), name))
return 1;
return 0;
}
SERVICE Test(WPARAM, LPARAM)
{
MessageBoxA(0, "Test service working", "INFO", MB_OK);
return 0;
}
SERVICE Shutdown(WPARAM, LPARAM)
{
shutdown_called = true;
}
DWORD WINAPI OnModulesLoadedThread( LPVOID lpParam )
{
OnModulesLoaded &l = (OnModulesLoaded&)lpParam;
l();
return 0;
}
|