//dll 动态加载 调用 类 函数小结:
静态加载时,调用类成员函数,很简单。此次研究了下动态加载。
首先困难点:
The first problem is that C++ member function names are decorated names (Specifying extern "C" does not help).
The second problem is that C++ language specifications do not allow pointer to member functions to be converted into other types
1. .def 中, DCls2_f = ?f@DCls2@@QAEXXZ
exe中, DCls2_f* c1 = (DCls2_f*)malloc(sizeof(DCls2_f));
方法1:
__asm { MOV ECX, c1 }//vs push this FUNC f = GetProcAddress()... f();//由于已放好this,可直接调用
方法2:
typedef void (DCls2::*MemFunc)(); typedef void (*GlbFunc)(); union uni { MemFunc mf; GlbFunc gf; } u1; u1.gf = GetProcAddress()... MemFunc mf = u1.mf; (c1->*mf)();
2. 继承接口基类,封装虚函数接口,返回指针调用,即可解决编译链接时错误,再运行时动态绑定虚函数(只要是虚函数就可以用于dll导出了,
就不会因为没有实现而出现链接错误,因为是(this->vtbl[1])()来调用,而不是通过函数符号DCls2::f;
继承基类接口,只是更好的风格,为了封装扩展)
dll:
class IBase { virtual void f(); };//also could pure virtual class DCls2 : public IBase { virtual void f(); }; IBase* createIns();//impl: return (IBase*)new DCls2; //will dync bind virtual func addr, //i.g: this->vtbl = _DCls2_vtbl (而_DCls2_vtbl[1] = DCls2::f)
exe:
IBase* c1 = createIns(); c1->f();//will not link err, because f is virtual 对于虚函数,无实现不会链接出错
3. 将class 包装成对外的C接口吧,做一个wrapper,我觉得还是这样比较好,不会有DLL Hell的问题,毕竟DLL本来就是为C函数服务设计的。
本文出自 “v” 博客,请务必保留此出处http://4651077.blog.51cto.com/4641077/1613808
原文地址:http://4651077.blog.51cto.com/4641077/1613808