summaryrefslogtreecommitdiff
path: root/lib/3rdparty/cdl.c
blob: d7a226a88f8f8f1b6f28802a2b6be69f90b22c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifndef size_t
#  include <stddef.h>
#endif

#if defined(_WIN32) || defined(_WIN64)
#  include <windows.h>
#elif defined(__posix__) || defined(__unix__) || defined(__linux__) || defined(__APPLE__)
#  include <dlfcn.h>
#else
#  warning "cdl: unsupported os"
#endif

void* chckDlLoad(const char *file, const char **outError)
{
   assert(file);
   void *handle = NULL;
   const char *error = NULL;

#if defined(_WIN32) || defined(_WIN64)
#if defined(__WINRT__)
   /**
    * WinRT only publically supports LoadPackagedLibrary() for loading .dll
    * files. LoadLibrary() is a private API, and not available for apps
    * (that can be published to MS' Windows Store.)
    */
   handle = (void*)LoadPackagedLibrary(file, 0);
#else
   handle = (void*)LoadLibrary(file);
#endif

   if (!handle)
      error = "Failed to load dll file.";
#elif defined(__posix__) || defined(__unix__) || defined(__linux__) || defined(__APPLE__)
   if (!(handle = dlopen(file, RTLD_NOW | RTLD_LOCAL)))
      error = dlerror();
#else
   error = "cdl: unsupported os";
#endif

   if (outError)
      *outError = error;

   return handle;
}

void* chckDlLoadSymbol(void *handle, const char *name, const char **outError)
{
   assert(handle);
   void *symbol = NULL;
   const char *error = NULL;

#if defined(_WIN32) || defined(_WIN64)
   if (!(symbol = (void*)GetProcAddress((HMODULE)handle, name)))
      error = "Failed to load symbol.";
#elif defined(__posix__) || defined(__unix__) || defined(__linux__) || defined(__APPLE__)
   if (!(symbol = dlsym(handle, name))) {
      size_t len = strlen(name) + 1;
      char *nname = calloc(1, len + 1);

      if (nname) {
         /* append an underscore for platforms that need that. */
         nname[0] = '_';
         memcpy(nname + 1, name, len);
         symbol = dlsym(handle, nname);
         free(nname);
      }
   }

   if (!symbol)
      error = dlerror();
#else
   error = "cdl: unsupported os";
#endif

   if (outError)
      *outError = error;

   return symbol;
}

void chckDlUnload(void *handle)
{
   assert(handle);

#if defined(_WIN32) || defined(_WIN64)
   FreeLibrary((HMODULE)handle);
#elif defined(__posix__) || defined(__unix__) || defined(__linux__) || defined(__APPLE__)
   dlclose(handle);
#endif
}

/* vim: set ts=8 sw=3 tw=0 :*/