2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
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.
|
|
|
|
*
|
2003-12-07 19:20:10 +00:00
|
|
|
* The Original Code is: all of this file, with exception of below:
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
2003-12-07 19:20:10 +00:00
|
|
|
* Contributor(s): Peter O'Gorman
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2005-03-09 19:45:59 +00:00
|
|
|
|
2011-02-27 20:37:56 +00:00
|
|
|
/** \file blender/blenlib/intern/dynlib.c
|
|
|
|
* \ingroup bli
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
DynamicLibrary *BLI_dynlib_open(char *name)
|
|
|
|
{
|
|
|
|
DynamicLibrary *lib;
|
2002-10-12 11:37:38 +00:00
|
|
|
void *handle= LoadLibrary(name);
|
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
if(!handle)
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
2011-05-06 15:17:42 +00:00
|
|
|
|
|
|
|
lib= MEM_callocN(sizeof(*lib), "Dynamic Library");
|
|
|
|
lib->handle= handle;
|
|
|
|
|
|
|
|
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 */
|
2008-09-20 14:43:59 +00:00
|
|
|
err= GetLastError();
|
2011-05-06 15:17:42 +00:00
|
|
|
if(!lib)
|
|
|
|
SetLastError(ERROR_SUCCESS);
|
2003-02-14 03:24:45 +00:00
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
if(err) {
|
2002-10-12 11:37:38 +00:00
|
|
|
static char buf[1024];
|
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
buf, sizeof(buf), NULL))
|
2002-10-12 11:37:38 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
DynamicLibrary *BLI_dynlib_open(char *name)
|
|
|
|
{
|
|
|
|
DynamicLibrary *lib;
|
2002-10-12 11:37:38 +00:00
|
|
|
void *handle= dlopen(name, RTLD_LAZY);
|
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
if(!handle)
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
2011-05-06 15:17:42 +00:00
|
|
|
|
|
|
|
lib= MEM_callocN(sizeof(*lib), "Dynamic Library");
|
|
|
|
lib->handle= handle;
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
2009-09-28 16:05:34 +00:00
|
|
|
|