一、VC程序调用VC编写的导出函数
a)定义
1)创建dll工程

2)在工程下添加源程序文件,并编写代码

源代码如下:
1 _declspec(dllexport) int IntPlus(int a,int b)
2 {
3 return a+b;
4 }
编译运行生成dll

b)调用
1)创建普通的控制台工程并将要使用dll和lib文件拷贝至工程目录下

2)工程设置中,生命要使用的lib文件

3)编写源代码
1 #include<iostream>
2 using namespace std;
3
4 extern int IntPlus(int a,int b);
5
6 int main()
7 {
8 int sum=0;
9 sum=IntPlus(10,200);
10 cout<<"调用全局的DLL结果: "<<sum<<endl;
11 return 0;
12 }
4)运行结果:

二、VC程序调用VC编写的导出类
a)定义
1)创建DLL工程
步骤同(一)相似,是普通的DLL而不是MFC DLL
2)编写代码

头文件代码如下:
1 class _declspec(dllexport) Computation
2 {
3 public:
4 int IntPlus(int a,int b);
5 };
类的实现代码如下:
1 #include "Computation.h";
2
3 int Computation::IntPlus(int a,int b)
4 {
5 return a+b;
6 }
b)调用
1)创建普通的控制台工程,并将要引用的文件拷至对应目录下
头文件和lib文件放于工程目录下:

要调用的DLL文件至于Debug目录下:

2)将目录下的Computation.h添加到工程文件中

3)编写主程序代码
1 #include<iostream>
2 #include "Computation.h"
3 #pragma comment(lib,"ExportClassForVC.lib")
4 using namespace std;
5
6 int main()
7 {
8 Computation compute;
9 int sum=100;
10 sum=compute.IntPlus(10,200);
11 cout<<"调用导出类DLL结果: "<<sum<<endl;
12 return 0;
13 }
4)编译运行结果如下:

三、Delphi程序调用VC编写的导出函数
a)定义
基本同(一)中的方式相同,但是代码中需要标识:extern "C"
主要代码如下:
1 extern "C" _declspec(dllexport) int IntPlus(int a,int b)
2 {
3 return a+b;
4 }
注:1)之所以要声明extern "C"就是为了解决不同语言调用时函数名改编的问题
2)只能用于全局函数,不能用于类的成员函数
b)调用
在Delphi中调用DLL十分方便,只需要将DLL文件直接拷贝至工程目录下即可(无需拷贝lib文件)

1)主要代码如下:
1 unit Main;
2
3 interface
4 uses
5 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
6 Dialogs, StdCtrls;
7
8 type
9 TForm1 = class(TForm)
10 btn1: TButton;
11 procedure btn1Click(Sender: TObject);
12 private
13 { Private declarations }
14 public
15 { Public declarations }
16 end;
17
18 function IntPlus(a:Integer; b:Integer):Integer;cdecl;external 'GlobalFuncForDelphi.dll';
19
20 var
21 Form1: TForm1;
22
23 implementation
24 {$R *.dfm}
25 procedure TForm1.btn1Click(Sender: TObject);
26 var
27 sum:integer;
28 begin
29 sum:=IntPlus(100,8);
30 showmessage('调用DLL中导出函数的结果为:'+inttostr(sum));
31 end;
32
33 end.
注:由于C中默认的调用方式是cdecl方式,所以在该delphi程序中也要以cdecl方式调用
2)编译运行测试结果如下:

四、Delphi程序调用VC编写的导出类
说明:Delphi程序不能直接调用VC编写的导出类,故需要用VC再编写一个导出函数进行桥接;
Delphi通过调用该导出函数来访为前一个dll中的导出类
a)定义用于桥接的DLL
1)将(三)中VC导出类的Computation.h和lib文件拷贝至桥接程序工程目录下

2)将要调用的DLL文件拷贝至工程的Debug目录下

3)编写主程序
1 #include "Computation.h"
2 #pragma comment(lib,"ExportClassForVC.lib")
3
4 extern "C" _declspec(dllexport) int IntPlusBridge(int a,int b)
5 {
6 Computation compute;
7 int sum=100;
8 sum=compute.IntPlus(a,b);
9 return sum;
10 }
4)编译生成用于桥接的DLL

附:实际上对于引用的Computation.h头文件,并不需要手动加入到Header Files中;项目中有一句“include XXX.h”,而该头文件不是在项目中定义的,则该文件会被认为是外部依赖的,会被自动添加到External Dependencies目录下

b)调用
1)编写主程序
1 unit Main;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 btn1: TButton;
12 procedure btn1Click(Sender: TObject);
13 private
14 { Private declarations }
15 public
16 { Public declarations }
17 end;
18
19 function IntPlusBridge(a:Integer; b:Integer):Integer;cdecl;external 'ExportClassBridgeForDelphi.dll';
20
21 var
22 Form1: TForm1;
23
24 implementation
25 {$R *.dfm}
26 procedure TForm1.btn1Click(Sender: TObject);
27 var
28 sum:integer;
29 begin
30 sum:=IntPlusBridge(20,800);
31 showmessage('调用导出类函数的执行结果为:'+inttostr(sum));
32 end;
33
34 end.
2)将导出类DLL和桥接DLL添加到工程目录下

3)编译运行执行结果如下:

作者:Gezidan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2011-08-07 19:18
日需博客 阅读(692)
评论(0) 编辑 收藏 引用 所属分类:
C C++ 、
技术文章 、
转载