DllProc Variable
unit SysInit; var DllProc: Pointer; procedureDllHandler
(Reason: Integer); begin ... end; DllProc := @DllHandler
;
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.
Windows calls the DLLProc
procedure in the context
of the new thread, so you can use threadvar
variables when attaching the DLL to a new thread. Do not use
threadvar
variables when detaching, though. Delphi
cleans up all thread local storage before calling
DllProc
.
The Dll_Process_Detach
reason is most useful if
the DLLProc
procedure is in the library’s
project file. In a unit, you can use the unit’s
finalization
section instead.
// Keep track of the application's threads. Threads contains
// the thread IDs, but you can easily modify this example to
// store other information.
library ThreadMonitor;
uses Windows, SysUtils, Classes;
var
Threads: TThreadList;
procedure DllHandler(Reason: Integer);
begin
case Reason of
Dll_Thread_Attach:
with Threads.LockList do
try
IsMultiThread := Count >= 1;
Add(Pointer(GetCurrentThreadID));
finally
Threads.UnlockList;
end;
Dll_Thread_Detach:
with Threads.LockList do
try
Remove(Pointer(GetCurrentThreadID));
IsMultiThread := Count <= 1;
finally
Threads.UnlockList;
end;
Dll_Process_Detach:
FreeAndNil(Threads);
// Else Windows might invent new reasons in the future. Ignore them.
end;
end;
begin
Threads := TThreadList.Create;
DllProc := @DllHandler;
end.