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
|
#define MAX_PATH 1024
#include <stdio.h>
#include <string.h>
#if defined(_WIN32)
#define SHARED_EXT "dll"
#elif defined(__APPLE__)
#define SHARED_EXT "dylib"
#else
#define SHARED_EXT "so"
#endif
typedef void(*dll_main_t)(int argc, char ** argv);
#if defined(_WIN32)
#include <windows.h>
static void showError() {
DWORD dwErrCode = GetLastError();
LPVOID lpMsgBuf;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL )) {
fprintf(stderr, "Error: %s\n", lpMsgBuf);
LocalFree(lpMsgBuf);
}
}
static HMODULE LoadLibraryExWrap(LPCTSTR lpFileName, HANDLE hFile, DWORD dwFlags) {
HMODULE r = LoadLibraryEx(lpFileName, hFile, dwFlags);
if (!r)
showError();
return r;
}
static dll_main_t LoadDllMain(const char * searchpath) {
HMODULE handle;
const char * fname = "lua-interface." SHARED_EXT;
if (!(handle = LoadLibraryExWrap(fname, NULL, LOAD_WITH_ALTERED_SEARCH_PATH)) &&
!(handle = LoadLibraryExWrap(fname, NULL, NULL))) {
LuaLoadPlugin(&Input(fname), L);
return NULL;
}
dll_main_t dll_main = (dll_main) GetProcAddress(handle, "dll_main");
if (!dll_main)
FreeLibrary(handle);
return dll_main;
}
#else
#include <dlfcn.h>
static dll_main_t LoadDllMain(const char * searchpath) {
const char * fname = "./lua-interface." SHARED_EXT;
void * handle = dlopen(fname, RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
char full_fname[MAX_PATH];
strcpy(full_fname, searchpath);
strcat(full_fname, "lua-interface." SHARED_EXT);
handle = dlopen(full_fname, RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
return NULL;;
}
}
dll_main_t dll_main = (dll_main_t) dlsym(handle, "dll_main");
if (!dll_main)
dlclose(handle);
return dll_main;
}
#endif
int main(int argc, char ** argv) {
char searchpath[MAX_PATH];
char * slash;
strcpy(searchpath, argv[0]);
slash = strrchr(searchpath, '/');
if (slash) {
*(++slash) = 0;
} else {
searchpath[0] = 0;
}
dll_main_t dll_main = LoadDllMain(searchpath);
if (dll_main)
dll_main(argc, argv);
else
fprintf(stderr, "Couldn't locate lua-interface's library.\n");
}
|