chiark / gitweb /
gpg agent lockup fix: Interrupt main loop when active_connections_value==0
[gnupg2.git] / common / dynload.h
1 /* dynload.h - Wrapper functions for run-time dynamic loading
2  *      Copyright (C) 2003, 2010 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify it
7  * under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * GnuPG is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copies of the GNU General Public License
27  * and the GNU Lesser General Public License along with this program;
28  * if not, see <https://www.gnu.org/licenses/>.
29  */
30
31 #ifndef GNUPG_COMMON_DYNLOAD_H
32 #define GNUPG_COMMON_DYNLOAD_H
33
34 #ifndef __MINGW32__
35 # include <dlfcn.h>
36 #else
37 # include <windows.h>
38 # include "utf8conv.h"
39 # include "mischelp.h"
40 # define RTLD_LAZY 0
41
42 static inline void *
43 dlopen (const char *name, int flag)
44 {
45   void *hd;
46 #ifdef HAVE_W32CE_SYSTEM
47   wchar_t *wname = utf8_to_wchar (name);
48   hd = wname? LoadLibrary (wname) : NULL;
49   xfree (wname);
50 #else
51   hd = LoadLibrary (name);
52 #endif
53   (void)flag;
54   return hd;
55 }
56
57 static inline void *
58 dlsym (void *hd, const char *sym)
59 {
60   if (hd && sym)
61     {
62 #ifdef HAVE_W32CE_SYSTEM
63       wchar_t *wsym = utf8_to_wchar (sym);
64       void *fnc = wsym? GetProcAddress (hd, wsym) : NULL;
65       xfree (wsym);
66 #else
67       void *fnc = GetProcAddress (hd, sym);
68 #endif
69       if (!fnc)
70         return NULL;
71       return fnc;
72     }
73   return NULL;
74 }
75
76
77 static inline const char *
78 dlerror (void)
79 {
80   static char buf[32];
81   snprintf (buf, sizeof buf, "ec=%lu", GetLastError ());
82   return buf;
83 }
84
85
86 static inline int
87 dlclose (void * hd)
88 {
89   if (hd)
90     {
91       CloseHandle (hd);
92       return 0;
93     }
94   return -1;
95 }
96 # endif /*__MINGW32__*/
97 #endif /*GNUPG_COMMON_DYNLOAD_H*/