Browse Source

libobs/util: Load win. module deps from same dir

Certain plugins/modules that are loaded on windows may be dependent on
libraries that are located in the same directory as the module in
question.  This makes is so that LoadLibrary will also search for
dependent libraries for that module in the module's directory.
jp9000 10 years ago
parent
commit
d7d44a92ff
1 changed files with 16 additions and 0 deletions
  1. 16 0
      libobs/util/platform-windows.c

+ 16 - 0
libobs/util/platform-windows.c

@@ -52,20 +52,36 @@ void *os_dlopen(const char *path)
 {
 {
 	struct dstr dll_name;
 	struct dstr dll_name;
 	wchar_t *wpath;
 	wchar_t *wpath;
+	wchar_t *wpath_slash;
 	HMODULE h_library = NULL;
 	HMODULE h_library = NULL;
 
 
 	if (!path)
 	if (!path)
 		return NULL;
 		return NULL;
 
 
 	dstr_init_copy(&dll_name, path);
 	dstr_init_copy(&dll_name, path);
+	dstr_replace(&dll_name, "\\", "/");
 	if (!dstr_find(&dll_name, ".dll"))
 	if (!dstr_find(&dll_name, ".dll"))
 		dstr_cat(&dll_name, ".dll");
 		dstr_cat(&dll_name, ".dll");
 
 
 	os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
 	os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
+
+	/* to make module dependency issues easier to deal with, allow
+	 * dynamically loaded libraries on windows to search for dependent
+	 * libraries that are within the library's own directory */
+	wpath_slash = wcsrchr(wpath, L'/');
+	if (wpath_slash) {
+		*wpath_slash = 0;
+		SetDllDirectoryW(wpath);
+		*wpath_slash = L'/';
+	}
+
 	h_library = LoadLibraryW(wpath);
 	h_library = LoadLibraryW(wpath);
 	bfree(wpath);
 	bfree(wpath);
 	dstr_free(&dll_name);
 	dstr_free(&dll_name);
 
 
+	if (wpath_slash)
+		SetDllDirectoryW(NULL);
+
 	if (!h_library)
 	if (!h_library)
 		blog(LOG_INFO, "LoadLibrary failed for '%s', error: %ld",
 		blog(LOG_INFO, "LoadLibrary failed for '%s', error: %ld",
 				path, GetLastError());
 				path, GetLastError());