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代码如下: