DLL 的静态调用实例代码


本文整理自网络,侵删。

 
自己写了一MinMax.dll文件 里面定义了2个函数Min、Max
在测试中使用了静态调用的方法


 
完整代码如下:
----------------------------------------
unit unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Buttons;

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

var
  Form1: TForm1;

implementation


{$R *.dfm}
uses unit3;
function min(x,y:integer):integer;stdcall; external 'Minmax.dll' name 'Min';
function max(x,y:integer):integer;stdcall; external 'Minmax.dll' name 'Max';

procedure TForm1.Button1Click(Sender: TObject);
var
  x,y,mi,ma:integer;
begin
  if (edit1.Text ='') or (edit2.Text ='') then
    showmessage('X或Y值为空!')
  else
  begin
    x:=strtoint(trim(edit1.Text) );
    y:=strtoint(trim(edit2.Text) );
    mi:=min(x,y);
    ma:=max(x,y);
    Memo1.Lines.Clear ;
    Memo1.Lines.Add ('最小值:'+inttostr(mi));
    Memo1.Lines.Add ('最大值:'+inttostr(ma));
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Form3:=TForm3.Create(Form1);
  Form3.ShowModal ;
end;

end.

-----------------------------------

MinMax.dll文件源码如下:

 

library Minmax;

{ 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}
function Min(x,y:integer):integer;stdcall ;
begin
  if (x<y) then
    Min:=x
  else
    Min:=y;
end;

function Max(x,y:integer):integer;stdcall ;
begin
  if (x>y) then
    Max:=x
  else
    Max:=y;
end;
exports Min,Max;
begin
end.
------------------------------

 https://blog.csdn.net/rshhs/article/details/4767997

相关阅读 >>

Delphi fmx下怎么得到当前程序的路径和文件名称?

Delphi 关闭所有正在运行的程序

Delphi winapi: getwindowtext - 获取窗口标题

Delphi: ttreeview 中禁止双击事件展开或关闭节点

Delphi中paramstr的用法

Delphi xe 跨平台(windows、android安卓、苹果macos、苹果ios)写法

Delphi 坐标截图如何实现

Delphi 用firedac处理sqlite的日期型

Delphi与用windows 7下的用户账户控制(uac)机制

Delphi中用拼音首字符序列来实现检索功能

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...