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
|
#include <windows.h>
#include <iostream>
//#include <tchar.h>
#include <list>
#include <pluginapi.h>
#include "plugin.h"
using namespace std;
list<plugin*> plugins;
int LoadModules();
int main(int argc, char *argv[])
{
if(LoadModules())
return 1; //something wrong
for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++)
{
cout<<"Loaded plugin: "<<(*p)->getName()<<'\n';
(*p)->getFuncs().load();
}
for(;;)
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");
if(funcs.info && funcs.load)
{
PLUGININFO *pi = funcs.info();
plugins.push_back(new plugin(hPlugin, funcs, pi->shortName));
}
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;
}
int plugin::setName(const char *name)
{
if(strlen(name)< 2)
return 1;
szPluginName = new char [strlen(name)+1];
strcpy(szPluginName, name);
return 0;
}
const char* plugin::getName()
{
return szPluginName;
}
const plugin::exported_funcs_s plugin::getFuncs()
{
return funcs;
}
plugin::plugin(const HMODULE hMod, const exported_funcs_s fnct, const char *name)
{
hModule = hMod;
funcs = fnct;
szPluginName = new char [strlen(name)+1];
strcpy(szPluginName, name);
}
plugin::~plugin()
{
FreeLibrary(hModule);
free(szPluginName);
}
|