Skip to content

Commit ea56b09

Browse files
committed
New Module class for dynamic library linking
1 parent f802a73 commit ea56b09

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/xrCore/ModuleLookup.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,53 @@
22

33
#include "ModuleLookup.hpp"
44

5-
#define WIN32_LEAN_AND_MEAN
6-
#include <windows.h>
7-
85
namespace XRay
96
{
7+
Module::Module() : handle(nullptr) {}
8+
9+
Module::Module(pcstr moduleName, bool log /*= true*/)
10+
{
11+
open(moduleName, log);
12+
}
13+
14+
Module::~Module()
15+
{
16+
close();
17+
}
18+
19+
void* Module::open(pcstr moduleName, bool log /*= true*/)
20+
{
21+
if (exist())
22+
close();
23+
24+
if (log)
25+
Log("Loading DLL:", moduleName);
26+
27+
handle = ::LoadLibrary(moduleName);
28+
return handle;
29+
}
30+
31+
void Module::close()
32+
{
33+
FreeLibrary(static_cast<HMODULE>(handle));
34+
handle = nullptr;
35+
}
36+
37+
bool Module::exist() const
38+
{
39+
return handle != nullptr;
40+
}
41+
42+
void* Module::operator()() const
43+
{
44+
return handle;
45+
}
46+
47+
void* Module::getProcAddress(pcstr procName) const
48+
{
49+
return ::GetProcAddress(static_cast<HMODULE>(handle), procName);
50+
}
51+
1052
HMODULE LoadLibrary(const char* libraryFileName, bool log)
1153
{
1254
if (log)

src/xrCore/ModuleLookup.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44

55
namespace XRay
66
{
7+
class XRCORE_API Module
8+
{
9+
void* handle;
10+
11+
public:
12+
Module();
13+
Module(pcstr moduleName, bool log = true);
14+
~Module();
15+
16+
void* open(pcstr moduleName, bool log = true);;
17+
void close();
18+
19+
bool exist() const;
20+
21+
void* operator()() const;
22+
23+
void* getProcAddress(pcstr procName) const;
24+
};
25+
726
XRCORE_API HMODULE LoadLibrary(const char* libraryFileName, bool log = true);
827
XRCORE_API void UnloadLibrary(HMODULE libraryHandle);
928
XRCORE_API void* GetProcAddress(HMODULE libraryHandle, const char* procName);

0 commit comments

Comments
 (0)