Name

DllProc Variable

Syntax

unit SysInit;
var DllProc: Pointer;

procedure DllHandler(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 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.

Example

// 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.

See Also

Finalization Keyword, Initialization Keyword, IsMultiThread Variable, Library Keyword, Threadvar Keyword
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset