/src/opensc/src/common/libscdl.c
Line | Count | Source |
1 | | /* |
2 | | * libscdl.c: wrappers for dlfcn() interfaces |
3 | | * |
4 | | * Copyright (C) 2010 Martin Paljak <martin@martinpaljak.net> |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #ifdef HAVE_CONFIG_H |
22 | | #include <config.h> |
23 | | #endif |
24 | | |
25 | | #include "libscdl.h" |
26 | | |
27 | | #ifdef _WIN32 |
28 | | #include <shlwapi.h> |
29 | | #include <stdio.h> |
30 | | #include <windows.h> |
31 | | |
32 | | void *sc_dlopen(const char *filename) |
33 | | { |
34 | | DWORD flags = PathIsRelativeA(filename) ? 0 : LOAD_WITH_ALTERED_SEARCH_PATH; |
35 | | return (void *)LoadLibraryExA(filename, NULL, flags); |
36 | | } |
37 | | |
38 | | void *sc_dlsym(void *handle, const char *symbol) |
39 | | { |
40 | | return GetProcAddress((HMODULE)handle, symbol); |
41 | | } |
42 | | |
43 | | const char *sc_dlerror() |
44 | | { |
45 | | static char msg[1024]; |
46 | | DWORD err = GetLastError(); |
47 | | DWORD rv = 0; |
48 | | |
49 | | if (err == ERROR_BAD_EXE_FORMAT) { |
50 | | return "LoadLibrary/GetProcAddress failed: check module architecture matches application architecture"; |
51 | | } |
52 | | |
53 | | rv = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
54 | | NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
55 | | msg, sizeof(msg), NULL); |
56 | | if (rv == 0) { |
57 | | snprintf(msg, sizeof(msg), "LoadLibrary/GetProcAddress failed: %lx", err); |
58 | | } |
59 | | return msg; |
60 | | } |
61 | | |
62 | | int sc_dlclose(void *handle) |
63 | | { |
64 | | return FreeLibrary((HMODULE)handle); |
65 | | } |
66 | | |
67 | | #else |
68 | | |
69 | | #include <dlfcn.h> |
70 | | |
71 | | void *sc_dlopen(const char *filename) |
72 | 0 | { |
73 | 0 | return dlopen(filename, RTLD_LAZY | RTLD_LOCAL |
74 | 0 | #ifdef RTLD_DEEPBIND |
75 | 0 | | RTLD_DEEPBIND |
76 | 0 | #endif |
77 | 0 | ); |
78 | 0 | } |
79 | | |
80 | | void *sc_dlsym(void *handle, const char *symbol) |
81 | 0 | { |
82 | 0 | return dlsym(handle, symbol); |
83 | 0 | } |
84 | | |
85 | | const char *sc_dlerror(void) |
86 | 0 | { |
87 | 0 | return dlerror(); |
88 | 0 | } |
89 | | |
90 | | int sc_dlclose(void *handle) |
91 | 0 | { |
92 | 0 | return dlclose(handle); |
93 | 0 | } |
94 | | #endif |