2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2005-03-09 19:45:59 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bli
|
2011-02-27 20:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-01-08 11:32:49 +11:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
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
|
2015-02-08 12:41:45 -05:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
2023-10-19 17:07:56 +02:00
|
|
|
# include "utf_winfunc.hh"
|
|
|
|
|
# include "utfconv.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
# include <windows.h>
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2017-02-06 10:44:25 +01:00
|
|
|
DynamicLibrary *BLI_dynlib_open(const char *name)
|
2011-05-06 15:17:42 +00:00
|
|
|
{
|
|
|
|
|
DynamicLibrary *lib;
|
2012-03-20 02:17:37 +00:00
|
|
|
void *handle;
|
|
|
|
|
|
|
|
|
|
UTF16_ENCODE(name);
|
2012-05-12 15:13:06 +00:00
|
|
|
handle = LoadLibraryW(name_16);
|
2012-03-20 02:17:37 +00:00
|
|
|
UTF16_UN_ENCODE(name);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!handle) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2011-05-06 15:17:42 +00:00
|
|
|
|
2023-10-19 17:07:56 +02:00
|
|
|
lib = MEM_cnew<DynamicLibrary>("Dynamic Library");
|
2012-05-12 15:13:06 +00:00
|
|
|
lib->handle = handle;
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2023-10-19 17:07:56 +02:00
|
|
|
return GetProcAddress(HMODULE(lib->handle), symname);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
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;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-02-14 03:24:45 +00:00
|
|
|
/* if lib is NULL reset the last error code */
|
2012-05-12 15:13:06 +00:00
|
|
|
err = GetLastError();
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!lib) {
|
2011-05-06 15:17:42 +00:00
|
|
|
SetLastError(ERROR_SUCCESS);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (err) {
|
2002-10-12 11:37:38 +00:00
|
|
|
static char buf[1024];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-19 17:07:56 +02:00
|
|
|
FreeLibrary(HMODULE(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>
|
|
|
|
|
|
2017-02-06 10:44:25 +01:00
|
|
|
DynamicLibrary *BLI_dynlib_open(const char *name)
|
2011-05-06 15:17:42 +00:00
|
|
|
{
|
|
|
|
|
DynamicLibrary *lib;
|
2012-05-12 15:13:06 +00:00
|
|
|
void *handle = dlopen(name, RTLD_LAZY);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!handle) {
|
2023-10-21 21:07:18 +11:00
|
|
|
return nullptr;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2011-05-06 15:17:42 +00:00
|
|
|
|
2023-10-19 17:07:56 +02:00
|
|
|
lib = MEM_cnew<DynamicLibrary>("Dynamic Library");
|
2012-05-12 15:13:06 +00:00
|
|
|
lib->handle = handle;
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2011-05-06 15:17:42 +00:00
|
|
|
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();
|
|
|
|
|
}
|
2018-06-17 16:32:54 +02:00
|
|
|
|
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
|