summaryrefslogtreecommitdiff
path: root/core/main.cpp
blob: 44be9a609a2dfd4bce5222ddc5b6c7fd851961e8 (plain)
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
#include <windows.h>
#include <iostream>
//#include <tchar.h>

#include <list>

#include <api.h>
#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
	return 0;
}

typedef PLUGININFO * (__cdecl * SetPluginInfo) ();
typedef int (__cdecl * Load) ();

struct exported_funcs_s
{
	SetPluginInfo info;
	Load load;
};

int LoadModules()
{
	WIN32_FIND_DATA findFileData;
	HANDLE hFile = FindFirstFile(".\\modules\\*", &findFileData);
	HMODULE hPlugin = 0;
	char tmp[MAX_PATH] = {0};
	exported_funcs_s funcs;
	while(hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_NO_MORE_FILES)
	{
		strcpy(tmp, ".\\modules\\");
		strcat(tmp, (char*)findFileData.cFileName);
		hPlugin = LoadLibrary(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(plugin(hPlugin ,pi->shortName));
			//cout<<pi->shortName;
			funcs.load();
		}
		FindNextFile(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;
}
plugin::plugin(const HMODULE &hMod, const char *name)
{
	hModule = hMod;
	szPluginName = new char [strlen(name)+1];
	strcpy(szPluginName, name);
}
plugin::~plugin()
{
	FreeLibrary(hModule);
	free(szPluginName);
}