delphi dll 调用

library circle;
uses
  System.SysUtils,
  System.Classes,
  math;

{$R *.res}
 function CircleArea(const Radius:double):double;stdcall;
 begin
   Result:= Radius*Radius*3.1415926;
 end;
 exports
  CircleArea;

begin
end.
/////////////////////////////<以上是DLL>//////////////////////////////////////////////////

unit Unit5;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

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

var
  Form5: TForm5;
 function CircleArea(const radius : double) : double; external 'circle.dll';  //静态调用
implementation

{$R *.dfm}

procedure TForm5.Button1Click(Sender: TObject);
VAR
R:DOUBLE;
begin
if EDIT1.Text='' then EXIT;
R:=STRTOFLOAT(EDIT1.Text);
 EDIT2.TEXT:=FLOATTOSTR(CircleArea(R)); //静态调用
end;

procedure TForm5.Button2Click(Sender: TObject);   //动态调用
type
TCircleAreaFunc=function (const radius: double) : double; stdcall;
var
dllHandle : cardinal;
circleAreaFunc : TCircleAreaFunc;
R:double;
begin
  if edit1.Text='' then exit;

  dllHandle := loadlibrary('circle.dll');
  if dllHandle<>0 then
  begin
      @circleAreaFunc :=GetProcAddress(dllHandle,'CircleArea');
      if Assigned (circleAreaFunc) then
      begin
         r:=strtofloat(edit1.Text);
       edit2.Text :=floattostr( circleAreaFunc(r));  //call the function  计算半径为15的圆面积了;
      end

      else
      ShowMessage('"CircleArea" function not found') ;   //  CircleArea 不存在的话
      FreeLibrary(dllHandle) ;   //退出江湖
  end//if dllHandle<>0 then
  else
  ShowMessage('circle.dll not found / not loaded') ; //
end;

end.
 

猜你喜欢

转载自blog.csdn.net/qq_25439957/article/details/85304361
今日推荐