Blender V4.3
dynlib.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_dynlib.h"
16
18 void *handle;
19};
20
21#ifdef WIN32
22# define WIN32_LEAN_AND_MEAN
23# include "utf_winfunc.hh"
24# include "utfconv.hh"
25# include <windows.h>
26
27DynamicLibrary *BLI_dynlib_open(const char *name)
28{
30 void *handle;
31
32 UTF16_ENCODE(name);
33 handle = LoadLibraryW(name_16);
34 UTF16_UN_ENCODE(name);
35
36 if (!handle) {
37 return NULL;
38 }
39
40 lib = MEM_cnew<DynamicLibrary>("Dynamic Library");
41 lib->handle = handle;
42
43 return lib;
44}
45
46void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname)
47{
48 return GetProcAddress(HMODULE(lib->handle), symname);
49}
50
52{
53 int err;
54
55 /* if lib is NULL reset the last error code */
56 err = GetLastError();
57 if (!lib) {
58 SetLastError(ERROR_SUCCESS);
59 }
60
61 if (err) {
62 static char buf[1024];
63
64 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
65 NULL,
66 err,
67 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
68 buf,
69 sizeof(buf),
70 NULL))
71 {
72 return buf;
73 }
74 }
75
76 return NULL;
77}
78
80{
81 FreeLibrary(HMODULE(lib->handle));
83}
84
85#else /* Unix */
86
87# include <dlfcn.h>
88
90{
92 void *handle = dlopen(name, RTLD_LAZY);
93
94 if (!handle) {
95 return nullptr;
96 }
97
98 lib = MEM_cnew<DynamicLibrary>("Dynamic Library");
99 lib->handle = handle;
100
101 return lib;
102}
103
104void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname)
105{
106 return dlsym(lib->handle, symname);
107}
108
110{
111 (void)lib; /* unused */
112 return dlerror();
113}
114
116{
117 dlclose(lib->handle);
118 MEM_freeN(lib);
119}
120
121#endif
Read Guarded memory(de)allocation.
#define NULL
char * BLI_dynlib_get_error_as_string(DynamicLibrary *lib)
Definition dynlib.cc:109
void * BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname)
Definition dynlib.cc:104
void BLI_dynlib_close(DynamicLibrary *lib)
Definition dynlib.cc:115
DynamicLibrary * BLI_dynlib_open(const char *name)
Definition dynlib.cc:89
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void * handle
Definition dynlib.cc:18
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84
static DynamicLibrary lib