Repository navigation
Expand file tree
/
Copy pathDynamicLibraryModule.cpp
More file actions
executable file
·108 lines (93 loc) · 2.61 KB
/
Copy pathDynamicLibraryModule.cpp
File metadata and controls
executable file
·108 lines (93 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//==============================================================================
// Copyright (c) 2011-2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools
/// \file
/// \brief Base class for dynamic loading of .so/.dll.
//==============================================================================
#include "DynamicLibraryModule.h"
DynamicLibraryModule::DynamicLibraryModule(void)
: m_Module(NULL)
{
}
DynamicLibraryModule::DynamicLibraryModule(const std::string& name)
: m_Module(NULL)
{
LoadModule(name);
}
DynamicLibraryModule::~DynamicLibraryModule(void)
{
UnloadModule();
}
bool DynamicLibraryModule::LoadModule(const std::string& name)
{
#ifdef _WIN32
char exe_path[MAX_PATH] = {0};
DWORD len = GetModuleFileNameA(nullptr, exe_path, MAX_PATH);
if (len == 0 || len == MAX_PATH)
{
return false;
}
std::string full_path(exe_path);
size_t last_backslash = full_path.rfind('\\');
if (last_backslash != std::string::npos)
{
full_path.resize(last_backslash + 1);
}
// Append the DLL name
full_path.append(name);
m_Module = LoadLibraryA(full_path.c_str());
if (m_Module == nullptr)
{
// Fall back to the name passed in, in case it is a fully-qualified name already.
m_Module = LoadLibraryA(name.c_str());
}
#else
m_Module = dlopen(name.c_str(), RTLD_LAZY);
#endif
return (m_Module != NULL);
}
// start loading based on the vector of library names. First success
// terminates the loading attempts. Needed for CentOS6 and OpenCL.
// The Catalyst installer (at least in Catalyst 12.1) creates libOpenCL.so.1
// but no symbolic link to libOpenCL.so. This symbolic link exists on other
// distributions where multi-step repackaging is required (build a .deb, run it)
bool DynamicLibraryModule::LoadModule(const std::vector<std::string>& names)
{
bool loaded = false;
for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++)
{
loaded = LoadModule(*it);
if (loaded)
{
return loaded;
}
}
return loaded;
}
void DynamicLibraryModule::UnloadModule()
{
if (m_Module != NULL)
{
#ifdef _WIN32
FreeLibrary(m_Module);
#else
dlclose(m_Module);
#endif
m_Module = NULL;
}
}
void* DynamicLibraryModule::GetProcAddress(const std::string& name)
{
if (m_Module != NULL)
{
#ifdef _WIN32
return ::GetProcAddress(m_Module, name.c_str());
#else
return dlsym(m_Module, name.c_str());
#endif
}
else
{
return NULL;
}
}