Add plugin support
This commit is contained in:
114
src/internal/dl.cc
Normal file
114
src/internal/dl.cc
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "internal/dl.h"
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
MYNTEYE_BEGIN_NAMESPACE
|
||||
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
|
||||
namespace {
|
||||
|
||||
// How to get the error message from the error code returned by GetLastError()?
|
||||
// https://stackoverflow.com/questions/9272415/how-to-convert-dword-to-char
|
||||
std::string GetLastErrorAsString() {
|
||||
DWORD dw = ::GetLastError();
|
||||
if (dw == 0)
|
||||
return std::string();
|
||||
LPSTR lpMsgBuf;
|
||||
size_t size = FormatMessageA(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0,
|
||||
NULL);
|
||||
std::string message(lpMsgBuf, size);
|
||||
LocalFree(lpMsgBuf);
|
||||
return message;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
||||
DL::DL() : handle(nullptr) {
|
||||
VLOG(2) << __func__;
|
||||
}
|
||||
|
||||
DL::DL(const char *filename) : handle(nullptr) {
|
||||
VLOG(2) << __func__;
|
||||
Open(filename);
|
||||
}
|
||||
|
||||
DL::~DL() {
|
||||
VLOG(2) << __func__;
|
||||
Close();
|
||||
}
|
||||
|
||||
bool DL::Open(const char *filename) {
|
||||
if (handle != nullptr) {
|
||||
VLOG(2) << "Already opened, do nothing";
|
||||
// Close();
|
||||
return false;
|
||||
}
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
handle = LoadLibraryEx(filename, nullptr, 0);
|
||||
#else
|
||||
handle = dlopen(filename, RTLD_LAZY);
|
||||
#endif
|
||||
if (handle == nullptr) {
|
||||
VLOG(2) << "Open library failed: " << filename;
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool DL::IsOpened() {
|
||||
return handle != nullptr;
|
||||
}
|
||||
|
||||
void *DL::Sym(const char *symbol) {
|
||||
if (handle == nullptr) {
|
||||
VLOG(2) << "Not opened, do nothing";
|
||||
return nullptr;
|
||||
}
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
void *f = GetProcAddress(handle, symbol);
|
||||
if (f == nullptr) {
|
||||
VLOG(2) << "Load symbol failed: " << symbol;
|
||||
}
|
||||
#else
|
||||
dlerror(); // reset errors
|
||||
void *f = dlsym(handle, symbol);
|
||||
const char *error = dlerror();
|
||||
if (error != nullptr) {
|
||||
VLOG(2) << "Load symbol failed: " << symbol;
|
||||
f = nullptr;
|
||||
}
|
||||
#endif
|
||||
return f;
|
||||
}
|
||||
|
||||
int DL::Close() {
|
||||
int ret = 0;
|
||||
if (handle == nullptr) {
|
||||
VLOG(2) << "Not opened, do nothing";
|
||||
} else {
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
ret = FreeLibrary(handle) ? 0 : 1;
|
||||
#else
|
||||
ret = dlclose(handle);
|
||||
#endif
|
||||
handle = nullptr;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char *DL::Error() {
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
return GetLastErrorAsString().c_str();
|
||||
#else
|
||||
return dlerror();
|
||||
#endif
|
||||
}
|
||||
|
||||
MYNTEYE_END_NAMESPACE
|
||||
56
src/internal/dl.h
Normal file
56
src/internal/dl.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef MYNTEYE_INTERNAL_DL_H_ // NOLINT
|
||||
#define MYNTEYE_INTERNAL_DL_H_
|
||||
#pragma once
|
||||
|
||||
#include "mynteye/mynteye.h"
|
||||
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
MYNTEYE_BEGIN_NAMESPACE
|
||||
|
||||
#if defined(OS_WIN) && !defined(OS_MINGW) && !defined(OS_CYGWIN)
|
||||
using DLLIB = HMODULE;
|
||||
#else
|
||||
using DLLIB = void *;
|
||||
#endif
|
||||
|
||||
// Dynamic loading
|
||||
// https://en.wikipedia.org/wiki/Dynamic_loading
|
||||
// C++ dlopen mini HOWTO
|
||||
// http://tldp.org/HOWTO/C++-dlopen/
|
||||
class MYNTEYE_API DL {
|
||||
public:
|
||||
DL();
|
||||
explicit DL(const char *filename);
|
||||
~DL();
|
||||
|
||||
bool Open(const char *filename);
|
||||
|
||||
bool IsOpened();
|
||||
|
||||
void *Sym(const char *symbol);
|
||||
|
||||
template <typename Func>
|
||||
Func *Sym(const char *symbol);
|
||||
|
||||
int Close();
|
||||
|
||||
const char *Error();
|
||||
|
||||
private:
|
||||
DLLIB handle;
|
||||
};
|
||||
|
||||
template <typename Func>
|
||||
Func *DL::Sym(const char *symbol) {
|
||||
void *f = Sym(symbol);
|
||||
return reinterpret_cast<Func *>(f);
|
||||
}
|
||||
|
||||
MYNTEYE_END_NAMESPACE
|
||||
|
||||
#endif // MYNTEYE_INTERNAL_DL_H_ NOLINT
|
||||
Reference in New Issue
Block a user