Delphi exports dll 파일 생성

Delphi의 exports는 dll 프로젝트에 사용되며 dll의 출력 함수를 가리킨다.exports에서 설명한 함수만 외부에서 호출할 수 있습니다.다음은 간단한 Dll 항목입니다.
library dllTest;
uses SysUtils,Classes;
function Test1(a,b:integer):integer;
begin
Result:=a+b;
end;
exports
Test1 index 1;
begin
end.

이 Dll에서 덧셈 함수를 설명한 다음 exports 문구로 출력합니다. 출력된 함수와 과정만 다른 프로그램에서 호출될 수 있습니다.exports 문장의 문법은 함수 이름 [index]이고 [index]은 함수에 지정된 색인 번호로 다른 프로그램이 함수 주소를 정할 수 있도록 합니다.물론 index 키워드가 없으면 델파이는 exports 후 함수에 따라 자동으로 1에서 색인 번호를 분배합니다.
다음은 참조할 수 있도록 패키지된 객체의 예입니다.
Demo 클래스를 작성합니다. 주로 차의 상속과 합성 관계이고 다음은 각 클래스의 상속 관계입니다.
클래스 코드:
unit Demo;

interface

uses
  SysUtils, Windows, Messages, Classes, Dialogs;

type
  TEngine = class(TObject)
  private
    FCapacity: Integer;
    FPower: Integer;
  public
    constructor create;
    procedure start;
    procedure stop;
  end;
  
  TWheel = class(TObject)
  private
    FNo: Integer;
    FSize: Integer;
    FTypeName: string;
    procedure check;
  public
    constructor create(size:Integer;TypeName:string;No:Integer);
  end;
  
  TVehicle = class(TObject)
  protected
    FColor: string;
    FMake: string;
    FTopSpeed: Integer;
    FWheel: TWheel;
    FWheels: TList;
    procedure SlowDown;
    procedure SpeedUp;
    procedure Start;
    procedure Stop;
  end;
  
  TBicycle = class(TVehicle)
  public
    constructor create; 
    destructor Destory;
    procedure ride;virtual;
  end;
  
  TCar = class(TVehicle)
  protected
    FEngine: TEngine;
  public
    constructor create;
    destructor Destory;
    procedure drive;virtual;
  end;

implementation


{
*********************************** TBicycle ***********************************
}
constructor TBicycle.create;
var
  i: Integer;
begin
  FColor:='  ';
  FMake:='  ';
  FTopSpeed:=20;
  ShowMessage(FColor+FMake+' ,    :'+IntToStr(FTopSpeed));
  FWheels:= TList.Create;
  for i:=1  to 2 do
    FWheels.Add(TWheel.create(21,'B      ',i));
end;

destructor TBicycle.Destory;
var
  i: Integer;
begin
  for i:=1  to 2 do
    TWheel(FWheels.Items[i]).Free;
  inherited;
end;

procedure TBicycle.ride;
begin
  start;
  speedUp;
  showmessage('       ...');
  SlowDown;
  Stop;
end;

{
************************************* TCar *************************************
}
constructor TCar.create;
var
  i: Integer;
begin
  inherited create;
  FEngine:=TEngine.create;
  FColor:='  ';
  FMake:='  ';
  FTopSpeed:=200;
  ShowMessage(FColor+FMake+' ,    :'+IntToStr(FTopSpeed));
  FWheels:= TList.Create;
  for i:=1  to 4 do
    FWheels.Add(TWheel.create(36,'A     ',i));
end;

destructor TCar.Destory;
var
  i: Integer;
begin
  for i:=1  to 4 do
    TWheel(FWheels.Items[i]).Free;
  FEngine.Free;
  inherited;
end;

procedure TCar.drive;
begin
  start;
  FEngine.start;
  speedUp;
  showmessage('     ...');
  SlowDown;
  FEngine.stop;
  Stop;
end;

{
*********************************** TEngine ************************************
}
constructor TEngine.create;
begin
  Fcapacity:=1500;
  Fpower:=130;
end;

procedure TEngine.start;
begin
  ShowMessage(inttostr(Fcapacity)+'cc,'+inttostr(Fpower)+'          !');
end;

procedure TEngine.stop;
begin
  ShowMessage('      ');
end;

{
*********************************** TVehicle ***********************************
}

procedure TVehicle.SlowDown;
begin
  ShowMessage('    ...');
end;

procedure TVehicle.SpeedUp;
begin
  ShowMessage('    ...');
end;

procedure TVehicle.Start;
begin
  ShowMessage('      ');
end;

procedure TVehicle.Stop;
begin
  ShowMessage('    ');
end;

{
************************************ TWheel ************************************
}
constructor TWheel.create(size:Integer;TypeName:string;No:Integer);
begin
  FSize:=size;
  FTypeName:=TypeName;
  FNo:=No;
  check;
end;

procedure TWheel.check;
begin
  ShowMessage('   '+IntToStr(FNo)+'   。  :'+FTypeName+'   :'+IntToStr(FSize));
end;

end.

Delphi에서 Dll 프로젝트를 만들고 다음 코드를 입력하여 dll 파일을 생성합니다.
library DemoSvr;

{ 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
  ShareMem,
  SysUtils,
  Classes,
  Demo in 'Demo.PAS';

{$R *.res}

function CarObj:TCar;
begin
  Result:=TCar.create;
end;

function BicycleObj:TBicycle;
begin
  Result:=TBicycle.create;
end;

exports
  CarObj,
  BicycleObj;
end.

dll 파일을 읽고 프로그램 실행 효과를 보여 주는 Form을 만듭니다.
unit frmDemo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
//---         DLL      ---

  TVehicle = class(TObject);
  TCar = class(TVehicle)
  public
    procedure drive;virtual;abstract;
  end;
  TBicycle = class(TVehicle)
  public
    procedure ride;virtual;abstract;
  end;

//----------------------------

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  function CarObj:TCar ;external 'DemoSvr.dll';
  function BicycleObj:TBicycle ;external 'DemoSvr.dll';

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var MyCar:TCar;
begin
  MyCar:=CarObj;
  if Mycar=nil then exit;
  try
    MyCar.drive;
  finally
    MyCar.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Bicycle:TBicycle;
begin
  Bicycle:=BicycleObj;
  try
    Bicycle.ride;
  finally
    Bicycle.Free;
  end;
end;

end.

프로그램 실행 예:

좋은 웹페이지 즐겨찾기