Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
dl_shim.hpp
Go to the documentation of this file.
1#pragma once
2
3// Dynamic-loader shim for Windows/POSIX parity.
4//
5// The host plugin loaders (`plugin_slot_{clap,vst3,lv2}.cpp`,
6// `scanner_clap.cpp`) use `<dlfcn.h>` to dlopen plugin bundles. Windows
7// does not ship `<dlfcn.h>`; the release-cli build broke on every v0.4.0
8// and later tag because of that. This header papers over the split by
9// exposing a `dlopen` / `dlsym` / `dlclose` / `dlerror` surface that
10// forwards to the platform-native loader.
11//
12// Scope is deliberately minimal: only the symbols the host loaders
13// actually call. If callers need more of POSIX `<dlfcn.h>`, extend the
14// Windows branch rather than reaching for a third-party port.
15
16#if defined(_WIN32)
17
18#ifndef WIN32_LEAN_AND_MEAN
19#define WIN32_LEAN_AND_MEAN
20#endif
21#ifndef NOMINMAX
22#define NOMINMAX
23#endif
24#include <windows.h>
25
26// POSIX flag constants — no-ops on Windows, kept so call sites don't
27// need #ifdefs at every use.
28#ifndef RTLD_LAZY
29#define RTLD_LAZY 0
30#endif
31#ifndef RTLD_LOCAL
32#define RTLD_LOCAL 0
33#endif
34#ifndef RTLD_NOW
35#define RTLD_NOW 0
36#endif
37
38namespace pulp::host::detail {
39
40inline void* dl_open(const char* path, int /*flags*/) {
41 // Invalid plugin probes are expected during scanning/tests. Suppress the
42 // Windows loader's critical-error UI so failures return NULL promptly.
43 DWORD old_error_mode = 0;
44 constexpr DWORD kLoaderProbeMode =
45 SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
46 const BOOL changed_error_mode =
47 ::SetThreadErrorMode(kLoaderProbeMode, &old_error_mode);
48
49 HMODULE module = ::LoadLibraryA(path);
50
51 if (changed_error_mode) {
52 ::SetThreadErrorMode(old_error_mode, nullptr);
53 }
54 // LoadLibraryA returns NULL on failure. Caller inspects return value.
55 return reinterpret_cast<void*>(module);
56}
57
58inline void* dl_sym(void* handle, const char* name) {
59 return reinterpret_cast<void*>(
60 ::GetProcAddress(reinterpret_cast<HMODULE>(handle), name));
61}
62
63inline int dl_close(void* handle) {
64 // POSIX dlclose returns 0 on success; Windows FreeLibrary returns
65 // non-zero on success. Normalize to POSIX.
66 return ::FreeLibrary(reinterpret_cast<HMODULE>(handle)) ? 0 : -1;
67}
68
69inline const char* dl_error() {
70 // Windows does not have a dlerror equivalent that returns a stable
71 // thread-local string. Call sites already log the path; this stub
72 // lets them keep compiling. Returning nullptr is safe — existing
73 // call sites already guard with `err ? err : "unknown"`.
74 return nullptr;
75}
76
77} // namespace pulp::host::detail
78
79// POSIX-style entry points in the global namespace so call sites don't
80// change.
81inline void* dlopen(const char* path, int flags) {
82 return ::pulp::host::detail::dl_open(path, flags);
83}
84inline void* dlsym(void* handle, const char* name) {
85 return ::pulp::host::detail::dl_sym(handle, name);
86}
87inline int dlclose(void* handle) {
88 return ::pulp::host::detail::dl_close(handle);
89}
90inline const char* dlerror() {
91 return ::pulp::host::detail::dl_error();
92}
93
94#else // !_WIN32
95
96#include <dlfcn.h>
97
98#endif // _WIN32
Definition baked_codec.hpp:158