본문 바로가기
Delphi, RadStudio

[delphi] Tracking DLL loading, attaching, detaching and unloading

by SB리치퍼슨 2011. 12. 12.
DLL 을 이용할 때 dll 이 로딩되는지 언로딩 되는지 알 수 있다.

Library testdll;

http://www.delphifaq.com/faq/delphi_windows_API/f534_0.htm

The code snippet shows how your DLL can track its usage.



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.
반응형

댓글