-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
79 lines (75 loc) · 1.76 KB
/
main.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
#include "xhost.h"
#include "xpackage.h"
#include "singleton.h"
#include "smbus.h"
#if (WIN32)
#include <windows.h>
#define X_EXPORT __declspec(dllexport)
#else
#include <dlfcn.h>
#define X_EXPORT
#endif
static bool GetCurLibInfo(void* EntryFuncName, std::string& strFullPath,
std::string& strFolderPath, std::string& strLibName)
{
#if (WIN32)
HMODULE hModule = NULL;
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR)EntryFuncName,
&hModule);
char path[MAX_PATH];
GetModuleFileName(hModule, path, MAX_PATH);
std::string strPath(path);
strFullPath = strPath;
auto pos = strPath.rfind("\\");
if (pos != std::string::npos)
{
strFolderPath = strPath.substr(0, pos);
strLibName = strPath.substr(pos + 1);
}
#else
Dl_info dl_info;
dladdr((void*)EntryFuncName, &dl_info);
std::string strPath = dl_info.dli_fname;
strFullPath = strPath;
auto pos = strPath.rfind("/");
if (pos != std::string::npos)
{
strFolderPath = strPath.substr(0, pos);
strLibName = strPath.substr(pos + 1);
}
#endif
//remove ext
pos = strLibName.rfind(".");
if (pos != std::string::npos)
{
strLibName = strLibName.substr(0, pos);
}
return true;
}
namespace X
{
XHost* g_pXHost = nullptr;
class ElectronLink :
public Singleton<ElectronLink>
{
public:
BEGIN_PACKAGE(ElectronLink)
APISET().AddClass<1, X::ElectonLink::SMBus>("smbus");
END_PACKAGE
};
}
extern "C" X_EXPORT void Load(void* pHost,X::Value curModule)
{
std::string strFullPath;
std::string strFolderPath;
std::string strLibName;
GetCurLibInfo((void*)Load, strFullPath, strFolderPath, strLibName);
X::g_pXHost = (X::XHost*)pHost;
X::RegisterPackage<X::ElectronLink>(strFullPath.c_str(), "electronlink", &X::ElectronLink::I());
}
extern "C" X_EXPORT void Unload()
{
X::g_pXHost = nullptr;
}