DSL
DynamicLinking.cpp
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #include <drift/dslcore.h>
12 #include <drift/DynamicLinking.h>
13 #include <drift/WhereIs.h>
14 
15 DL_HANDLE DSL_CC DL_FindAndOpen(const char * fn) {
16  WHEREIS_RESULTS * res = WhereIs(fn);
17  DL_HANDLE hDL = NULL;
18  if (res) {
19  for (int i=0; i < res->nCount && hDL == NULL; i++) {
20  hDL = DL_Open(res->sResults[i]);
21  }
22  WhereIs_FreeResults(res);
23  }
24  return hDL;
25 }
26 
27 #ifdef WIN32
28 
29 DL_HANDLE DSL_CC DL_Open(const char * fn) {
30  return LoadLibraryA(fn);
31 }
32 
33 void * DSL_CC DL_GetAddress(DL_HANDLE hHandle, const char * name) {
34  return GetProcAddress(hHandle,name);
35 }
36 
37 void DSL_CC DL_Close(DL_HANDLE hHandle) {
38  FreeLibrary(hHandle);
39 }
40 
41 static char dl_error_buf[256];
42 const char * DSL_CC DL_LastError() {
43  memset(dl_error_buf, 0, sizeof(dl_error_buf));
44  if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), LANG_SYSTEM_DEFAULT, dl_error_buf, sizeof(dl_error_buf)-1, NULL) == 0) {
45  strcpy(dl_error_buf, "Error getting error message!");
46  }
47  return dl_error_buf;
48 }
49 
50 #else
51 
52 DL_HANDLE DL_Open(const char * fn) {
53 #ifdef FREEBSD
54  HANDLE hHandle = dlopen(fn,RTLD_LAZY|RTLD_LOCAL);
55  if (hHandle == NULL) {
56  hHandle = dlopen(fn,RTLD_NOW|RTLD_LOCAL);
57  }
58 #else
59  HANDLE hHandle = dlopen(fn,RTLD_LAZY|RTLD_LOCAL|RTLD_DEEPBIND);
60  if (hHandle == NULL) {
61  hHandle = dlopen(fn,RTLD_NOW|RTLD_LOCAL|RTLD_DEEPBIND);
62  }
63 #endif
64  return hHandle;
65 }
66 
67 void * DL_GetAddress(DL_HANDLE hHandle, const char * name) {
68  return dlsym(hHandle,name);
69 }
70 
71 void DL_Close(DL_HANDLE hHandle) {
72  dlclose(hHandle);
73 }
74 
75 const char * DL_LastError() {
76  return dlerror();
77 }
78 
79 #endif
DL_HANDLE DSL_CC DL_FindAndOpen(const char *fn)
Attempts to find the named library in common library folders (c:\windows\system32 on Windows,...