From 043acfe894526e46c8f7bf1f204dbb4f6f1f025f Mon Sep 17 00:00:00 2001 From: Xiaoqiang Wang Date: Tue, 7 Jan 2020 16:43:11 +0100 Subject: [PATCH] search dependencies in the library dir This instructs LoadLibrary to search dependent libraries in the dir where the module resides. --- GNUmakefile | 2 +- require.c | 35 ++++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index ab47113..e530ba9 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -29,7 +29,7 @@ USR_INCLUDES_Linux=-idirafter ${EPICS_BASE}/include USR_CFLAGS += -DT_A='"${T_A}"' # This should really go into some global WIN32 config file -USR_CFLAGS_WIN32 += /D_WIN32_WINNT=0x501 +USR_CFLAGS_WIN32 += /D_WIN32_WINNT=0x502 dbLoadTemplate.c: dbLoadTemplate_lex.c ../dbLoadTemplate.h diff --git a/require.c b/require.c index 657e8b1..2ea152d 100644 --- a/require.c +++ b/require.c @@ -300,21 +300,30 @@ static HMODULE loadlib(const char* libname) libname, dlerror()); } #elif defined (_WIN32) - if ((libhandle = LoadLibrary(libname)) == NULL) { - LPVOID lpMsgBuf; + char *p; + char *libpath = strdup(libname); + if ((p = strrchr(libpath, '/')) != NULL) + *p = '\0'; + SetDllDirectory(libpath); + if ((libhandle = LoadLibrary(libname)) == NULL) + { + LPVOID lpMsgBuf; - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &lpMsgBuf, - 0, NULL ); - fprintf (stderr, "Loading %s library failed: %s\n", - libname, lpMsgBuf); - LocalFree(lpMsgBuf); + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, + 0, NULL ); + fprintf (stderr, "Loading %s library failed: %s\n", + libname, lpMsgBuf); + LocalFree(lpMsgBuf); + } + free(libpath); + SetDllDirectory(NULL); } #elif defined (vxWorks) {