diff options
author | Gerald Combs <gerald@wireshark.org> | 2010-08-25 20:30:59 +0000 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 2010-08-25 20:30:59 +0000 |
commit | 607b228df6f1f754bf9bda6cfa21563913b1e7ff (patch) | |
tree | 6d1cf5c9dae154869fc6a13d181489834d7a190e /wsutil/file_util.c | |
parent | 43af5f6344d47af3ccfa574c1013dbc6a011904c (diff) | |
download | wireshark-607b228df6f1f754bf9bda6cfa21563913b1e7ff.tar.gz |
Add ws_load_library and ws_module_open, which respectively call
LoadLibrary and g_module_open only for the program directory and system
directory on Windows. Use them to replace a bunch of LoadLibrary and
g_module_open calls. Use the extension ".dll" for all the DLLs that we
load. Add comments about DLL loading in Python.
svn path=/trunk/; revision=33924
Diffstat (limited to 'wsutil/file_util.c')
-rw-r--r-- | wsutil/file_util.c | 114 |
1 files changed, 112 insertions, 2 deletions
diff --git a/wsutil/file_util.c b/wsutil/file_util.c index d145752c83..0c57efbe19 100644 --- a/wsutil/file_util.c +++ b/wsutil/file_util.c @@ -48,8 +48,8 @@ #include "file_util.h" - - +static gchar *program_path = NULL; +static gchar *system_path = NULL; /** * g_open: @@ -441,3 +441,113 @@ ws_stdio_freopen (const gchar *filename, errno = save_errno; return retval; } + + +/* DLL loading */ +static gboolean +init_dll_load_paths() { + TCHAR path_pfx[MAX_PATH]; + + if (program_path && system_path) + return TRUE; + + /* XXX - Duplicate code in filesystem.c:init_progfile_dir */ + if (GetModuleFileName(NULL, path_pfx, MAX_PATH) == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + return FALSE; + } + + if (!program_path) { + program_path = g_utf16_to_utf8(path_pfx, -1, NULL, NULL, NULL); + } + + if (GetSystemDirectory(path_pfx, MAX_PATH) == 0) { + return FALSE; + } + + if (!system_path) { + system_path = g_utf16_to_utf8(path_pfx, -1, NULL, NULL, NULL); + } + + if (program_path && system_path) + return TRUE; + + return FALSE; +} + +/* + * Internally g_module_open uses LoadLibrary on Windows and returns an + * HMODULE cast to a GModule *. However there's no guarantee that this + * will always be the case, so we call LoadLibrary and g_module_open + * separately. + */ + +void * +ws_load_library(gchar *library_name) { + gchar *full_path; + wchar_t *full_path_w; + HMODULE dll_h; + + if (!init_dll_load_paths() || !library_name) + return NULL; + + /* First try the program directory */ + full_path = g_module_build_path(program_path, library_name); + full_path_w = g_utf8_to_utf16(full_path, -1, NULL, NULL, NULL); + + if (full_path && full_path_w) { + dll_h = LoadLibraryW(full_path_w); + if (dll_h) { + g_free(full_path); + g_free(full_path_w); + return dll_h; + } + } + + /* Next try the system directory */ + full_path = g_module_build_path(system_path, library_name); + full_path_w = g_utf8_to_utf16(full_path, -1, NULL, NULL, NULL); + + if (full_path && full_path_w) { + dll_h = LoadLibraryW(full_path_w); + if (dll_h) { + g_free(full_path); + g_free(full_path_w); + return dll_h; + } + } + + return NULL; +} + +GModule * +ws_module_open(gchar *module_name, GModuleFlags flags) { + gchar *full_path; + GModule *mod; + + if (!init_dll_load_paths() || !module_name) + return NULL; + + /* First try the program directory */ + full_path = g_module_build_path(program_path, module_name); + + if (full_path) { + mod = g_module_open(full_path, flags); + if (mod) { + g_free(full_path); + return mod; + } + } + + /* Next try the system directory */ + full_path = g_module_build_path(system_path, module_name); + + if (full_path) { + mod = g_module_open(full_path, flags); + if (mod) { + g_free(full_path); + return mod; + } + } + + return NULL; +}
\ No newline at end of file |