This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/intern/dynlib.c

131 lines
2.7 KiB
C
Raw Normal View History

/*
* ***** 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
* 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.
*
* The Original Code is: all of this file, with exception of below:
2002-10-12 11:37:38 +00:00
*
* Contributor(s): Peter O'Gorman
2002-10-12 11:37:38 +00:00
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
2011-02-27 20:37:56 +00:00
/** \file blender/blenlib/intern/dynlib.c
* \ingroup bli
*/
#include <stdio.h>
2002-10-12 11:37:38 +00:00
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
2002-10-12 11:37:38 +00:00
#include "BLI_dynlib.h"
struct DynamicLibrary {
void *handle;
};
2002-10-12 11:37:38 +00:00
#ifdef WIN32
#include <windows.h>
DynamicLibrary *BLI_dynlib_open(char *name)
{
DynamicLibrary *lib;
2002-10-12 11:37:38 +00:00
void *handle= LoadLibrary(name);
if(!handle)
2002-10-12 11:37:38 +00:00
return NULL;
lib= MEM_callocN(sizeof(*lib), "Dynamic Library");
lib->handle= handle;
return lib;
2002-10-12 11:37:38 +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);
}
char *BLI_dynlib_get_error_as_string(DynamicLibrary *lib)
{
int err;
2002-10-12 11:37:38 +00:00
/* if lib is NULL reset the last error code */
err= GetLastError();
if(!lib)
SetLastError(ERROR_SUCCESS);
if(err) {
2002-10-12 11:37:38 +00:00
static char buf[1024];
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;
}
return NULL;
2002-10-12 11:37:38 +00:00
}
void BLI_dynlib_close(DynamicLibrary *lib)
{
2002-10-12 11:37:38 +00:00
FreeLibrary(lib->handle);
MEM_freeN(lib);
2002-10-12 11:37:38 +00:00
}
#else /* Unix */
2002-10-12 11:37:38 +00:00
#include <dlfcn.h>
DynamicLibrary *BLI_dynlib_open(char *name)
{
DynamicLibrary *lib;
2002-10-12 11:37:38 +00:00
void *handle= dlopen(name, RTLD_LAZY);
if(!handle)
2002-10-12 11:37:38 +00:00
return NULL;
lib= MEM_callocN(sizeof(*lib), "Dynamic Library");
lib->handle= handle;
return lib;
2002-10-12 11:37:38 +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);
}
char *BLI_dynlib_get_error_as_string(DynamicLibrary *lib)
{
(void)lib; /* unused */
2002-10-12 11:37:38 +00:00
return dlerror();
}
void BLI_dynlib_close(DynamicLibrary *lib)
{
2002-10-12 11:37:38 +00:00
dlclose(lib->handle);
MEM_freeN(lib);
2002-10-12 11:37:38 +00:00
}
#endif