{ 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,
//penniesint in 'penniesint.pas';
penniesint;
function penniestocoins(Totpennies:word;coinsrec:pcoinsrec):word;stdcall;
begin
result := totpennies;
with coinsrec^ do
begin
quarters := totpennies div 25;
totpennies := totpennies - quarters*25;
dimes := totpennies div 10;
totpennies := totpennies -dimes * 10;
nickels := totpennies div 5 ;
totpennies := totpennies - nickels*5;
pennies := totpennies;
end;
end;
exports
penniestocoins;
end.
就是定义了一个函数如何把美分换成硬币的个数
下面是这个DLL的接口单元
unit penniesint;
interface
type
pcoinsrec = ^tcoinsrec;
tcoinsrec = record
quarters,
dimes,
nickels,
pennies:word;
end;
{$ifndef pennieslib}
function penniestocoins(totpennies:word;coinsrec:pcoinsrec):word;stdcall;
{$endif}
implementation
{$ifndef pennieslib}
function penniestocoins;external 'pennieslib.dll' name 'penniestocoins';
{$endif}
end.
下面是在一个工程中调用这个DLL
{ Copyright ?2001 Delphi 6 Developer's Guide Xavier Pacheco
and Steve Teixeira }
implementation
uses PenniesInt; // Use an interface unit
{$R *.DFM}
procedure TMainForm.btnMakeChangeClick(Sender: TObject);
var
CoinsRec: TCoinsRec;
TotPennies: word;
begin
{ Call the DLL function to determine the minimum coins required
for the amount of pennies specified. }
TotPennies := PenniesToCoins(StrToInt(meTotalPennies.Text), @CoinsRec);
with CoinsRec do
begin
{ Now display the coin information }
lblQuarters.Caption := IntToStr(Quarters);
lblDimes.Caption := IntToStr(Dimes);
lblNickels.Caption := IntToStr(Nickels);
lblPennies.Caption := IntToStr(Pennies);
end
end;
procedure TForm1.Button2Click(Sender: TObject);
const libName= 'a.dll';
var
libHandle : THandle;
A : function(stFileName : PChar): DWORD; stdcall;
begin
libHandle := LoadLibrary(libName);
if libHandle <> 0 then
begin
try
HCHFilterDownload := GetProcAddress(libHandle, 'a');
ShowMessage(IntToStr(A('C:\notepad.exe')));
except
on e:Exception do
ShowMessage(e.Message);
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
thellodll=procedure(myform:tform);
//end;
var
Form1: TForm1;
dllinstance:thandle;
pfunc:tfarproc;
implementation
//uses Unit1;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
type
thellodll=procedure(myform:tform);
var
dllinstance:thandle;
pfunc:tfarproc;
begin
dllinstance:=loadlibrary('d:\aaa\project1.dll');
if dllinstance=0 then
begin
showmessage('can not load dll');
exit
end;
pfunc:=getprocaddress(dllinstance,'hellodll');
if pfunc<>nil then
thellodll(pfunc)(form1)
else
showmessage('can not find the function');
freelibrary(dllinstance);
uses
SysUtils,
forms,
Classes,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
function showform:integer;stdcall;
var
form:tform1;
begin
form:=tform1.Create(application);
result:=form.ShowModal;
form.Free;
end;
exports
showform;