| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2005-03-09 19:45:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup bli | 
					
						
							| 
									
										
										
										
											2011-02-27 20:37:56 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2005-03-09 19:45:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | #include "BLI_dynlib.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct DynamicLibrary { | 
					
						
							|  |  |  | 	void *handle; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2003-02-21 15:37:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | #ifdef WIN32
 | 
					
						
							| 
									
										
										
										
											2015-12-21 13:00:06 +11:00
										 |  |  | #ifndef _WIN32_WINNT
 | 
					
						
							| 
									
										
										
										
											2015-02-08 12:41:45 -05:00
										 |  |  | #define _WIN32_WINNT 0x501 /* Windows XP or newer */
 | 
					
						
							| 
									
										
										
										
											2015-12-21 13:00:06 +11:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-02-08 12:41:45 -05:00
										 |  |  | #define WIN32_LEAN_AND_MEAN
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2012-03-20 02:17:37 +00:00
										 |  |  | #include "utf_winfunc.h"
 | 
					
						
							|  |  |  | #include "utfconv.h"
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 10:44:25 +01:00
										 |  |  | DynamicLibrary *BLI_dynlib_open(const char *name) | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	DynamicLibrary *lib; | 
					
						
							| 
									
										
										
										
											2012-03-20 02:17:37 +00:00
										 |  |  | 	void *handle; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	UTF16_ENCODE(name); | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	handle = LoadLibraryW(name_16); | 
					
						
							| 
									
										
										
										
											2012-03-20 02:17:37 +00:00
										 |  |  | 	UTF16_UN_ENCODE(name); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	if (!handle) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	lib = MEM_callocN(sizeof(*lib), "Dynamic Library"); | 
					
						
							|  |  |  | 	lib->handle = handle; | 
					
						
							| 
									
										
										
										
											2018-06-17 16:32:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 	return lib; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	return GetProcAddress(lib->handle, symname); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | char *BLI_dynlib_get_error_as_string(DynamicLibrary *lib) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2003-02-14 03:24:45 +00:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-14 03:24:45 +00:00
										 |  |  | 	/* if lib is NULL reset the last error code */ | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	err = GetLastError(); | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	if (!lib) | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 		SetLastError(ERROR_SUCCESS); | 
					
						
							| 
									
										
										
										
											2003-02-14 03:24:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	if (err) { | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 		static char buf[1024]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 		if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 
					
						
							|  |  |  | 		                  NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | 
					
						
							|  |  |  | 		                  buf, sizeof(buf), NULL)) | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 			return buf; | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-17 16:32:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-15 16:13:32 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | void BLI_dynlib_close(DynamicLibrary *lib) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	FreeLibrary(lib->handle); | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 	MEM_freeN(lib); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | #else /* Unix */
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <dlfcn.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 10:44:25 +01:00
										 |  |  | DynamicLibrary *BLI_dynlib_open(const char *name) | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	DynamicLibrary *lib; | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	void *handle = dlopen(name, RTLD_LAZY); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	if (!handle) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	lib = MEM_callocN(sizeof(*lib), "Dynamic Library"); | 
					
						
							|  |  |  | 	lib->handle = handle; | 
					
						
							| 
									
										
										
										
											2018-06-17 16:32:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 	return lib; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	return dlsym(lib->handle, symname); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | char *BLI_dynlib_get_error_as_string(DynamicLibrary *lib) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-16 14:32:17 +00:00
										 |  |  | 	(void)lib; /* unused */ | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	return dlerror(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-17 16:32:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | void BLI_dynlib_close(DynamicLibrary *lib) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	dlclose(lib->handle); | 
					
						
							| 
									
										
										
										
											2011-05-06 15:17:42 +00:00
										 |  |  | 	MEM_freeN(lib); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |