1*e0dbd025SPavel Labath #ifndef LLDB_TEST_DYLIB_H
2*e0dbd025SPavel Labath #define LLDB_TEST_DYLIB_H
3*e0dbd025SPavel Labath
4*e0dbd025SPavel Labath #include <stdio.h>
5*e0dbd025SPavel Labath
6*e0dbd025SPavel Labath #ifdef _WIN32
7*e0dbd025SPavel Labath #include <Windows.h>
8*e0dbd025SPavel Labath
9*e0dbd025SPavel Labath #define dylib_get_symbol(handle, name) GetProcAddress((HMODULE)handle, name)
10*e0dbd025SPavel Labath #define dylib_close(handle) (!FreeLibrary((HMODULE)handle))
11*e0dbd025SPavel Labath #else
12*e0dbd025SPavel Labath #include <dlfcn.h>
13*e0dbd025SPavel Labath
14*e0dbd025SPavel Labath #define dylib_get_symbol(handle, name) dlsym(handle, name)
15*e0dbd025SPavel Labath #define dylib_close(handle) dlclose(handle)
16*e0dbd025SPavel Labath #endif
17*e0dbd025SPavel Labath
18*e0dbd025SPavel Labath
dylib_open(const char * name)19*e0dbd025SPavel Labath inline void *dylib_open(const char *name) {
20*e0dbd025SPavel Labath char dylib_prefix[] =
21*e0dbd025SPavel Labath #ifdef _WIN32
22*e0dbd025SPavel Labath "";
23*e0dbd025SPavel Labath #else
24*e0dbd025SPavel Labath "lib";
25*e0dbd025SPavel Labath #endif
26*e0dbd025SPavel Labath char dylib_suffix[] =
27*e0dbd025SPavel Labath #ifdef _WIN32
28*e0dbd025SPavel Labath ".dll";
29*e0dbd025SPavel Labath #elif defined(__APPLE__)
30*e0dbd025SPavel Labath ".dylib";
31*e0dbd025SPavel Labath #else
32*e0dbd025SPavel Labath ".so";
33*e0dbd025SPavel Labath #endif
34*e0dbd025SPavel Labath char fullname[1024];
35*e0dbd025SPavel Labath snprintf(fullname, sizeof(fullname), "%s%s%s", dylib_prefix, name, dylib_suffix);
36*e0dbd025SPavel Labath #ifdef _WIN32
37*e0dbd025SPavel Labath return LoadLibraryA(fullname);
38*e0dbd025SPavel Labath #else
39*e0dbd025SPavel Labath return dlopen(fullname, RTLD_NOW);
40*e0dbd025SPavel Labath #endif
41*e0dbd025SPavel Labath }
42*e0dbd025SPavel Labath
dylib_last_error()43*e0dbd025SPavel Labath inline const char *dylib_last_error() {
44*e0dbd025SPavel Labath #ifndef _WIN32
45*e0dbd025SPavel Labath return dlerror();
46*e0dbd025SPavel Labath #else
47*e0dbd025SPavel Labath DWORD err = GetLastError();
48*e0dbd025SPavel Labath char *msg;
49*e0dbd025SPavel Labath FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
50*e0dbd025SPavel Labath NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *)&msg, 0, NULL);
51*e0dbd025SPavel Labath return msg;
52*e0dbd025SPavel Labath #endif
53*e0dbd025SPavel Labath }
54*e0dbd025SPavel Labath
55*e0dbd025SPavel Labath #endif
56