使用 Lazarus 创建一个 DLL

library mydll;
 
{$mode objfpc}{$H+}
 
uses
  Classes, Windows
  { you can add units after this };
 
  function myMessage(): string; stdcall;
  begin
     MessageBox(0,PChar('Hello world'),PChar(''),MB_OK);
   end;
 
exports myMessage;
end.

动态调用

unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Dynlibs;
 
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
type
  TMyDLL = function (): string; stdcall;
 
{$R *.lfm}
 
{ TForm1 }
 
procedure TForm1.Button1Click(Sender: TObject);
var
  MyHandle: TLibHandle;
  MyDLLFunc: TMyDLL;
begin
  MyHandle := SafeLoadLibrary('mydll.dll');
  if MyHandle<>0 then
    begin
      MyDLLFunc := TMyDLL(GetProcedureAddress(MyHandle,'myMessage'));
      if Assigned(MyDLLFunc) then
        begin
          MyDLLFunc();
        end;
    end
  else
    begin
      //Notify can't found DLL
    end;
  FreeLibrary(MyHandle);
end;
 
end.

静态链接

unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
 
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
 
var
  Form1: TForm1;
 
procedure myMessage; external 'mydll.dll';
 
implementation
 
{$R *.lfm}
 
{ TForm1 }
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  myMessage;
end;
 
end.

你可能感兴趣的:(使用 Lazarus 创建一个 DLL)