动态和静态调用DLL
2012-02-09 20:46
197 查看
写一个简单的动态库,动态库中有一个求和函数,函数返回求和结果
DLL的工程单元代码:
DLL中的写了函数体的单元Unit1代码:
---------------------------------------
以下为调用该动态库
新建Form1, 点击按钮则调用DLL计算Edit1和Edit2的和,并将结果赋值给Edit3
DLL的工程单元代码:
library wudll; uses SysUtils, Classes, Unit1 in 'Unit1.pas'; {$R *.RES} exports //用exports 声明函数出口 Wuadd; begin end.
DLL中的写了函数体的单元Unit1代码:
unit Unit1; interface function Wuadd(anum1,Anum2:Integer):Integer;stdcall; //函数声明 //Delphi 默认采用 register 调用约定,如果dll涉及跨语言调用,最好明确指出采用 stdcall 调用约定 implementation function Wuadd(Anum1,Anum2:Integer):Integer;stdcall; //函数体 begin Result:=0; Result:=Anum1+Anum2; end; end.
---------------------------------------
以下为调用该动态库
新建Form1, 点击按钮则调用DLL计算Edit1和Edit2的和,并将结果赋值给Edit3
unit Unit1; interface uses ShareMem,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; //ShareMem 如果dll涉及长字符串的参数或变量,需要在 uses 第一的位置引用 ShareMem 单元。 type Twuabc=function (num1,num2:Integer):Integer;stdcall; //动态调用声明 必须写在type下 TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Button1: TButton; Edit3: TEdit; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function Wuadd(Anum1,Anum2:Integer):Integer;stdcall;external'd:\wudll.dll'; //静态调用DLL声明,必须写在type外面或implementation下面,这里的函数名必须和DLL中的函数名相同,大小写敏感 var Form1: TForm1; implementation {$R *.DFM} //function Wuadd(Anum1,Anum2:Integer):Integer;stdcall;external'd:\wudll.dll'; procedure TForm1.Button1Click(Sender: TObject);//动态调用DLL var wuhandle:THandle; wufunc:Twuabc; str1:Integer; begin wuhandle:=LoadLibrary('d:/wudll.dll'); //加载 DLL 到内存 if wuhandle<>0 then begin try @wufunc:=GetProcAddress(wuhandle,'Wuadd'); //获得函数的入口地址 if @wufunc<>nil then begin str1:=wufunc(StrToInt(Edit1.Text),StrToInt(Edit2.Text)); //调用DLL中的函数 Edit3.Text:=IntToStr(str1); end; finally FreeLibrary(wuhandle); end; end; end; procedure TForm1.Button2Click(Sender: TObject);//静态调用DLL var str1:Integer; begin str1:=Wuadd(StrToInt(Edit1.Text),StrToInt(Edit2.Text)); //调用DLL中的函数 Edit3.Text:=IntToStr(str1); end; end.
相关文章推荐
- DLL专题之动态连接库的静态调用(1)
- DLL静态(显示)调用,动态(隐式)调用
- vb.net静态动态调用c++dll的方法
- Delphi 动态与静态调用DLL
- 在C++中调用DLL中的函数——静态调用和动态调用
- C# 中静态调用C++dll 和C# 中动态调用C++dll
- DLL专题之动态连接库的静态调用(2)
- DLL DEF文件编写方法 VC++ 调用、调试DLL的方法 显式(静态)调用、隐式(动态)调用
- Delphi 动态与静态调用DLL
- C++调用DLL有两种方法——静态调用和动态调用
- .h .lib .dll文件,静态链接库与动态链接库,静态调用与动态调用
- C++ 编写DLL 动态及静态调用DLL方法
- C# 中静态调用C++dll 和C# 中动态调用C++dll
- Delphi 类库(DLL)动态调用与静态调用示例讲解
- dll 调用,动态,静态
- VC中使用动态链接库DLL:静态调用和动态调用
- dll的静态调用、动态调用
- 【VB技巧】VB静态调用与动态调用dll详解
- dll的静态调用和动态调用使用说明及简单实例
- Delphi编写DLL(以及静态和动态方式调用)