38 {
39 if (!std::filesystem::exists(libraryFilePath))
40 throw std::runtime_error("File does not exist: " + std::string(libraryFilePath));
41
42#ifdef _WIN32
43 auto libraryHandle = LoadLibraryA(libraryFilePath.data());
44#else
45 auto libraryHandle = dlopen(libraryFilePath.data(), RTLD_LAZY);
46#endif
47
48 if (!libraryHandle)
49 throw std::runtime_error("Failed to load library: " + std::string(libraryFilePath));
50
51 auto loadFunctionAddress = GetProcAddress(libraryHandle, LOAD_FUNCTION_NAME);
52 if (!loadFunctionAddress)
53 throw std::runtime_error(
54 "Failed to get load function address: " + std::string(LOAD_FUNCTION_NAME)
55 );
56
57 auto loadFunction = reinterpret_cast<LOAD_FUNCTION_SIGNATURE>(loadFunctionAddress);
58
59 if (auto* group = loadFunction(_globalSpecEnvironment)) {
60 _loadedLibraries.emplace(libraryFilePath, libraryHandle);
61 return group;
62 } else {
63 FreeLibrary(libraryHandle);
64 throw std::runtime_error(
65 "Failed to load group from library: " + std::string(libraryFilePath)
66 );
67 }
68 }