2024-05-31 13:12:28 -04:00
|
|
|
#include "dlhandle.h"
|
|
|
|
|
2024-05-31 16:33:40 -04:00
|
|
|
#include <string>
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <dlfcn.h>
|
|
|
|
#else
|
2024-05-31 16:33:40 -04:00
|
|
|
# include <cassert>
|
2024-05-31 13:12:28 -04:00
|
|
|
# include <sstream>
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2024-05-31 16:33:40 -04:00
|
|
|
using namespace std::string_literals;
|
2024-05-31 13:12:28 -04:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
Dlhandle::Dlhandle(const fs::path &fpath)
|
|
|
|
{
|
2024-05-31 13:12:28 -04:00
|
|
|
chandle = dlopen(fpath.c_str(), RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
if (!chandle) {
|
2024-05-31 16:33:40 -04:00
|
|
|
throw Exception("dlopen: "s + dlerror());
|
2024-05-31 13:12:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
Dlhandle::~Dlhandle()
|
|
|
|
{
|
2024-05-31 13:12:28 -04:00
|
|
|
if (chandle) dlclose(chandle);
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void *Dlhandle::get_internal(const char *symbol) const
|
|
|
|
{
|
2024-05-31 13:12:28 -04:00
|
|
|
return dlsym(chandle, symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // defined(_WIN32)
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
Dlhandle::Dlhandle(const fs::path &fpath)
|
|
|
|
{
|
2024-05-31 16:33:40 -04:00
|
|
|
fs::path afpath = fs::absolute(fpath);
|
|
|
|
|
|
|
|
// Suppress the "Entry Point Not Found" dialog, caused by outdated nvcuda.dll from the GPU driver
|
|
|
|
UINT lastErrorMode = GetErrorMode();
|
2024-06-25 17:22:51 -04:00
|
|
|
SetErrorMode(lastErrorMode | SEM_FAILCRITICALERRORS);
|
2024-05-31 16:33:40 -04:00
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
chandle = LoadLibraryExW(afpath.c_str(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
|
|
|
|
|
2024-06-25 17:22:51 -04:00
|
|
|
SetErrorMode(lastErrorMode);
|
2024-05-31 16:33:40 -04:00
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
if (!chandle) {
|
2024-05-31 16:33:40 -04:00
|
|
|
DWORD err = GetLastError();
|
2024-05-31 13:12:28 -04:00
|
|
|
std::ostringstream ss;
|
2024-05-31 16:33:40 -04:00
|
|
|
ss << "LoadLibraryExW failed with error 0x" << std::hex << err;
|
2024-05-31 13:12:28 -04:00
|
|
|
throw Exception(ss.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
Dlhandle::~Dlhandle()
|
|
|
|
{
|
2024-05-31 13:12:28 -04:00
|
|
|
if (chandle) FreeLibrary(HMODULE(chandle));
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void *Dlhandle::get_internal(const char *symbol) const
|
|
|
|
{
|
2024-05-31 13:12:28 -04:00
|
|
|
return GetProcAddress(HMODULE(chandle), symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(_WIN32)
|