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
IsMultiThreadto 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
DLLProcprocedure in the context of the new thread, so you can usethreadvarvariables when attaching the DLL to a new thread. Do not usethreadvarvariables when detaching, though. Delphi cleans up all thread local storage before callingDllProc.The
Dll_Process_Detachreason is most useful if theDLLProcprocedure is in the library’s project file. In a unit, you can use the unit’sfinalizationsection instead.
Example
// Keep track of the application's threads. Threads contains // the thread IDs, but you can ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access