1 #ifndef IVL_ivl_dlfcn_H
2 #define IVL_ivl_dlfcn_H
3 /*
4  * Copyright (c) 2001-2014 Stephen Williams (steve@icarus.com)
5  *
6  *    This source code is free software; you can redistribute it
7  *    and/or modify it in source code form under the terms of the GNU
8  *    General Public License as published by the Free Software
9  *    Foundation; either version 2 of the License, or (at your option)
10  *    any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #if defined(__MINGW32__)
23 # include <windows.h>
24 # include <cstdio>
25 typedef void * ivl_dll_t;
26 #elif defined(HAVE_DLFCN_H)
27 # include  <dlfcn.h>
28 typedef void* ivl_dll_t;
29 #elif defined(HAVE_DL_H)
30 # include  <dl.h>
31 typedef shl_t ivl_dll_t;
32 #endif
33 
34 #if defined(__MINGW32__)
ivl_dlopen(const char * name,bool)35 inline ivl_dll_t ivl_dlopen(const char *name, bool)
36 {
37       static char full_name[4096];
38       unsigned long length = GetFullPathName(name, sizeof(full_name),
39                                              full_name, NULL);
40       if ((length == 0) || (length > sizeof(full_name)))
41             return 0;
42 
43       return (void *)LoadLibrary(full_name);
44 }
45 
ivl_dlsym(ivl_dll_t dll,const char * nm)46 inline void *ivl_dlsym(ivl_dll_t dll, const char *nm)
47 { return (void *)GetProcAddress((HINSTANCE)dll,nm);}
48 
ivl_dlclose(ivl_dll_t dll)49 inline void ivl_dlclose(ivl_dll_t dll)
50 { (void)FreeLibrary((HINSTANCE)dll);}
51 
dlerror(void)52 inline const char *dlerror(void)
53 {
54   static char msg[256];
55   unsigned long err = GetLastError();
56   FormatMessage(
57 		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
58 		NULL,
59 		err,
60 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
61 		(LPTSTR) &msg,
62 		sizeof(msg) - 1,
63 		NULL
64 		);
65   return msg;
66 }
67 
68 #elif defined(HAVE_DLFCN_H)
ivl_dlopen(const char * name,bool global_flag)69 inline ivl_dll_t ivl_dlopen(const char*name, bool global_flag)
70 { return dlopen(name,RTLD_LAZY|(global_flag?RTLD_GLOBAL:0)); }
71 
ivl_dlsym(ivl_dll_t dll,const char * nm)72 inline void* ivl_dlsym(ivl_dll_t dll, const char*nm)
73 {
74       void*sym = dlsym(dll, nm);
75 	/* Not found? try without the leading _ */
76       if (sym == 0 && nm[0] == '_')
77 	    sym = dlsym(dll, nm+1);
78       return sym;
79 }
80 
ivl_dlclose(ivl_dll_t dll)81 inline void ivl_dlclose(ivl_dll_t dll)
82 { dlclose(dll); }
83 
84 #elif defined(HAVE_DL_H)
ivl_dlopen(const char * name)85 inline ivl_dll_t ivl_dlopen(const char*name)
86 { return shl_load(name, BIND_IMMEDIATE, 0); }
87 
ivl_dlsym(ivl_dll_t dll,const char * nm)88 inline void* ivl_dlsym(ivl_dll_t dll, const char*nm)
89 {
90       void*sym;
91       int rc = shl_findsym(&dll, nm, TYPE_PROCEDURE, &sym);
92       return (rc == 0) ? sym : 0;
93 }
94 
ivl_dlclose(ivl_dll_t dll)95 inline void ivl_dlclose(ivl_dll_t dll)
96 { shl_unload(dll); }
97 
dlerror(void)98 inline const char*dlerror(void)
99 { return strerror( errno ); }
100 #endif
101 
102 #endif /* IVL_ivl_dlfcn_H */
103