|
Blender
V2.59
|
00001 /* 00002 * $Id: dynlib.c 36519 2011-05-06 15:17:42Z blendix $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file, with exception of below: 00024 * 00025 * Contributor(s): Peter O'Gorman 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 */ 00029 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include <string.h> 00037 00038 #include "MEM_guardedalloc.h" 00039 00040 #include "BLI_dynlib.h" 00041 00042 struct DynamicLibrary { 00043 void *handle; 00044 }; 00045 00046 #ifdef WIN32 00047 00048 #include <windows.h> 00049 00050 DynamicLibrary *BLI_dynlib_open(char *name) 00051 { 00052 DynamicLibrary *lib; 00053 void *handle= LoadLibrary(name); 00054 00055 if(!handle) 00056 return NULL; 00057 00058 lib= MEM_callocN(sizeof(*lib), "Dynamic Library"); 00059 lib->handle= handle; 00060 00061 return lib; 00062 } 00063 00064 void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname) 00065 { 00066 return GetProcAddress(lib->handle, symname); 00067 } 00068 00069 char *BLI_dynlib_get_error_as_string(DynamicLibrary *lib) 00070 { 00071 int err; 00072 00073 /* if lib is NULL reset the last error code */ 00074 err= GetLastError(); 00075 if(!lib) 00076 SetLastError(ERROR_SUCCESS); 00077 00078 if(err) { 00079 static char buf[1024]; 00080 00081 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 00082 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 00083 buf, sizeof(buf), NULL)) 00084 return buf; 00085 } 00086 00087 return NULL; 00088 } 00089 00090 void BLI_dynlib_close(DynamicLibrary *lib) 00091 { 00092 FreeLibrary(lib->handle); 00093 MEM_freeN(lib); 00094 } 00095 00096 #else /* Unix */ 00097 00098 #include <dlfcn.h> 00099 00100 DynamicLibrary *BLI_dynlib_open(char *name) 00101 { 00102 DynamicLibrary *lib; 00103 void *handle= dlopen(name, RTLD_LAZY); 00104 00105 if(!handle) 00106 return NULL; 00107 00108 lib= MEM_callocN(sizeof(*lib), "Dynamic Library"); 00109 lib->handle= handle; 00110 00111 return lib; 00112 } 00113 00114 void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname) 00115 { 00116 return dlsym(lib->handle, symname); 00117 } 00118 00119 char *BLI_dynlib_get_error_as_string(DynamicLibrary *lib) 00120 { 00121 (void)lib; /* unused */ 00122 return dlerror(); 00123 } 00124 00125 void BLI_dynlib_close(DynamicLibrary *lib) 00126 { 00127 dlclose(lib->handle); 00128 MEM_freeN(lib); 00129 } 00130 00131 #endif 00132