Http://blog.sina.com.cn/s/blog_62c46c3701010q7h.html
First, write the DLL
Library TestDllByD2007;
Uses
Sysutils,
Classes;
function test (const ASTR:PCHAR): Boolean;stdcall;
Begin
Result:=true;
End
{$R *.res}
Exports test;
Begin
End.
Note: 1. You cannot use the string type, otherwise it involves the issue of resource release, using Pchar instead of string.
String cannot be returned directly in 2.Dll, unless the SHAREMEM unit is referenced, the publisher needs to include BORLNDMM.DLL
Second, write the test form, just a button. In the Code of the button, the dynamic loading and releasing of the DLL is implemented.
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
Tftest=function (const Astr:pchar): Boolean;
TForm1 = Class (Tform)
Btn1:tbutton;
Procedure Btn1click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.btn1click (Sender:tobject);
Var
sdll:string;
Tfttt:tftest;
Handle:thandle;
sstr:string;//declares the variable in order to avoid an exception.
Begin
sdll:= ' TestDllByD2007.dll ';
Caption:= ";
Handle:=loadlibrary (Pansichar (Sdll));
If Handle <> 0 Then
Begin
@tfTTT: = GetProcAddress (Handle, ' test ');
If @tfTTT <> Nil Then
Begin
sstr:= ' testsss ';
If TFTTT (PChar (SSTR)) =true Then
Begin
Caption:= ' true ';
End
Else
Begin
Caption:= ' false ';
End
End
Else
Begin
Caption:= ' can not find the test function ';
End
FreeLibrary (Handle);
End
Else
Begin
Caption:= ' Can not load library ' + Sdll;
End
End
End.
Delphi Dll Dynamic Invocation Example (1)