|
16 | 16 | #include <qdmi/device.h> |
17 | 17 | #include <spdlog/spdlog.h> |
18 | 18 |
|
| 19 | +#include <array> |
19 | 20 | #include <cassert> |
20 | 21 | #include <cstddef> |
21 | 22 | #include <cstring> |
|
31 | 32 |
|
32 | 33 | #ifdef _WIN32 |
33 | 34 | #include <windows.h> |
| 35 | + |
| 36 | +#include <filesystem> |
34 | 37 | #else |
35 | 38 | #include <dlfcn.h> |
36 | 39 | #endif // _WIN32 |
37 | 40 |
|
38 | 41 | namespace qdmi { |
39 | 42 | #ifdef _WIN32 |
40 | | -#define DL_OPEN(lib) LoadLibraryA((lib)) |
| 43 | +namespace { |
| 44 | +[[nodiscard]] auto getDriverDirectory() -> std::filesystem::path { |
| 45 | + HMODULE module = nullptr; |
| 46 | + if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 47 | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 48 | + reinterpret_cast<LPCWSTR>(&getDriverDirectory), |
| 49 | + &module) == 0) { |
| 50 | + return {}; |
| 51 | + } |
| 52 | + |
| 53 | + std::wstring buffer(MAX_PATH, L'\0'); |
| 54 | + DWORD size = 0; |
| 55 | + while (true) { |
| 56 | + size = GetModuleFileNameW(module, buffer.data(), |
| 57 | + static_cast<DWORD>(buffer.size())); |
| 58 | + if (size == 0) { |
| 59 | + return {}; |
| 60 | + } |
| 61 | + if (size < buffer.size()) { |
| 62 | + buffer.resize(size); |
| 63 | + break; |
| 64 | + } |
| 65 | + buffer.resize(buffer.size() * 2); |
| 66 | + } |
| 67 | + |
| 68 | + return std::filesystem::path(buffer).parent_path(); |
| 69 | +} |
| 70 | + |
| 71 | +[[nodiscard]] auto loadDeviceLibrary(const std::string& libName) -> HMODULE { |
| 72 | + const auto requested = std::filesystem::path(libName); |
| 73 | + |
| 74 | + const std::filesystem::path path = requested.has_parent_path() |
| 75 | + ? requested |
| 76 | + : getDriverDirectory() / requested; |
| 77 | + |
| 78 | + return LoadLibraryExW(path.wstring().c_str(), nullptr, |
| 79 | + LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | |
| 80 | + LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); |
| 81 | +} |
| 82 | +} // namespace |
| 83 | + |
| 84 | +#define DL_OPEN(lib) loadDeviceLibrary((lib)) |
41 | 85 | #define DL_SYM(lib, sym) \ |
42 | 86 | reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>((lib)), (sym))) |
43 | 87 | #define DL_CLOSE(lib) FreeLibrary(static_cast<HMODULE>((lib))) |
@@ -337,7 +381,7 @@ auto QDMI_Session_impl_d::querySessionProperty(QDMI_Session_Property prop, |
337 | 381 |
|
338 | 382 | namespace qdmi { |
339 | 383 | Driver::Driver() { |
340 | | - for (const auto& [lib, prefix] : DYN_DEV_LIBS) { |
| 384 | + for (const auto& [lib, prefix] : std::array{DYN_DEV_LIBS}) { |
341 | 385 | addDynamicDeviceLibrary(lib, prefix); |
342 | 386 | } |
343 | 387 | } |
|
0 commit comments