From ed172fd1e4c6892040d49e54c7e942641ae9b9d3 Mon Sep 17 00:00:00 2001 From: Themaister Date: Wed, 19 Jan 2011 13:25:18 +0100 Subject: [PATCH] Add dynamic loading for Win32. --- Makefile.win32 | 4 ++++ dynamic.c | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Makefile.win32 b/Makefile.win32 index 8a1d911de9..7ca4f865c9 100644 --- a/Makefile.win32 +++ b/Makefile.win32 @@ -35,6 +35,10 @@ ifeq ($(HAVE_XML), 1) LIBS += -lxml2 endif +ifeq ($(DYNAMIC), 1) + DEFINES += -DHAVE_DYNAMIC +endif + ifneq ($(V),1) Q := @ endif diff --git a/dynamic.c b/dynamic.c index 0dfc464f54..fd95a76b3e 100644 --- a/dynamic.c +++ b/dynamic.c @@ -26,18 +26,29 @@ #include #ifdef HAVE_DYNAMIC -#include +#ifdef _WIN32 +#include +#define SYM(x) do { \ + p##x = ((void*)GetProcAddress(lib_handle, #x)); \ + if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \ +} while(0) +#else +#include #define SYM(x) do { \ p##x = dlsym(lib_handle, #x); \ if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \ } while(0) - -#endif +#endif // _WIN32 +#endif // HAVE_DYNAMIC #ifdef HAVE_DYNAMIC +#ifdef _WIN32 +static HMODULE lib_handle; +#else static void *lib_handle = NULL; #endif +#endif void (*psnes_init)(void); @@ -86,7 +97,11 @@ void (*psnes_term)(void); static void load_dynamic(void) { SSNES_LOG("Loading dynamic libsnes from: \"%s\"\n", g_settings.libsnes); +#ifdef _WIN32 + lib_handle = LoadLibrary(g_settings.libsnes); +#else lib_handle = dlopen(g_settings.libsnes, RTLD_LAZY); +#endif if (!lib_handle) { SSNES_ERR("Failed to open dynamic library: \"%s\"\n", g_settings.libsnes); @@ -171,6 +186,12 @@ void uninit_dlsym(void) { #ifdef HAVE_DYNAMIC if (lib_handle) + { +#ifdef _WIN32 + FreeLibrary(lib_handle); +#else dlclose(lib_handle); +#endif + } #endif }