掌舵老人工作室

给我一片天空 我还您一个世界

delphi 编写DLL与实际调用小案例

本文简单介绍了一下在delphi 7中对dll文件得调用和dll文件得生成!
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Label4: TLabel;
    Label5: TLabel;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
function Max(a,b:integer):integer;stdcall;External'project2.dll';
function Min(a,b:integer):integer;stdcall;External'project2.dll';

{上面是调用dll中得函数}
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
  edit1.Text:='';
  edit2.Text:='';
  edit3.Text:='';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
  close;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  a,b:integer;
begin
   label3.Caption:='MAX';
   if (edit1.Text='') and (edit2.Text='') then
     edit3.text:='请检查你得输入,输入不能为空!'
   else
     begin
       a:=strtoint(edit1.Text);
       b:=strtoint(edit2.Text);
       edit3.Text:=inttostr(Max(a,b));
     end;
end;
{上面是调用dll文件中得Max函数}
procedure TForm1.Button4Click(Sender: TObject);
var
  a,b:integer;
begin
   label3.Caption:='MIN';
   if (edit1.Text='') and (edit2.Text='') then
     edit3.text:='请检查你得输入,输入不能为空!'
   else
     begin
       a:=strtoint(edit1.Text);
       b:=strtoint(edit2.Text);
       edit3.Text:=inttostr(Min(a,b));
     end;
end;
{上面是调用dll文件中得Min函数}
end.
{上面是主窗体中得定义}
{下面完成对dll文件得申明和定义}
library Project2;
uses
  SysUtils,
  Classes;
{$R *.res}
function Max(a,b:integer):integer;stdcall;
begin
  Max:=a;
  if b>a then
    Max:=b;
end;
exports
Max;
function Min(a,b:integer):integer;stdcall;
begin
  Min:=a;
  if b<a then
    Min:=b;
end;
exports
Min;
begin
end.

阅读更多
个人分类: Delphi
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭