加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++11 DllParser

发布时间:2020-12-15 04:48:23 所属栏目:百科 来源:网络整理
导读:#include "pch.h" #include #include #include #include #include using namespace std; class DllParser { public: DllParser():m_hMod(nullptr) { } ~DllParser() { UnLoad(); } bool Load(const std::string dllPath) { m_hMod = LoadLibraryA(dllPath.da

#include "pch.h"

#include

#include

#include

#include

#include

using namespace std;

class DllParser

{

public:

DllParser():m_hMod(nullptr)

{

}

~DllParser()

{

UnLoad();

}

bool Load(const std::string & dllPath)

{

m_hMod = LoadLibraryA(dllPath.data());

if (nullptr == m_hMod)

{

cout << "LoadLibrary failed" << endl;

}

return true;

}

bool UnLoad()

{

if (nullptr == m_hMod)

return true;

auto b = FreeLibrary(m_hMod);

if (!b)

return false;

m_hMod = nullptr;

return true;

}

template

function GetFunction(const string& funcName)

{

auto it = m_map.find(funcName);

if (it == m_map.end())

{

auto addr = GetProcAddress(m_hMod,funcName.c_str());

if (!addr)

return nullptr;

m_map.insert(make_pair(funcName,addr));

it = m_map.find(funcName);

}

return function((T*)(it->second));

}

template

typename result_of(Args...)>::type ExcecuteFunc(string& funcName,Args&&... args)

{

auto f = GetFunction(funcName);

if (nullptr==f)

{

string s = "can not find this function "+funcName;

throw exception(s.c_str());

}

return f(forward(args)...);

}

private:

HMODULE m_hMod;

map m_map;

};

int main()

{

/*

类似于如下调用

auto max = ExcecuteFunc("Max",5,8);

auto ret = ExecuteFunc<"Get",5);

*/

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读