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