forked from livebook-dev/pythonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdl.hpp
More file actions
60 lines (39 loc) · 1.14 KB
/
dl.hpp
File metadata and controls
60 lines (39 loc) · 1.14 KB
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
#pragma once
#include <string>
#if defined(_WIN32)
// Windows
#include <windows.h>
namespace pythonx::dl {
using LibraryHandle = HMODULE;
inline LibraryHandle open_library(const char *name) {
return LoadLibrary(name);
}
inline void *get_symbol(LibraryHandle lib, const char *name) {
return reinterpret_cast<void *>(GetProcAddress(lib, name));
}
inline bool close_library(LibraryHandle lib) { return FreeLibrary(lib); }
inline std::string error() {
auto code = GetLastError();
if (code == 0) {
return nullptr;
}
return "code " + std::to_string(code);
}
} // namespace pythonx::dl
#else
// Unix
#include <dlfcn.h>
namespace pythonx::dl {
using LibraryHandle = void *;
inline LibraryHandle open_library(const char *name) {
// Note that we want RTLD_GLOBAL, so that Python library symbols
// are visible to Python C extensions.
return dlopen(name, RTLD_GLOBAL | RTLD_LAZY);
}
inline void *get_symbol(LibraryHandle lib, const char *name) {
return dlsym(lib, name);
}
inline bool close_library(LibraryHandle lib) { return dlclose(lib) == 0; }
inline std::string error() { return dlerror(); }
} // namespace pythonx::dl
#endif