我的项目 – Windows/Linux动态库加载类
原创文章,作者:StubbornHuang,如若转载,请注明出处:《我的项目 – Windows/Linux动态库加载类》https://www.stubbornhuang.com/807/
1 Windows/Linux的动态库加载
该类用于在Windows/Linux加载动态库,并使用动态库,具体功能如下:
- 加载动态库
- 卸载动态库
- 获取动态库中接口函数指针
- 得到错误信息
- 得到动态库加载状态
Github地址 : https://github.com/HW140701/DynamicModuleLoader
2 代码
2.1 DynamicModuleLoader.h
头文件代码如下:
//!
//! @brief - Load dynamic libraries on Windows and Linux platforms
//!
//! @author - HuangWang
//!
//! @data - 2019-09-26
//!
#ifndef _DYNAMIC_MODULE_LOADER_H_
#define _DYNAMIC_MODULE_LOADER_H_
#include <string>
#include <fstream>
#define _DYNAMIC_LOAD
#define WINDOWS
#if defined(_DYNAMIC_LOAD)
#if defined(WINDOWS)
#include <Windows.h>
#define MODULE_HANDLER HINSTANCE
#elif defined(LINUX)
#include <dlfcn.h>
#define MODULE_HANDLER void*
#endif
#endif
#ifdef WINDOWS
#define PLATFORM_PATH_SPLIT_CHAR "\"
#define PLATFORM_DYNAMIC_LIBRARY_PRE ""
#define PLATFORM_DYNAMIC_LIBRARY_EXT ".dll"
#elif defined (LINUX)
#define PLATFORM_PATH_SPLIT_CHAR "/"
#define PLATFORM_DYNAMIC_LIBRARY_PRE "lib"
#define PLATFORM_DYNAMIC_LIBRARY_EXT ".so"
#endif // WINDOWS
namespace DynamicModuleLoaderSpace
{
enum DynamicModuleState
{
DMS_UnLoaded = 0,
DMS_Loaded = 1
};
class DynamicModuleLoader
{
public:
DynamicModuleLoader();
virtual ~DynamicModuleLoader();
static bool IsFileExist(const std::string filePath);
bool LoadDynamicModule(const std::string dynamicModulePath);
void* GetFunction(const std::string functionName);
bool UnloadDynamicModule();
std::string GetErrorMessage();
bool GetDynamicModuleState();
private:
void GetInternalErrorMessge(int errorCode);
private:
MODULE_HANDLER m_DynamicModulePtr;
std::string m_ErrorMessage;
DynamicModuleState m_DynamicModuleState;
};
}
#endif /// _DYNAMIC_MODULE_LOADER_H_
cpp代码如下:
#include "DynamicModuleLoader.h"
namespace DynamicModuleLoaderSpace
{
DynamicModuleLoader::DynamicModuleLoader() :m_DynamicModulePtr(NULL), m_ErrorMessage(""), m_DynamicModuleState(DynamicModuleState::DMS_UnLoaded)
{
}
DynamicModuleLoader::~DynamicModuleLoader()
{
}
bool DynamicModuleLoader::IsFileExist(const std::string filePath)
{
std::fstream file;
file.open(filePath, std::ios::in);
if (file)
{
file.close();
return true;
}
else
{
return false;
}
}
bool DynamicModuleLoader::LoadDynamicModule(const std::string dynamicModulePath)
{
if (IsFileExist(dynamicModulePath))
{
#ifdef WINDOWS
m_DynamicModulePtr = LoadLibrary(dynamicModulePath.c_str());
#elif LINUX
m_DynamicModulePtr = dlopen(dynamicModulePath.c_str(), RTLD_NOW | RTLD_GLOBAL);
#endif // WINDOWS
if (m_DynamicModulePtr != NULL)
{
m_DynamicModuleState = DynamicModuleState::DMS_Loaded;
return true;
}
else
{
#ifdef WINDOWS
int errorCode = GetLastError();
#endif
GetInternalErrorMessge(errorCode);
return false;
}
}
return false;
}
void* DynamicModuleLoader::GetFunction(const std::string functionName)
{
if (m_DynamicModulePtr)
{
void* tempFunctionPtr = NULL;
#ifdef WINDOWS
tempFunctionPtr = GetProcAddress(m_DynamicModulePtr, functionName.c_str());
#elif LINUX
tempFunctionPtr = dlsym(m_DynamicModulePtr, functionName.c_str());
#endif // WINDOWS
if (tempFunctionPtr != NULL)
{
return tempFunctionPtr;
}
else
{
#ifdef WINDOWS
int errorCode = GetLastError();
#endif
GetInternalErrorMessge(errorCode);
}
}
return NULL;
}
bool DynamicModuleLoader::UnloadDynamicModule()
{
if (m_DynamicModulePtr)
{
#ifdef WINDOWS
if (FreeLibrary(m_DynamicModulePtr) == 0)
{
int errorCode = GetLastError(); // 得到错误代码
GetInternalErrorMessge(errorCode);
return false;
}
#elif LINUX
dlclose(m_DynamicModulePtr);
#endif
m_DynamicModuleState = DynamicModuleState::DMS_UnLoaded;
return true;
}
return false;
}
#ifdef WINDOWS
#endif
void DynamicModuleLoader::GetInternalErrorMessge(int errorCode)
{
std::string errorMessge = "";
#ifdef WINDOWS
LPVOID lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL
))
{
errorMessge = (LPCTSTR)(lpMsgBuf);
LocalFree(lpMsgBuf);
}
#elif LINUX
errorMessge = dlerror();
#endif // WINDOWS
m_ErrorMessage = errorMessge;
}
std::string DynamicModuleLoader::GetErrorMessage()
{
return m_ErrorMessage;
}
bool DynamicModuleLoader::GetDynamicModuleState()
{
return (bool)m_DynamicModuleState;
}
}
当前分类随机文章推荐
- 我的开源项目 - 支持C++11特性的定时器TinyTimer
- 我的开源项目 - 各种搜索引擎收录查询接口(Google/百度/必应/360/搜狗......)
- 我的开源项目 - 3DPoseEstimation从2D视频中估计人物三维姿势,并生成BVH文件
- 我的项目 - Windows/Linux动态库加载类
全站随机文章推荐
- C++11/std::thread - 线程管理join/detach
- 资源分享 - Real-Time Rendering, Third Edition 英文原版Pdf下载
- WordPress - 插件OSS Upload与WP Editor.md/WP Githuber MD插件冲突,导致katex公式不正确显示的问题
- Centos7 - 配置Go环境
- Modern OpenGL - GLSL着色语言3:GLSL中的数据类型
- 盗墓笔记到底讲了什么故事???-带你领会终极和它
- Python3爬虫 - requests库的requests.exceptions所有异常详细说明
- OpenCV - 打开摄像头并用窗口显示摄像头的内容
- 资源分享 - 图解机器学习(日 杉山将著 许永伟译)PDF下载
- 常见的三维点云数据下载链接,自己整理+网上收集