Software Testing and Quality Management

delphi下DLL创建及调用-阿祖总结

上一篇 / 下一篇  2007-10-18 10:06:56 / 个人分类:Dephi

有两种方法去创建DLL
1.常规手段去创建File->New->Other,在New选项卡下选择DLL Wizard,工程文件如下:
library Project1;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes;

{$R *.res}

begin
end.
2.还可以将Application下的工程文件的program更名为library,并删除单元文件,删除工程文件下的begin也end之间的代码,并在begin之前加上EXPORT
---------------------------------------------------------------------------
连库的详细说明:
1.创建DLL

本例是通过Dll访问数据库,在数据库里调用一个函数并且返回想要的数据。
按第一种方法创建DLL模型后,在此基础之上再建立一个窗体文件(为的是连库用),在创建Form窗体上添加TSQLConnection及TSimpleDataSet两个组件,将TSQLConnection里的连库参数添加好,再将TSimpleDataSet里Connection属性改为在TSQLConnection组件的名称即可,这样就准备好写代码前题了!
在DLL的单元的{$R *.res}下声明我们要定义的函数,代码如下:

library mydll;

uses
  SysUtils,
  Classes,
  FOrms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

function ENCRYPT(vFlag:String):pchar;export;//定义的函数
var
  v:string;

begin

  if form1=nil then
     form1:=TForm1.Create(application);//Dll在加载时不能自动创建窗体文件,就是刚才咱们之前手动创建的窗体文件!得写一小段代码才行!
  form1.SQLConnection1.Open;
  form1.SimpleDataSet1.Close;
  form1.SimpleDataSet1.DataSet.CommandText:='select RAWTOHEX(TSANGHAOCRYPTO.ENCRYPT(TO_CHAR('''+vFlag+'''))) from dual';//调用数据库的函数;
  form1.SimpleDataSet1.Open;
  v:=form1.SimpleDataSet1.Fields[0].AsString;//将查询的数据给变量V;
  form1.Close;
  form1:=nil;//将窗体资源释放
  ENCRYPT:=pchar(v);

end;
exports ENCRYPT;
begin
end.
在刚才创建的单元文件里写如下代码,
procedure TForm1.FormCreate(Sender: TObject);//连库
begin
  SQLConnection1.Open;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);//释放资源
begin
  Action :=cafree
end;
---------------------------------------------------------------------------
2.编译DLL
用CTRL+F9即可创建DLL文件了!
---------------------------------------------------------------------------
3.调用DLL
有了DLL调用就不难了,这样操作:打开Delphi后,创建一个Application,在窗体Form上添加两个Edit、一个Button组件后,在单元文件下书写如下代码即可:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
    TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
function ENCRYPT(vFlag:String):pchar;external 'mydll.dll';//声明函数
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  edit2.Text:=strpas(ENCRYPT(edit1.Text));//调用函数
end;

end.
---------------------------------------------------------------------------


TAG: Dephi

 

评分:0

我来说两句

日历

« 2024-05-01  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 13891
  • 日志数: 12
  • 建立时间: 2007-04-05
  • 更新时间: 2007-10-18

RSS订阅

Open Toolbar