BRL-CAD
dlfcn.c
Go to the documentation of this file.
1 /* D L F C N . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * 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 file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <string.h>
24 
25 #ifdef HAVE_DLFCN_H
26 # include <dlfcn.h>
27 /* this should not be needed. Maybe an explicit cmake test? */
28 # ifndef HAVE_DLOPEN
29 # define HAVE_DLOPEN 1
30 # endif
31 #endif
32 #include "bio.h"
33 
34 #include "bu/file.h"
35 
36 void *
37 bu_dlopen(const char *path, int mode)
38 {
39 #ifdef HAVE_DLOPEN
40  return dlopen(path, mode);
41 #elif defined(WIN32)
42  return LoadLibrary(path);
43 #else
44  bu_log("dlopen not supported\n");
45  return NULL;
46 #endif
47 }
48 
49 void *
50 bu_dlsym(void *handle, const char *symbol)
51 {
52 #ifdef HAVE_DLOPEN
53  return dlsym(handle, symbol);
54 #elif defined(WIN32)
55  return GetProcAddress(handle, symbol);
56 #else
57  bu_log("dlsym not supported\n");
58  return NULL;
59 #endif
60 }
61 
62 int
63 bu_dlclose(void *handle)
64 {
65 #ifdef HAVE_DLOPEN
66  return dlclose(handle);
67 #elif defined(WIN32)
68  return 0;
69 #else
70  bu_log("dlclose not supported\n");
71  return 0;
72 #endif
73 }
74 
75 const char *
77 {
78 #ifdef HAVE_DLOPEN
79  return dlerror();
80 #elif defined(WIN32)
81  static char buf[BUFSIZ];
82  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), LANG_NEUTRAL, buf, BUFSIZ, NULL);
83  return (const char *)buf;
84 #else
85  bu_log("dlerror not supported\n");
86  return NULL;
87 #endif
88 }
89 
90 /*
91  * Local Variables:
92  * mode: C
93  * tab-width: 8
94  * indent-tabs-mode: t
95  * c-file-style: "stroustrup"
96  * End:
97  * ex: shiftwidth=4 tabstop=8
98  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Header file for the BRL-CAD common definitions.
const char * bu_dlerror(void)
Definition: dlfcn.c:76
void * bu_dlsym(void *handle, const char *symbol)
Definition: dlfcn.c:50
int bu_dlclose(void *handle)
Definition: dlfcn.c:63
void * bu_dlopen(const char *path, int mode)
Definition: dlfcn.c:37