Mega Code Archive

 
Categories / Delphi / Examples
 

Tracking dll loading, attaching, detaching and unloading

The code snippet shows how your DLL can track its usage. Library testdll; uses Windows, // DLL_PROCESS_nnn defined in here SysUtils, Classes; procedure MyDLLProc(Reason: Integer); begin case Reason of DLL_PROCESS_ATTACH: // called when the DLL is loaded begin end; DLL_PROCESS_DETACH: // called when the DLL is freed, if you want // multithreaded, look at DLL_THREAD_DETACH begin end; end; end; begin DLLProc := @MyDLLProc; // this sets the DLL entry point to the MyDLLProc. // when ever the DLL is loaded or unloaded, // this proc is called with: // DLL_PROCESS_ATTACH // DLL_PROCESS_DETACH // DLL_THREAD_ATTACH // DLL_THREAD_DETACH MyDLLProc(DLL_PROCESS_ATTACH); // because we interupted the default // dllproc, we must recall our proc // with the DLL_PROCESS_ATTACH reason. end.