type
//定义略了
procedure scrollbarredscroll(sender:tobject;scrollcode:tscrollcode; var scrollpas:integer);
var
formscroll:tformscroll;
//extern dll function declaration
function getcolor(col:longint):longint;stdcall;
implementation
{$R *.DFM}
procedure tformscroll.scrollbarredscroll(sender:tobject....);
begin
//略
end;
//extern dll function
function getcolor (col:longint):longint;
begin
//default value
result:=col;
try
formscroll:=tformscroll.create(application);
try
with formscroll do
begin
{initialize the date}
shape1.brush.color:=col;
shape2.pen.color:=col;
scrollbarred.position:=getrvalue(col);
{show the form}
if showmodal=mrok then
result:=rgb(scrollbarred.position,....)
end;
finally
formscroll.free;
end;
except
on E:exception do
messagedlg('error in formdll:'+e.message,mterror,[mbok],0);
end;
end;
type
tform1=class(tform)
button1:tbutton;
edit1:tedit;
label1:tlabel;
spinedit1:tspinedit;
label2:tlabel;
procedure button1click(sender:tobject);
private
{ private declarations}
public
{ public declarations }
var
form1:tform1;
implementation
{$R *.DFM}
type
TIntfunction=function(I:integer):integer;stdcall;
procedure tform1.button1click(sender:tobject);
var
hinst:THandle;
fpointer:TFarProc;
myfunct:TIntFunction;
begin
hinst:=loadlibrary('firstdll.dll');
if hinst>0 then
try
fpointer:=getprocaddress(hinst,pchar(edit1.text));
if fpointer<>nil then
begin
myfunct:=tintfunction(fpointer);
spinedit1.value:=myfunct(spinedit1.value);
end
else
showmessage('dll function not found');
finally
freelibrary(hinst);
end
else
showmessage(library not found');
end;
动态调用
You can access routines in a DLL through direct calls to Windows library functions, including LoadLibrary, FreeLibrary, and GetProcAddress (all declared in Delphi抯 Windows unit). In this case, use procedural-type variables to reference the imported routines. For example,
uses Windows, ...;
type
TTimeRec = record
Second: Integer;
Minute: Integer;
Hour: Integer;
end;
TGetTime = procedure(var Time: TTimeRec);
THandle = Integer;
var
Time: TTimeRec;
Handle: THandle;
GetTime: TGetTime;
...
begin
Handle := LoadLibrary('DATETIME.DLL');
if Handle <> 0 then
begin
@GetTime := GetProcAddress(Handle, 'GetTime');
if @GetTime <> nil then
begin
GetTime(Time);
with Time do
WriteLn('The time is ', Hour, ':', Minute, ':', Second);
end;
FreeLibrary(Handle);
end;
end;
When you import routines this way, the DLL is not loaded until the code containing the call to LoadLibrary executes. The DLL is later unloaded by the call to FreeLibrary. This allows you to conserve memory, and to run your program even when some of the DLLs it uses are not present.