System Delphi

Title: Delphi Simple DLL
Question: How to create delphi articles
Answer:
Creating DLL
library Data;
{ 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
SysUtils,
Classes;
{$R *.res}
function suma(x,y:integer):integer; stdcall;
begin
asm
mov eax,x;
add eax,y
mov @Result,eax;
end;
end;
function diferenta(x,y:integer):integer; stdcall;
begin
asm
mov eax,x;
sub eax,y
mov @Result,eax;
end;
end;
function produs(x,y:integer):integer; stdcall;
begin
produs:=x*y;
end;
function impartire(x,y:integer):extended; stdcall;
begin
impartire:=x/y;
end;
function modul(x:integer):integer; stdcall;
begin
modul:=abs(x);
end;
procedure exit;
begin
halt;
end;
exports suma index 1,
diferenta index 2,
produs index 3,
impartire index 4,
modul index 6,
exit index 5;
begin
end.
How to use the DLL
unit UsingDLL;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function suma(x,y:integer):integer; stdcall; external 'Data.dll' index 1;
function diferenta(x,y:integer):integer; stdcall; external 'Data.dll' index 2;
function produs(x,y:integer):integer; stdcall; external 'Data.dll' index 3;
function impartire(x,y:integer):extended; stdcall; external 'Data.dll' index 4;
procedure exit; stdcall; external 'Data.dll' index 5;
function modul(x:integer):integer; stdcall; external 'Data.dll' index 6;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var x,y:integer;
begin
x:=strtoint(edit1.text);
y:=strtoint(edit2.text);
memo1.Lines.Clear;
memo1.Lines.Add('Adunare: '+inttostr(suma(x,y)));
memo1.Lines.Add('Scadere: '+inttostr(diferenta(x,y)));
memo1.Lines.Add('Inmultire: '+inttostr(produs(x,y)));
memo1.Lines.Add('Impartire: '+floattostr(impartire(x,y)));
memo1.Lines.Add('Modul '+edit1.text+': '+inttostr(modul(x)));
memo1.Lines.Add('Modul '+edit2.text+': '+inttostr(modul(y)));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
exit;
end;
end.