From b6e2181f0e9e32e5a6222a7044f758ebc3d0bd47 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 03:01:11 +0300 Subject: [PATCH] Plugin: load shared libraries on Windows Add a private dlopen/dlsym/dlclose compatibility shim over LoadLibraryW / GetProcAddress and use it from the loader, host and manager. Allowlist Plugin for the Windows target. Verified with the Windows cross build under Wine 11 (x86_64-w64-mingw32 GCC 16.2): Plugin suite 6 tests, 5 passed, 1 skipped, 0 failed. --- cmake/Targets.cmake | 1 + src/Plugin/DlCompat.hpp | 76 +++++++++++++++++++++++++++++++++++++++++ src/Plugin/Host.cpp | 3 ++ src/Plugin/Loader.cpp | 3 ++ src/Plugin/Manager.cpp | 3 ++ 5 files changed, 86 insertions(+) create mode 100644 src/Plugin/DlCompat.hpp diff --git a/cmake/Targets.cmake b/cmake/Targets.cmake index 2a2610a..2185628 100644 --- a/cmake/Targets.cmake +++ b/cmake/Targets.cmake @@ -72,6 +72,7 @@ set(VOID_WINDOWS_MODULES Messaging Observability ObservabilityHooks + Plugin Scheduler Service Sqlite diff --git a/src/Plugin/DlCompat.hpp b/src/Plugin/DlCompat.hpp new file mode 100644 index 0000000..e834e2a --- /dev/null +++ b/src/Plugin/DlCompat.hpp @@ -0,0 +1,76 @@ +#pragma once + +// Minimal dlopen/dlsym/dlclose compatibility for Windows (LoadLibrary). +// Private to the Plugin module; included instead of on Windows. + +#ifdef _WIN32 + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include + +#include +#include + +#ifndef RTLD_NOW +#define RTLD_NOW 0 +#endif +#ifndef RTLD_LOCAL +#define RTLD_LOCAL 0 +#endif + +namespace void_dl +{ +inline std::string& error_slot() noexcept +{ + static thread_local std::string error; + return error; +} + +inline void* open_wide(const wchar_t* path) +{ + const HMODULE module = ::LoadLibraryW(path); + if (module == nullptr) { + error_slot() = "LoadLibrary failed (" + + std::to_string(::GetLastError()) + ")"; + return nullptr; + } + return reinterpret_cast(module); +} +} // namespace void_dl + +inline void* dlopen(const wchar_t* path, int) +{ + return void_dl::open_wide(path); +} + +inline void* dlopen(const char* path, int) +{ + const std::u8string utf8{reinterpret_cast(path)}; + const std::filesystem::path wide{utf8}; + return void_dl::open_wide(wide.c_str()); +} + +inline void* dlsym(void* handle, const char* symbol) +{ + return reinterpret_cast( + ::GetProcAddress(static_cast(handle), symbol)); +} + +inline int dlclose(void* handle) +{ + return ::FreeLibrary(static_cast(handle)) != 0 ? 0 : -1; +} + +inline char* dlerror() +{ + return void_dl::error_slot().empty() + ? nullptr + : const_cast(void_dl::error_slot().c_str()); +} + +#endif // _WIN32 diff --git a/src/Plugin/Host.cpp b/src/Plugin/Host.cpp index 046dd8d..d88faed 100644 --- a/src/Plugin/Host.cpp +++ b/src/Plugin/Host.cpp @@ -6,6 +6,9 @@ #if defined(__unix__) || defined(__APPLE__) #include #define VOID_HOST_HAS_DLOPEN 1 +#elif defined(_WIN32) +#include "DlCompat.hpp" +#define VOID_HOST_HAS_DLOPEN 1 #endif #include diff --git a/src/Plugin/Loader.cpp b/src/Plugin/Loader.cpp index 18f8f4a..cefb99e 100644 --- a/src/Plugin/Loader.cpp +++ b/src/Plugin/Loader.cpp @@ -4,6 +4,9 @@ #if defined(__unix__) || defined(__APPLE__) #include #define VOID_HAS_DLOPEN 1 +#elif defined(_WIN32) +#include "DlCompat.hpp" +#define VOID_HAS_DLOPEN 1 #endif #include diff --git a/src/Plugin/Manager.cpp b/src/Plugin/Manager.cpp index ee0ff19..8d9f47c 100644 --- a/src/Plugin/Manager.cpp +++ b/src/Plugin/Manager.cpp @@ -5,6 +5,9 @@ #if defined(__unix__) || defined(__APPLE__) #include #define VOID_HAS_DLOPEN 1 +#elif defined(_WIN32) +#include "DlCompat.hpp" +#define VOID_HAS_DLOPEN 1 #endif #include