您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 开发专栏 > DELPHI开发 > 正文
·速成电脑精英(包分配)白领高薪一族从这里开始



-Java套接字编程(下)
-MediaStudio Pro 6.5教程
-三款卸载软件最新试用
-基于Visual C++的Winsock API研究

在Delphi和VC中创建和调用动态链接库
2001-10-31· ·丛蓉 李恺··yesky

上一页  1 2 3 4  


  六、实例

  因为用Delphi创建和调用DLL,过程与用VC创建和调用DLL相似,只是前者代码要比在VC环境中稍微复杂一些,因此在文章的最后给出一个完整的用Delphi创建和调用DLL的例子。该DLL主要用来检查输入的口令是否正确,窗体含有一个Edit编辑框,一个按钮Button,在编辑框内输入口令,比较编辑框的值和输入参数返回真假值。

//文件名为checkpassword.dpr;编译此文件生成checkpassword.dll
library checkpassword;
uses
SysUtils, Classes, Unit1 in 'Unit1.pas' {Form1};
exports
checkpwd name 'checkpwd';//声明DLL函数
{$R *.RES}
begin
end.

file://文件名为unit1.pas
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
password: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
flag:boolean;//返回输入密码是否正确
rightpwd:string;//记录输入参数(正确的密码)
function checkpwd(pwd:string):boolean;export;//声明DLL函数
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if password.text=rightpwd then
flag:=true;
form1.close;
end;

function checkpwd(pwd:string):boolean;export;
begin
flag:=false;
rightpwd:=pwd;//读入正确的密码值
form1:=Tform1.create(Application);//创建密码验证窗口
form1.ShowModal;//显示窗口
checkpwd:=flag;
form1.free;//释放资源
end;
end.

file://主程序main.pas,调用DLL
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
jieguo: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
 public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function checkpwd(pwd:string):boolean;external 'project1.dll' ;//函数说明
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if checkpwd('congrong') then//调用函数
jieguo.text:='true'
else
jieguo.text:='false';
end;
end.


  以上程序在Windows98/Delphi5下编译通过。


上一页  1 2 3 4  

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 Delphi中多媒体组件使用解析
 用Delphi制作动态菜单
 DELPHI中自适应表单的实现
 Delphi中利用MSCOMM控件进行GPS数据采集
 Delphi中带缓存的数据更新技术
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!