delphi 静态/动态调用DL  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 1.2481451034546 seconds