程序中調用DLL實現回調函數

//程序中調用DLL實現
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

{$R *.dfm}

function AddTwo(x,y:integer):integer;stdcall;external 'project2.dll';

function MultiTwo(x,y:integer):integer;stdcall;external 'project2.dll';

type
  TCallBackFun=function(x,y:integer):integer;stdcall;

function Myfun(x,y:integer;f:TCallBackFun):integer;
begin
  result := TCallBackFun(f)(x,y);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  Edit1.Text := IntToStr(MyFun(10,20,@AddTwo));

  Edit2.Text := IntToStr(MyFun(10,20,@MultiTwo));

end;

end.

//DLL中
library Project2;

{ 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;
 
  function AddTwo(x,y:integer):integer;stdcall;
  begin
    Result:=x+y;
  end;

  function MultiTwo(x,y:integer):integer;stdcall;
  begin
    Result:=x*y;
  end;

{$R *.RES}

  exports AddTwo,MultiTwo;   //函數名

begin

end.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章