http://blog.csdn.net/zhenghui1/article/details/6618273
Entry function and Exit function for 1.DLL
When you write a DLL, you can begin in the DLL project file. End joins the DLL's entry function and Exit function, the global variable dllproc is a process pointer, specify the entry/Exit function, the initial value is nil, only need to assign their own access function to it, their own access function must pass in a DWORD type parameter. This is the purpose for which the entry function event corresponds.
Dll_process_attach the DLL maps to the address space of the current process when the process starts or calls LoadLibrary (). During this event, the D l l initializes the instance data
The Dll_process_detach DLL is being detached from the address space of the process, which may be the process itself exiting or calling FreeLibrary (). In this event, the D l l may not have initialized any of the instances.
The Dll_thread_attach process creates a new thread. At this point, the system invokes all DLL entry functions associated with the process. This call is made in the context of the new thread and is used to allocate thread-specific data
Dll_thread_detach a thread is exiting. In this event, the DLL releases the thread-specific initialization data
Warning Call TerminateThread () did not call Dll_thread_detach when the thread was terminated abnormally.
Using callback functions in 2.DLL
Callback functions are typically called by Win32dll or other DLLs, rather than by applications. The address of the pass function when the callback function is called. In a DLL, you can define a type of callback function and then make it an argument for the exported function, and the actual application calls the DLL itself to implement the callback function type and pass the actual type parameters.
Case one.
Open Delphi to create a new DLL, the source code is as follows:
Library dllentry;
Uses
Sysutils,
Windows
Dialogs,
Classes;
{$R *.res}
Procedure DllEntryPoint (Dwreason:dword); Into the Exit function
Begin
Case Dwreason of
Dll_process_attach:showmessage (' Attaching to Process ');
Dll_process_detach:showmessage (' detaching from PROCESS ');
Dll_thread_attach:messagebeep (0);
Dll_thread_detach:messagebeep (0);
End
End
Begin
Dllproc:[email protected]; Assigning a value to a global variable
DllEntryPoint (Dll_process_attach); Pass DWORD Type value
End.
Call the DLL, create a new Delphi application, place a label on the form, and 4 button controls. The source code is as follows:
Unit mainfrm;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
Ttestthread=class (TThread)//define Thread
Procedure Execute;override;
Procedure Setcaptiondata;
End
Tmainform = Class (Tform)
Btnloadlib:tbutton;
Btnfreelib:tbutton;
Btncreatethread:tbutton;
Btnfreethread:tbutton;
Lblcount:tlabel;
Procedure Btnloadlibclick (Sender:tobject);
Procedure Btnfreelibclick (Sender:tobject);
Procedure Btncreatethreadclick (Sender:tobject);
Procedure Btnfreethreadclick (Sender:tobject);
Procedure Formcreate (Sender:tobject);
Private
Libhandle:thandle;
Testthread:ttestthread;
Counter:integer;
Gothread:boolean;
Public
{Public declarations}
End
Var
Mainform:tmainform;
Implementation
{$R *.DFM}
Procedure Ttestthread.execute (); Override the thread's Execute method
Begin
While Mainform.gothread do
Begin
Synchronize (Setcaptiondata);
INC (Mainform.counter);
End
End
Procedure Ttestthread.setcaptiondata; Thread Async method modifies the caption of a Label control
Begin
Mainform.lblcount.caption:=inttostr (Mainform.counter);
End
Procedure Tmainform.btnloadlibclick (Sender:tobject); Load DLL
Begin
If Libhandle=0 Then
Begin
Libhandle:=loadlibrary (' DLLEntry.dll ');
If Libhandle=0 Then
Raise Exception.create (' Unable to Load DLL ');
End
Else
Messagedlg (' Library already loaded ', mtwarning,[mbok],0);
End
Procedure Tmainform.btnfreelibclick (Sender:tobject); Release DLL
Begin
If not (libhandle=0) then
Begin
FreeLibrary (Libhandle);
libhandle:=0;
End
End
Procedure Tmainform.btncreatethreadclick (Sender:tobject); Open Thread
Begin
If Testthread=nil Then
Begin
Gothread:=true;
Testthread:=ttestthread.create (False);
End
End
Procedure Tmainform.btnfreethreadclick (Sender:tobject); Releasing threads
Begin
If Assigned (Testthread) Then
Begin
Gothread:=false;
Testthread.free;
Testthread:=nil;
counter:=0;
End
End
Procedure Tmainform.formcreate (Sender:tobject); Initialize operation
Begin
libhandle:=0;
Testthread:=nil;
End
End.
Run
Create a new DLL with the following source code:
Library strsrchlib;
Uses
Sysutils,
Windows;
{$R *.res}
Type
Tfoundstrproc=procedure (Strpos:pchar); stdcall; Defining process Types
function Searchstr (Asrcstr,asearchstr:pchar; APROC:TFARPROC): Integer;stdcall; String Lookup function
Var
Findstr:pchar;
Begin
FINDSTR:=ASRCSTR;
Findstr:=strpos (FINDSTR,ASEARCHSTR); Find where Asearchstr appears in findstr
While Findstr<>nil do
Begin
If Aproc<>nil Then
Tfoundstrproc (Aproc) (FINDSTR); Coercion type conversion, findstr as a parameter
findstr:=findstr+1;
Findstr:=strpos (FINDSTR,ASEARCHSTR); Loop find
End
End
Exports
SEARCHSTR;
Begin
End.
Create a new Delphi application, place an edit memo and a button control on the form, and the source code is as follows:
Unit calltest;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
Tmainform = Class (Tform)
Btncalldllfunc:tbutton;
Lblsrchwrd:tlabel;
Mmostr:tmemo;
Edtsearchstr:tedit;
Procedure Btncalldllfuncclick (Sender:tobject);
End
Var
Mainform:tmainform;
Count:integer; Global count variable
Implementation
{$R *.DFM}
function Searchstr (Asrcstr,asearchstr:pchar; APROC:TFARPROC): integer;stdcall;external ' StrSrchLib.dll '; Declaring an import function
Procedure Strposproc (Astrpsn:pchar); stdcall; Specific type of callback function
Begin
Inc (count);
End
Procedure Tmainform.btncalldllfuncclick (Sender:tobject);
Var
s,s2:string;
Begin
count:=0;
SetLength (S,mmostr.gettextlen);
Mmostr.gettextbuf (PChar (s), Mmostr.gettextlen); These two functions are to assign the contents of the memo control to the S string
S2:=edtsearchstr.text;
Searchstr (PChar (s), PChar (S2), @StrPosProc);
Showmessagefmt ('%s%s '%s ', [Edtsearchstr.text, ' occurs ', count, ' times. ');
End
End.
DLL There are many other features, not listed here, the source code reference DELPHI5 Programming Guide, in Delphi7 under the compiler run through, about the DLL also need more application in the project in order to practice makes perfect.
Other applications of DLLs in Delphi