| 84 | SharedLibrary& SharedLibrary::operator=(SharedLibrary const& other) = default; |
| 85 | |
| 86 | void |
| 87 | SharedLibrary::Load(int flags) |
| 88 | { |
| 89 | if (d->m_Handle) |
| 90 | throw std::logic_error(std::string("Library already loaded: ") + GetFilePath()); |
| 91 | std::string libPath = GetFilePath(); |
| 92 | #ifdef US_PLATFORM_POSIX |
| 93 | d->m_Handle = dlopen(libPath.c_str(), flags); |
| 94 | if (!d->m_Handle) |
| 95 | { |
| 96 | std::error_code err_code(errno, std::generic_category()); |
| 97 | std::string err_msg = "Error loading " + libPath + "."; |
| 98 | char const* err = dlerror(); |
| 99 | if (err) |
| 100 | { |
| 101 | err_msg += " " + std::string(err); |
| 102 | } |
| 103 | |
| 104 | d->m_Handle = nullptr; |
| 105 | |
| 106 | // Bundle of origin information is not available here. It will be |
| 107 | // BundlePrivate::Start0() will catch this system_error and create |
| 108 | // a SharedLibraryException. |
| 109 | throw std::system_error(err_code, err_msg); |
| 110 | } |
| 111 | #else |
| 112 | US_UNUSED(flags); |
| 113 | std::wstring wpath(cppmicroservices::util::ToWString(libPath)); |
| 114 | d->m_Handle = LoadLibraryW(wpath.c_str()); |
| 115 | |
| 116 | if (!d->m_Handle) |
| 117 | { |
| 118 | std::error_code err_code(GetLastError(), std::generic_category()); |
| 119 | std::string errMsg = "Loading "; |
| 120 | errMsg.append(libPath).append("failed with error: ").append(util::GetLastWin32ErrorStr()); |
| 121 | |
| 122 | d->m_Handle = nullptr; |
| 123 | |
| 124 | // Bundle of origin information is not available here. Use try/catch |
| 125 | // around SharedLibrary::Load(), and throw a SharedLibraryException |
| 126 | // inside the catch statement, with the available bundle of origin. |
| 127 | throw std::system_error(err_code, errMsg); |
| 128 | } |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | SharedLibrary::Load() |