【转自】http://www.cppblog.com/codejie/archive/2009/09/24/97141.html

    使用LoadLibrary函数调用DLL中的函数的方法一般被称为“显式”调用,意义和使用lib的“隐式”调用相对应。 

    LoadLibrary调用DLL中的函数的方法比较简单,通过GetProcAddress获得函数的在DLL的地址就可以访问了,但DLL中的Class访问就相对很复杂了(目前我就发现这一种显式调用方式,哪位有其他方法么?)。一个简单的情况就是Class的函数在调用是,其名称是什么?还有Class的contructor函数怎么调用?下面的代码将演示下这些问题。 
这里是DLL的文件: 
    DllMain.h 

 1【转载】LoadLibrary调用DLL中的Class#ifndef __DLLMAIN_H__
 2【转载】LoadLibrary调用DLL中的Class#define __DLLMAIN_H__
 3【转载】LoadLibrary调用DLL中的Class
 4【转载】LoadLibrary调用DLL中的Class#include <string>
 5【转载】LoadLibrary调用DLL中的Class
 6【转载】LoadLibrary调用DLL中的Class#define DllExport __declspec(dllexport)
 7【转载】LoadLibrary调用DLL中的Class
 8【转载】LoadLibrary调用DLL中的Classextern "C" int DllExport Func(int x);
 9【转载】LoadLibrary调用DLL中的Class
10【转载】LoadLibrary调用DLL中的Classextern "C" class DllExport CA
11#endif

    DllMain.cpp 
 1【转载】LoadLibrary调用DLL中的Class#include <iostream>
 2【转载】LoadLibrary调用DLL中的Class
 3【转载】LoadLibrary调用DLL中的Class#include "DllMain.h"
 4【转载】LoadLibrary调用DLL中的Class
 5【转载】LoadLibrary调用DLL中的Classint Func(int x)
 6

    这里需要.def文件了,因为Class在DLL中的命名不像函数命名那么简单,会被转义的,像CA::Func(int)在DLL的export表中就是?Func@CA@@QAEHH@Z,具体定义说明可参看《xxx的自我修养》一书。因此,这里需要使用.def文件对函数进行重命名,下面是DllMain.def文件内容:
 
1【转载】LoadLibrary调用DLL中的ClassLIBRARY TESTDLL
2【转载】LoadLibrary调用DLL中的ClassEXPORTS
3【转载】LoadLibrary调用DLL中的Class    Func = Func
4【转载】LoadLibrary调用DLL中的Class    CA::CA(int= ??0CA@@QAE@H@Z
5【转载】LoadLibrary调用DLL中的Class    CA::~CA = ??1CA@@QAE@XZ
6【转载】LoadLibrary调用DLL中的Class    CA::Func0 = ?Func0@CA@@QAEHXZ
7【转载】LoadLibrary调用DLL中的Class    CA::Func(int= ?Func@CA@@QAEHH@Z
8【转载】LoadLibrary调用DLL中的Class    ;CA::FuncS(int,std::basic_string<char>&= ?FuncS@CA@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HABV23@@Z
9【转载】LoadLibrary调用DLL中的Class    CA::FuncS = ?FuncS@CA@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HABV23@@Z


    多说一句,这里.def的编写很需要Depends(Dependency Walker)工具的支持,其是查看DLL的首选工具啊。。 
【转载】LoadLibrary调用DLL中的Class

    编译DLL,用下面代码进行测试: 
    LoadLib.cpp 
 1【转载】LoadLibrary调用DLL中的Class#include <iostream>
 2【转载】LoadLibrary调用DLL中的Class#include <string>
 3【转载】LoadLibrary调用DLL中的Class
 4【转载】LoadLibrary调用DLL中的Class#include <windows.h>
 5【转载】LoadLibrary调用DLL中的Class
 6【转载】LoadLibrary调用DLL中的Class//#include "DllMain.h"
 7【转载】LoadLibrary调用DLL中的Class
 8【转载】LoadLibrary调用DLL中的Class#define DllExport __declspec(dllexport)
 9【转载】LoadLibrary调用DLL中的Class
10【转载】LoadLibrary调用DLL中的Classextern "C" int DllExport Func(int x);
11【转载】LoadLibrary调用DLL中的Class
12【转载】LoadLibrary调用DLL中的Classextern "C" class DllExport CA
13}


    结果还算正常: 
1【转载】LoadLibrary调用DLL中的Class100
2【转载】LoadLibrary调用DLL中的Classcontructor
3【转载】LoadLibrary调用DLL中的Class5
4【转载】LoadLibrary调用DLL中的Class50
5【转载】LoadLibrary调用DLL中的Classhello world
6【转载】LoadLibrary调用DLL中的Classdestructor
7【转载】LoadLibrary调用DLL中的Class


    上面的代码基本演示了DLL中Class的简单使用,包括对contructor、destrunctor的调用,有参、无参、多参函数调用,不知道有啥缺陷,但至少Work了,嘿嘿~ 
    由上述代码可以看出,这种“显式”使用DLL中的Class是非常繁琐和危险的事情,因此我觉得能用“隐式”就不要用“显式”,能静态就不要用动态。。。 
    注意到没,代码没有演示继承和虚函数,那是因此我加入Virtual函数,程序就会core,实在搞不定,这里也就没法给出好的方案来,不知道哪位有啥建议么。。。
    上面代码参考了如下地址: 
     http://www.codeproject.com/dll/classesexportedusingLL.asp 
     http://blog.csdn.net/jdcb2001/archive/2006/11/21/1401569.aspx

相关文章:

  • 2021-10-28
  • 2021-05-21
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
相关资源
相似解决方案