利用C++11可变模板,封装调用dll导出函数

起因

开发中经常需要动态调用一些导出函数,试着利用C++11特性封装一下

尝试

常规使用

typedef int WINAPI (*TMessageBoxA)(HWND hWnd,LPCSTR lpText,LPCSTR lpCaption,UINT uType);
if (HMODULE m = LoadLibraryA("user32")) {
	if (FARPROC proc = GetProcAddress(m,"MessageBoxA")) {
		TMessageBoxA msgBox = reinterpret_cast<TMessageBoxA>(fn);
		msgBox(0,0,0,0);	// call it ...
	}
	FreeLibrary(m);
}

流程很清晰,只是写多了看着不爽 😉
曾经利用 decltype function bind 实现过另个版本,这次尝试更纯粹一些

解决

参考std::function实现方式,仅使用"Variadic Templates"

#include <windows.h>

template<typename _T> class Api;
template<typename _Res, typename... _ArgTypes>
class Api<_Res(_ArgTypes...)> {
public:
	Api(const char* dllName, const char* funcName) {
		_M_module = LoadLibraryA(dllName);
		_M_func = reinterpret_cast<_Func>(GetProcAddress(_M_module, funcName));
	}
	~Api() {
		if (_M_module) FreeLibrary(_M_module);
	}
	_Res operator()(_ArgTypes... __args) const {
		return _M_func(__args...);
	} 
private:
	typedef _Res (*_Func)(_ArgTypes...);
	_Func _M_func;
	HMODULE _M_module;
};

int main()
{
	Api<int(HWND,LPCTSTR,LPCTSTR,UINT)> msgbox("user32", "MessageBoxA");
	msgbox(0,0,0,0);
} 

注意,为了最小利用特性,这里没有利用final、delete进行使用限制,也没有对HMODULE进行缓存管理,生产环境请自行抉择。
有更好的方式,请留言交流;-)

还是附上另一种更常用的方法

#include <map>
#include <string>
#include <functional>
#include <windows.h>


class WinDll {
public:
	WinDll(const char* dll) {
		mMoudle = LoadLibraryA(dll);
	}
	WinDll(const wchar_t* dll) {
		mMoudle = LoadLibraryW(dll);
	}
	~WinDll() {
		if (mMoudle) FreeLibrary(mMoudle);
	}
	
	FARPROC RawApi(const std::string& fn) {
		auto it = mFuncCaches.find(fn);
		if (it == mFuncCaches.end()) {
			FARPROC func = GetProcAddress(mMoudle, fn.c_str());
			if (!func) throw "not found!";
			mFuncCaches[fn] = func;
			return func;
		} 
		return it->second;
	}
	
	template<typename _T>
	std::function<_T> ApiFunc(const std::string& fn) {
		return std::function<_T>(reinterpret_cast<_T*>(RawApi(fn)));
	}
	
private:
	HMODULE mMoudle;
	std::map<std::string,FARPROC> mFuncCaches;
};

int main()
{
	WinDll user32("user32");
	using TMessageBoxA = int(HWND,LPCTSTR,LPCTSTR,UINT);
	auto msgBox = user32.ApiFunc<TMessageBoxA>("MessageBoxA");
	msgBox(0,0,0,0);
} 

原文链接: https://www.cnblogs.com/wuyaSama/p/11510889.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    利用C++11可变模板,封装调用dll导出函数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/302550

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午11:26
下一篇 2023年2月15日 下午11:26

相关推荐