��������������, ��� ������� ������� ������� DLL

Previous  Top  Next

    
 

 

��������������, ��� '�������' ������� ������� DLL

 

���������� ��� ������ ��� ������� � �������� ������� �� Dynamic Link Library (DLL). ������ ����� (������� ������ ����������� � ������ ���������), ���������� "�������" (Implicit) ���������. ������� �������� �������� � ���� ����������� �������� DLL ��� ������� ���������, � ��������� ������� � �������� ����� ��������� ���������� �������. ������ ����� ������ �������������� � ������, ���� ���������� ��������� ������� �� �������� DLL ��� ���������������� ����������������. ������ ����� ������� ���������� "�����" ���������, ��������� DLL ����������� ����������� �� ����������. ���� ����� ������� ��������������� ����������� � ������ ��������������, ���� ���������� ����� �������� � ������, ���� ���� DLL �� ������ ��������� �����������.

 

��� ����� "�������" �������������� �������?

 

������� �������, ��� ������ �������, ������� �� ���������� � ������ interface � ���� � ������ implementation (������ �� ���������� ����������� ��� ������), ������� ������������� �������, ��� ������ �������, ������������� �� DLL. ������� �������� ������� ����������� � ������������ ������, ������� ������������ ����� ����� � DLL. � Delphi ������� ������������ ����� ���� ������, ���������� ��� ���������� �������. ������ ������������� Delphi ��� ������� ��� ��� ������� ������� � ����������� ��������� ���������� Windows. �� ������ ��������� ������������� �������� ������� ��� ������� ������� dll, �� ��� � Delphi �� �������� �� ������ ������� ���������������� ������ DLL � �������� � ��� �������.

 

������, � ����� ������� ����� � ������ �������� �������� ��������� ���������� � ��������. ���� �� ������ ���������� ��� ��������� ������������ �� ��������� (���������) � DLL ������� �������� World Wide Web. ������ ����� ����� � MSDN, � ���� � ��� ��� ����������, �� ����� ���������� � �������������� ��������� ��������, ������� ��������� ������� ������ ��� ����������. ��� ��������� ��������� ������� ������� ����� ������������ ����� C++ � ��������� ���� Borland C++ ��� MS Visual C++. ���������� �� ������� � ��������������� ����� ������ �������� ��������� ��������� � ��������������� ������� ����� C++ � PASCAL. ������� ������ �� �������� ������������� ����� Delphi � C++ ���������� �� ����� Borland �� ������: http://www.borland.com/delphi/papers/brick.html.

 

����� ����, ��� �� ����� ����������� ������, ��� ������������ �� �������������� DLL ��������, �� ��������� ����� ����� �������� ������ ������. ��������� ������ ����� ��������� ��������� � ����, ����������� ��� ������ ��������� ������� DLL, � ��������� ����� �������. ������ ��������� ������� �������� ��������-������������ �����������, ����������� ������ ����������� Delphi �������� ������� ��������������� DLL. ��� ������ ������ ������ interface ����� ���������, ��������� ������� ����� implementation. ������ ������ implementation �������� ���������� ������������� ������� �������. ��� ��������� �� ��������� ���, ������� ����������� � ������ ������ interface (������� �������� �������� �������������� ������� ���� ������ ������ ���������� ��� ����������). ��� ��������� �������������� ���������� �� ���� ���� ���������� � ������ "DLLs:accessing procedures and functions" ������� ������ �� Delphi 3.

 

���������� ����, ��� � ��� ���� ������� � ������ BOB � DLL � ������ 'BLODGE.DLL'. (���� ��������� ��������� � ����������� ����, ��� ���������������, ��� �� ����� ������������ ������� �������� DLL):

 

����� ���������� �� ��������� �������, ��� ������� BOB ������ ���������� ���������� �������� � ������� � �������� ���������� �������� ���� word � boolean.

�������� � Delphi ����� ���� ������ � ������ 'UseBob.pas' (File|New � �������� unit)

��������� ������ ���� ������ ������������� � ������ interface ������ ������:

 

 

function BOB(Fire: Word; Dances: Boolean): Boolean; stdcall;

 

 

��������� ������ ���� ������ ������������� � ������ implementation ������ ������:

 

 

function BOB; external 'BLODGE';

 

 

��������� ������ � ������ 'UseBob.pas'.

���������� ��������� � ���, ��� UseBob.pas ���������� � �������� �������� �������, ��� ��������� � ��������, ����������� � ����� ������ Delphi.

�������� � ������ uses ������ ������ ������� ������ 'UseBob'. ������ ������� BOB ����� ���� ������� �� ������ ������� ������� ����� ������ ����������� �������.

�� ����� ���������� ���������� BLODGE.DLL ������ ��������� � ���� ������� ���������� ��������� ��������.

��� �������, ��� ������� 'BLODGE.DLL' ������ ���� ��������� ����, ��������� �������������� �����������. ��� ���� ����������� ����, ���������� ������ ���������� �������/�������� (� ��� ���������� � ������ �������).

���� �������� ������ � ����������� ������ ������� BOB, ���������������� ��� ������� �� ������:

 

 

Code:

unit UDLLTest;

 

interface

 

uses

 

Windows, Messages, SysUtils, Classes, Graphics, Controls,

Forms, Dialogs, StdCtrls;

 

type

 

TForm1 = class(TForm)

   Button1: TButton;

   procedure Button1Click(Sender: TObject);

private

   { Private declarations }

public

   { Public declarations }

end;

 

{ ��� ����, ������� ��������� ��� ������ ����� ������� bob }

 

TBOB = function(Fire: Word; Dances: Boolean): Boolean; stdcall;

 

var

 

Form1: TForm1;

 

implementation

 

{$R *.DFM}

 

procedure TForm1.Button1Click(Sender: TObject);

var

 

BOB: TBOB;

hDLLInst: THandle;

IsAlive, IsDancing: Boolean;

Years: Word;

 

begin

 

{ ��������� � �������� ���������� ������ BLODGE.DLL }

hDLLInst := LoadLibrary('BLODGE.DLL');

{ ���� �������� �� ���� ��������, ���������� ���� ���������� }

if (hDLLInst <= 0) then

   raise exception.create('[��������� ����� LoadLibrary]');

{ ���������� �������� ����� ������� BOB }

try

   @BOB := GetProcAddress(hDLLInst, 'BOB');

   if not assigned(BOB) then

     raise exception.Create('[��������� ����� GetProcAddress]');

   Years := 25;

   IsDancing := True;

   { ������ �� ����� ��������� ������� BOB }

   IsAlive := BOB(Years, IsDancing);

finally

   { ����������� ���������� DLL }

   FreeLibrary(hDLLInst);

end;

end;

 

end.

 

 

 

©Drkb::02002

       

����� � http://delphiworld.narod.ru