From feb11115a7a1c5ac01e571901bbad5b33e867e99 Mon Sep 17 00:00:00 2001 From: gerald Date: Fri, 27 Aug 2010 19:24:44 +0000 Subject: [PATCH] Forward-port r33953. Move the SetDllDirectory calls to ws_init_dll_search_path. If SetDllDirectory fails, pass the Wireshark program path to SetCurrentDirectory. git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@33958 f5534014-38df-0310-8fa8-9805f1628bb7 --- dumpcap.c | 11 +++++------ gtk/main.c | 16 +++++----------- wsutil/file_util.c | 22 ++++++++++++++++++++++ wsutil/file_util.h | 7 +++++++ wsutil/libwsutil.def | 1 + 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/dumpcap.c b/dumpcap.c index 46daa0406f..30454d308a 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -3273,8 +3273,6 @@ main(int argc, char *argv[]) #ifdef _WIN32 WSADATA wsaData; - typedef BOOL (*SetDllDirectoryHandler)(LPCTSTR); - SetDllDirectoryHandler PSetDllDirectory; #else struct sigaction action, oldaction; #endif @@ -3297,10 +3295,11 @@ main(int argc, char *argv[]) #endif #ifdef _WIN32 - if (PSetDllDirectory = (SetDllDirectoryHandler) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "SetDllDirectoryW")) { - PSetDllDirectory(_T("")); - /* XXX - Exit on failure? */ - } + /* + * Initialize our DLL search path. MUST be called before LoadLibrary + * or g_module_open. + */ + ws_init_dll_search_path(); #endif #ifdef HAVE_PCAP_REMOTE diff --git a/gtk/main.c b/gtk/main.c index bb3bbdce6c..d4d70df0de 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -222,7 +222,6 @@ guint tap_update_timer_id; static gboolean has_console; /* TRUE if app has console */ static void destroy_console(void); static gboolean stdin_capture = FALSE; /* Don't grab stdin & stdout if TRUE */ -static gboolean dll_set = FALSE; /* Did we sucessfully trim our DLL path? */ #endif static void console_log_handler(const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer user_data); @@ -2109,11 +2108,6 @@ main(int argc, char *argv[]) argv[i] = g_strdup(utf_16to8(wc_argv[i])); } } /* XXX else bail because something is horribly, horribly wrong? */ - - if (!dll_set) { - simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "SetDllDirectory failed (%d)!\n", GetLastError()); - /* XXX - Exit? */ - } #endif /* _WIN32 */ /* @@ -3098,12 +3092,12 @@ WinMain (struct HINSTANCE__ *hInstance, int nCmdShow) { INITCOMMONCONTROLSEX comm_ctrl; - typedef BOOL (*SetDllDirectoryHandler)(LPCTSTR); - SetDllDirectoryHandler PSetDllDirectory; - if (PSetDllDirectory = (SetDllDirectoryHandler) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "SetDllDirectoryW")) { - dll_set = PSetDllDirectory(_T("")); - } + /* + * Initialize our DLL search path. MUST be called before LoadLibrary + * or g_module_open. + */ + ws_init_dll_search_path(); /* Initialize our controls. Required for native Windows file dialogs. */ memset (&comm_ctrl, 0, sizeof(comm_ctrl)); diff --git a/wsutil/file_util.c b/wsutil/file_util.c index 160272c0bf..2695d98170 100644 --- a/wsutil/file_util.c +++ b/wsutil/file_util.c @@ -43,6 +43,7 @@ #include #include #include +#include /*#include */ #include @@ -474,6 +475,27 @@ init_dll_load_paths() { return FALSE; } +gboolean +ws_init_dll_search_path() { + gboolean dll_dir_set = FALSE; + wchar_t *program_path_w; + + typedef BOOL (*SetDllDirectoryHandler)(LPCTSTR); + SetDllDirectoryHandler PSetDllDirectory; + + if (PSetDllDirectory = (SetDllDirectoryHandler) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "SetDllDirectoryW")) { + dll_dir_set = PSetDllDirectory(_T("")); + } + + if (!dll_dir_set && init_dll_load_paths()) { + program_path_w = g_utf8_to_utf16(program_path, -1, NULL, NULL, NULL); + SetCurrentDirectory(program_path_w); + g_free(program_path_w); + } + + return dll_dir_set; +} + /* * Internally g_module_open uses LoadLibrary on Windows and returns an * HMODULE cast to a GModule *. However there's no guarantee that this diff --git a/wsutil/file_util.h b/wsutil/file_util.h index 3427d417ad..798cea9261 100644 --- a/wsutil/file_util.h +++ b/wsutil/file_util.h @@ -100,6 +100,13 @@ extern FILE * ws_stdio_freopen (const gchar *filename, const gchar *mode, FILE * /* DLL loading */ +/** Try to remove the current directory from the DLL search path. + * SetDllDirectory is tried, then SetCurrentDirectory(program_dir) + * + * @return TRUE if we were able to call SetDllDirectory, FALSE otherwise. + */ +gboolean ws_init_dll_search_path(); + /** Load a DLL using LoadLibrary. * Only the system and program directories are searched. * diff --git a/wsutil/libwsutil.def b/wsutil/libwsutil.def index a0cafe95fc..e0e0202187 100644 --- a/wsutil/libwsutil.def +++ b/wsutil/libwsutil.def @@ -18,6 +18,7 @@ ws_stdio_remove ws_stdio_rename ws_stdio_stat ws_stdio_unlink +ws_init_dll_search_path ws_load_library ws_module_open -- 2.34.1