# HG changeset patch # User David Demelier # Date 1385665387 -3600 # Node ID 274b4f216e6577708383be782acba16a0d956c7a # Parent 42b77e0161d045e5746662cf2403ff6baa9a9f74 DynLib: Windows support complete diff -r 42b77e0161d0 -r 274b4f216e65 C++/DynLib.cpp --- a/C++/DynLib.cpp Wed Nov 27 21:20:26 2013 +0100 +++ b/C++/DynLib.cpp Thu Nov 28 20:03:07 2013 +0100 @@ -28,24 +28,63 @@ #if defined(_MSC_VER) +namespace { + +std::string systemError() +{ + LPSTR error = nullptr; + std::string errmsg = "Unknown error"; + + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&error, 0, NULL); + + if (error) { + errmsg = std::string(error); + LocalFree(error); + } + + return errmsg; +} + +} + void DynLib::systemInit() { - // TODO: Windows + m_handle = nullptr; } -DynLib::Handle DynLib::systemLoad(const std::string &path, Policy policy); +DynLib::Handle DynLib::systemLoad(const std::string &path, Policy policy) const { - // TODO: Windows + Handle handle = LoadLibraryA(path.c_str()); + + if (handle == nullptr) + throw std::runtime_error(systemError()); + + return handle; } DynLib::Sym DynLib::systemSym(const std::string &name) { - // TODO: Windows + Sym sym; + + if (m_handle == nullptr) + throw std::runtime_error("library not loaded"); + + sym = GetProcAddress(m_handle, name.c_str()); + if (sym == nullptr) + throw std::out_of_range(systemError()); + + return sym; } void DynLib::systemClose() { - // TODO: Windows + if (m_handle != nullptr) + FreeLibrary(m_handle); } #else diff -r 42b77e0161d0 -r 274b4f216e65 C++/DynLib.h --- a/C++/DynLib.h Wed Nov 27 21:20:26 2013 +0100 +++ b/C++/DynLib.h Thu Nov 28 20:03:07 2013 +0100 @@ -21,6 +21,10 @@ #include +#if defined(_MSC_VER) +# include +#endif + /** * @class DynLib * @brief Load a dynamic module @@ -31,6 +35,8 @@ class DynLib { public: #if defined(_MSC_VER) + using Handle = HMODULE; + using Sym = FARPROC; #else using Handle = void *; using Sym = void *;