delphi 静态/动态调用DL


本文整理自网络,侵删。

 
library TestDll;
{$S-}

uses
  Vcl.Forms,
  Winapi.Windows,
  System.SysUtils,
  System.Classes;

{$R *.res}


//stdcall参数的传递顺序

function Add(Num1, Num2: Integer): Integer; stdcall;
begin
  Result := Num1 + Num2;
  Exit;
end;

procedure ShowMessage(Content: string);
begin
  MessageBox(0, PWideChar(Content), '', MB_OK);
end;

function Show: Boolean; stdcall;
begin

  Application.MessageBox(PChar('Show FormDll'), '信息', 64);

  Result := True;

end;

exports

  Add;

begin

end.



//主程序
unit MainFrm;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
//DLL中导出的函数
function Add(Num1,Num2:Integer):Integer;stdcall;external 'TestDll.dll';

var
  Form1: TForm1;

implementation

{$R *.dfm}

//动态调用
procedure TForm1.Button1Click(Sender: TObject);
var
  MyFun: function(x, y: Integer): Integer; stdcall;
var
  Hand: System.Cardinal;
begin
   //加载DLL
  Hand := LoadLibrary('TestDll.dll');
  if Hand <> 0 then
  begin
    try
      MyFun := GetProcAddress(Hand, 'Add');

      ShowMessage(MyFun(10, 20).ToString);
    finally
       //释放DLL
      FreeLibrary(Hand);
    end;

  end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    ShowMessage(Add(15,1).ToString); //静态
end;

end.

相关阅读 >>

Delphi搜索字符串在流中的位置

Delphi 对比时间的函数

Delphi版的创建高权限进程

Delphi中datetimepicker控件同时输入日期和时间

Delphi combobox的属性和事件、及几个鼠标事件的触发

Delphi强制关闭执行程序(杀进程)

Delphi 提取网页源文件纯文本函数

Delphi tstringstream 简单用法

Delphi中判断某个文件是否已经打开

Delphi firemonkey限制tedit只能输入数字的完美方法

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



打赏

取消

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

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

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

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

评论

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