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

141 lines
2.9 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
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x501 /* Windows XP or newer */
#endif
#define WIN32_LEAN_AND_MEAN
2002-10-12 11:37:38 +00:00
#include <windows.h>
#include "utf_winfunc.h"
#include "utfconv.h"
2002-10-12 11:37:38 +00:00
DynamicLibrary *BLI_dynlib_open(const char *name)
{
DynamicLibrary *lib;
void *handle;
UTF16_ENCODE(name);
2012-05-12 15:13:06 +00:00
handle = LoadLibraryW(name_16);
UTF16_UN_ENCODE(name);
2002-10-12 11:37:38 +00:00
if (!handle)
2002-10-12 11:37:38 +00:00
return NULL;
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
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 */
2012-05-12 15:13:06 +00:00
err = GetLastError();
if (!lib)
SetLastError(ERROR_SUCCESS);
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))
{
2002-10-12 11:37:38 +00:00
return buf;
}
2002-10-12 11:37:38 +00:00
}
2018-06-17 16:32:54 +02:00
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(const char *name)
{
DynamicLibrary *lib;
2012-05-12 15:13:06 +00:00
void *handle = dlopen(name, RTLD_LAZY);
2002-10-12 11:37:38 +00:00
if (!handle)
2002-10-12 11:37:38 +00:00
return NULL;
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
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();
}
2018-06-17 16:32:54 +02:00
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