2 * Cross Platform dso/dll load abstraction
3 * Copyright(C) 2008 Michael Jerris
5 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
6 * copies of the Software, and permit persons to whom the Software is
9 * This work is provided under this license on an "as is" basis, without warranty of any kind,
10 * either expressed or implied, including, without limitation, warranties that the covered code
11 * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
12 * risk as to the quality and performance of the covered code is with you. Should any covered
13 * code prove defective in any respect, you (not the initial developer or any other contributor)
14 * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
15 * constitutes an essential part of this license. No use of any covered code is authorized hereunder
16 * except under this disclaimer.
21 #include "switch_dso.h"
27 SWITCH_DECLARE(void) switch_dso_destroy(switch_dso_lib_t
*lib
)
35 SWITCH_DECLARE(switch_dso_lib_t
) switch_dso_open(const char *path
, int global
, char **err
)
39 lib
= LoadLibraryEx(path
, NULL
, 0);
42 lib
= LoadLibraryEx(path
, NULL
, LOAD_WITH_ALTERED_SEARCH_PATH
);
46 lib
= LoadLibraryEx(path
, NULL
, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
);
50 DWORD error
= GetLastError();
51 *err
= switch_mprintf("dll open error [%ul]\n", error
);
57 SWITCH_DECLARE(switch_dso_func_t
) switch_dso_func_sym(switch_dso_lib_t lib
, const char *sym
, char **err
)
59 FARPROC func
= GetProcAddress(lib
, sym
);
61 DWORD error
= GetLastError();
62 *err
= switch_mprintf("dll sym error [%ul]\n", error
);
64 return (switch_dso_func_t
) func
;
67 SWITCH_DECLARE(void *) switch_dso_data_sym(switch_dso_lib_t lib
, const char *sym
, char **err
)
69 FARPROC addr
= GetProcAddress(lib
, sym
);
71 DWORD error
= GetLastError();
72 *err
= switch_mprintf("dll sym error [%ul]\n", error
);
74 return (void *) (intptr_t) addr
;
80 ** {========================================================================
81 ** This is an implementation of loadlib based on the dlfcn interface.
82 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
83 ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
84 ** as an emulation layer on top of native functions.
85 ** =========================================================================
91 void switch_dso_destroy(switch_dso_lib_t
*lib
)
94 #ifndef HAVE_FAKE_DLCLOSE
101 switch_dso_lib_t
switch_dso_open(const char *path
, int global
, char **err
)
106 lib
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
108 lib
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
);
112 const char *dlerr
= dlerror();
113 /* Work around broken uclibc returning NULL on both dlopen() and dlerror() */
115 *err
= strdup(dlerr
);
117 *err
= strdup("Unknown error");
123 switch_dso_func_t
switch_dso_func_sym(switch_dso_lib_t lib
, const char *sym
, char **err
)
125 void *func
= dlsym(lib
, sym
);
127 *err
= strdup(dlerror());
129 return (switch_dso_func_t
) (intptr_t) func
;
132 void *switch_dso_data_sym(switch_dso_lib_t lib
, const char *sym
, char **err
)
134 void *addr
= dlsym(lib
, sym
);
136 char *err_str
= NULL
;
139 if (!(addr
= dlsym(lib
, sym
))) {
140 err_str
= (char *)dlerror();
144 *err
= strdup(err_str
);
151 /* }====================================================== */
161 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet