-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_3.cpp
94 lines (80 loc) · 2.44 KB
/
2_3.cpp
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
#include "PluginEnumerator.h"
#include <dirent.h>
#include <dlfcn.h>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
#define MAX_D_NAME_LENGTH 256 /*最大路径字符长度*/
bool dlFunctionHook(string path, char *op);
int main(int argc, char *argv[])
{
if (argc != 2) //需要给出调用参数
{
std::cout << "Most used commands:" << std::endl;
std::cout << "\thelp" << std::endl;
std::cout << "\t[FuncID]" << std::endl;
return 0;
}
//vector of plugin path
std::vector<std::string> strPluginPath;
//get plugin path
PluginEnumerator pluginEnum;
if (!pluginEnum.GetPluginNames(strPluginPath))
{
std::cout << "Get plugin names error" << std::endl;
return 0;
}
int num_argv1 = atoi(argv[1]); // Convert string to number
int strPluginsize = strPluginPath.size(); // plugin number
if (strcmp(argv[1], "help") == 0) // if input help
{
//Cycle call help() in all plugin
for (int i = 0; i < strPluginsize; i++)
{
dlFunctionHook(strPluginPath[i], (char *)"help");
}
}
else if (num_argv1 >= 0 && num_argv1 <= strPluginsize)// cheak input number in bounds
{
dlFunctionHook(strPluginPath[num_argv1], (char *)"printStr");//use funcID select function
}
else //the input not allow
{
std::cout << "need follow operation:" << std::endl;
std::cout << "\thelp" << std::endl;
std::cout << "\t[FuncID]" << std::endl;
return 0;
}
return 0;
}
/**
* @brief call plugin
*
* @param path path of the dynamic link file
* @param op function name
* @return true success
* @return false fail
*/
bool dlFunctionHook(string path, char *op)
{
//延迟函数的调用绑定插件目录下的文件
void *handle = dlopen(path.c_str(), RTLD_LAZY);
if (handle == NULL) //检测动态链接是否成功
{
std::cout << "dlopen error" << std::endl;
return false;
}
typedef void (*Fun)(); // 函数原型
// 映射动态链接库中printStr的函数原型
Fun funPrintStr = (Fun)dlsym(handle, op);
if (funPrintStr == NULL)
{
std::cout << "Fun " << op << " error" << std::endl;
std::cout << dlerror() << std::endl; //查看出错原因
return false;
}
(*funPrintStr)(); // 用函数指针调用函数
dlclose(handle); // 关闭动态链接库
return true;
}