#define MAX_PATH 1024 #include #include #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 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 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"); }