From ea56b09e00b9b55ddc095ab663e2b7208f477635 Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Sat, 11 Nov 2017 14:25:10 +0500 Subject: [PATCH] New Module class for dynamic library linking --- src/xrCore/ModuleLookup.cpp | 48 ++++++++++++++++++++++++++++++++++--- src/xrCore/ModuleLookup.hpp | 19 +++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/xrCore/ModuleLookup.cpp b/src/xrCore/ModuleLookup.cpp index af05cea4d11..c8ec3b17a45 100644 --- a/src/xrCore/ModuleLookup.cpp +++ b/src/xrCore/ModuleLookup.cpp @@ -2,11 +2,53 @@ #include "ModuleLookup.hpp" -#define WIN32_LEAN_AND_MEAN -#include - namespace XRay { +Module::Module() : handle(nullptr) {} + +Module::Module(pcstr moduleName, bool log /*= true*/) +{ + open(moduleName, log); +} + +Module::~Module() +{ + close(); +} + +void* Module::open(pcstr moduleName, bool log /*= true*/) +{ + if (exist()) + close(); + + if (log) + Log("Loading DLL:", moduleName); + + handle = ::LoadLibrary(moduleName); + return handle; +} + +void Module::close() +{ + FreeLibrary(static_cast(handle)); + handle = nullptr; +} + +bool Module::exist() const +{ + return handle != nullptr; +} + +void* Module::operator()() const +{ + return handle; +} + +void* Module::getProcAddress(pcstr procName) const +{ + return ::GetProcAddress(static_cast(handle), procName); +} + HMODULE LoadLibrary(const char* libraryFileName, bool log) { if (log) diff --git a/src/xrCore/ModuleLookup.hpp b/src/xrCore/ModuleLookup.hpp index 689606140b5..8f8441db1fb 100644 --- a/src/xrCore/ModuleLookup.hpp +++ b/src/xrCore/ModuleLookup.hpp @@ -4,6 +4,25 @@ namespace XRay { +class XRCORE_API Module +{ + void* handle; + +public: + Module(); + Module(pcstr moduleName, bool log = true); + ~Module(); + + void* open(pcstr moduleName, bool log = true);; + void close(); + + bool exist() const; + + void* operator()() const; + + void* getProcAddress(pcstr procName) const; +}; + XRCORE_API HMODULE LoadLibrary(const char* libraryFileName, bool log = true); XRCORE_API void UnloadLibrary(HMODULE libraryHandle); XRCORE_API void* GetProcAddress(HMODULE libraryHandle, const char* procName);