56 lines · c
1#ifndef LLDB_TEST_DYLIB_H2#define LLDB_TEST_DYLIB_H3 4#include <stdio.h>5 6#ifdef _WIN327#include <Windows.h>8 9#define dylib_get_symbol(handle, name) GetProcAddress((HMODULE)handle, name)10#define dylib_close(handle) (!FreeLibrary((HMODULE)handle))11#else12#include <dlfcn.h>13 14#define dylib_get_symbol(handle, name) dlsym(handle, name)15#define dylib_close(handle) dlclose(handle)16#endif17 18 19inline void *dylib_open(const char *name) {20 char dylib_prefix[] =21#ifdef _WIN3222 "";23#else24 "lib";25#endif26 char dylib_suffix[] =27#ifdef _WIN3228 ".dll";29#elif defined(__APPLE__)30 ".dylib";31#else32 ".so";33#endif34 char fullname[1024];35 snprintf(fullname, sizeof(fullname), "%s%s%s", dylib_prefix, name, dylib_suffix);36#ifdef _WIN3237 return LoadLibraryA(fullname);38#else39 return dlopen(fullname, RTLD_NOW);40#endif41}42 43inline const char *dylib_last_error() {44#ifndef _WIN3245 return dlerror();46#else47 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#endif53}54 55#endif56