Mercurial > code
changeset 196:274b4f216e65
DynLib: Windows support complete
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 28 Nov 2013 20:03:07 +0100 |
parents | 42b77e0161d0 |
children | 0b029b53ff55 |
files | C++/DynLib.cpp C++/DynLib.h |
diffstat | 2 files changed, 50 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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 <string> +#if defined(_MSC_VER) +# include <Windows.h> +#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 *;