dll

1.cl创建并调用dll
//testdll.cpp
_declspec(dllexport)  double add(double da ,double db)

{

  
return da+db;

}

_declspec(dllexport)  
double subtract(double da , double db)

{

 

   
return da-db;

}
//main.cpp
main

打开命令提示符,输入以下命令

cl     /c    testdll.cpp       //生成testdll.obj

link  /dll  testdll.obj         //生成testdll.lib  testdll.exp  testdll.dll

cl  /c  main.cpp            //生成main.obj

link  main.obj  testdll.lib    //生成main.exe

执行main.exe

结果:

testdll

3.2+6.5=9.7

6.8-2.6=4.2
2.使用Def文件(模块定义文件)导出dll中的函数
设文件名为
MathLib.def
  LIBRARY             "MathLib.dll"  
  DESCRIPTION     'MathLib'  
   
  EXPORTS  
          
  add   @1  
  subtract   @2 
testdll.cpp改写成如下样子:
double add(double da ,double db)
{
  return da+db;
}

double subtract(double da , double db)
{
   return da-db;
}

打开命令提示符,输入以下命令

cl     /c    testdll.cpp       //生成testdll.obj

link /DEF:MathLib.def /dll testdll.obj         //生成testdll.lib  testdll.exp  testdll.dll

cl  /c  main.cpp            //生成main.obj

link  main.obj  testdll.lib    //生成main.exe

用Dependency Walker查看动态库MathLib.dll.导出的函数,为add 和 subtract//(它的导入库名称仍为testdll.lib)
用vc自带工具dumpbin查看testdll.lib导出的函数为
?add@@YANNN@Z (double __cdecl add(double,double))
 ?subtract@@YANNN@Z (double __cdecl subtract(double,double))
和1例的一样,有些奇怪.
3.调用约定对导出函数的影响
调用约定
4.显示调用DLL中的函数
显示调用


posted @ 2009-08-27 09:14  thinkpore  阅读(176)  评论(0)    收藏  举报