Blender V2.61 - r43446

dynlib.c

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