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; }
gboolean ws_init_dll_search_path() { gboolean dll_dir_set = FALSE, npf_found = FALSE; wchar_t *program_path_w; wchar_t npcap_path_w[MAX_PATH]; unsigned int retval; SC_HANDLE h_scm, h_serv; dll_dir_set = SetDllDirectory(_T("")); if (dll_dir_set) { /* Do not systematically add Npcap path as long as we favor WinPcap over Npcap. */ h_scm = OpenSCManager(NULL, NULL, 0); if (h_scm) { h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS); if (h_serv) { CloseServiceHandle(h_serv); npf_found = TRUE; } CloseServiceHandle(h_scm); } if (!npf_found) { /* npf service was not found, so WinPcap is not (properly) installed. Add Npcap folder to libraries search path. */ retval = GetSystemDirectoryW(npcap_path_w, MAX_PATH); if (0 < retval && retval <= MAX_PATH) { wcscat_s(npcap_path_w, MAX_PATH, L"\\Npcap"); dll_dir_set = SetDllDirectory(npcap_path_w); } } } 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; }
gboolean ws_init_dll_search_path() { gboolean dll_dir_set = FALSE; wchar_t *program_path_w; typedef BOOL (WINAPI *SetDllDirectoryHandler)(LPCTSTR); SetDllDirectoryHandler PSetDllDirectory; PSetDllDirectory = (SetDllDirectoryHandler) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "SetDllDirectoryW"); if (PSetDllDirectory) { 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; }
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; }