library.c (1837B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "library.h" 17 18 #if defined(OS_WINDOWS) 19 #include <Windows.h> 20 void* 21 library_open(const char* filename) 22 { 23 if(!filename) 24 return NULL; 25 return (void*)LoadLibraryA(filename); 26 } 27 28 res_T 29 library_close(void* lib) 30 { 31 BOOL b; 32 if(!lib) 33 return RES_BAD_ARG; 34 35 b = FreeLibrary((HMODULE)lib); 36 if(!b) 37 return RES_UNKNOWN_ERR; 38 39 return RES_OK; 40 } 41 42 void* 43 library_get_symbol(void* lib, const char* sym) 44 { 45 union { FARPROC proc; void* ptr; } ucast; 46 STATIC_ASSERT(sizeof(FARPROC) == sizeof(void*), Unexpected_type_size); 47 ucast.proc = GetProcAddress((HMODULE)lib, sym); 48 return ucast.ptr; 49 } 50 51 #elif defined(OS_UNIX) || defined(OS_MACH) 52 #include <dlfcn.h> 53 #include <stdio.h> 54 55 void* 56 library_open(const char* filename) 57 { 58 if(!filename) return NULL; 59 return dlopen(filename, RTLD_NOW|RTLD_GLOBAL); 60 } 61 62 void* 63 library_get_symbol(void* lib, const char* sym) 64 { 65 if(!lib || !sym) return NULL; 66 return dlsym(lib, sym); 67 } 68 69 res_T 70 library_close(void* handle) 71 { 72 int err = 0; 73 if(!handle) return RES_BAD_ARG; 74 75 err = dlclose(handle); 76 if(err) return RES_UNKNOWN_ERR; 77 78 return RES_OK; 79 } 80 #endif /* OS_<XXX> */