Name
DllProc Variable
Syntax
unit SysInit; var DllProc: Pointer; procedureDllHandler
(Reason: Integer); begin ... end; DllProc := @DllHandler
;
Description
When Windows loads or unloads a DLL or
when a thread starts or ends, Windows call the
DllProc
procedure in the DLL, passing the reason
for the call as the Reason
argument. Windows
ordinarily calls the DLL procedure when the DLL is first loaded in a
process, but Delphi handles that situation by initializing every unit
and then executing the statement block in the library’s project
file. Thus, Delphi calls DllProc
only for the
other three cases, that is, Reason
is one of the
following constants (which are declared in the
Windows
unit):
-
Dll_Thread_Attach
A process that loaded the DLL has created a new thread. If more than one thread attaches to the DLL, be sure to set
IsMultiThread
to True.
-
Dll_Thread_Detach
A thread has terminated.
-
Dll_Process_Detach
A process is exiting or unloading the DLL.
Tips and Tricks
Windows calls the
DLLProc
procedure in the context of the new thread, so you can usethreadvar
variables when attaching the DLL to a new thread. Do not usethreadvar
variables when detaching, though. Delphi cleans up all thread local storage before callingDllProc
.The
Dll_Process_Detach
reason is most useful if theDLLProc
procedure is in the library’s project file. In a unit, you can use the unit’sfinalization
section instead.
Example
// Keep track of the application's threads. Threads contains // the thread IDs, but you can ...
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.