home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / unity / d56 / FNDUTL.ZIP / System / cDynLib.pas next >
Encoding:
Pascal/Delphi Source File  |  2002-10-29  |  4.8 KB  |  136 lines

  1. {$INCLUDE ..\cDefines.inc}
  2. unit cDynLib;
  3.  
  4. {                                                                              }
  5. {                    Dynamically Loaded Libraries 3.02                         }
  6. {                                                                              }
  7. {      This unit is copyright ⌐ 2001-2002 by David Butler (david@e.co.za)      }
  8. {                                                                              }
  9. {                  This unit is part of Delphi Fundamentals.                   }
  10. {                    Its original file name is cDynLib.pas                     }
  11. {       The latest version is available from the Fundamentals home page        }
  12. {                     http://fundementals.sourceforge.net/                     }
  13. {                                                                              }
  14. {                I invite you to use this unit, free of charge.                }
  15. {        I invite you to distibute this unit, but it must be for free.         }
  16. {             I also invite you to contribute to its development,              }
  17. {             but do not distribute a modified copy of this file.              }
  18. {                                                                              }
  19. {          A forum is available on SourceForge for general discussion          }
  20. {             http://sourceforge.net/forum/forum.php?forum_id=2117             }
  21. {                                                                              }
  22. { Revision history:                                                            }
  23. {   2001/06/26  1.01  Added TDynamicLibrary.                                   }
  24. {   2002/06/06  3.02  Created cDynLib unit from cSysUtils.                     }
  25. {                                                                              }
  26.  
  27. interface
  28.  
  29. uses
  30.   // Delphi
  31.   Windows,
  32.   SysUtils;
  33.  
  34.  
  35.  
  36. { DLL functions                                                                }
  37. type
  38.   EDynamicLibrary = class (Exception);
  39.  
  40. Function  LoadDLL (const FileName : String) : HMODULE;
  41. Procedure UnloadDLL (const Handle : HMODULE);
  42. Function  LoadAnyDLL (const FileNames : Array of String) : HMODULE;
  43. Function  GetDLLProcAddress (const Handle : HMODULE; const ProcName : String) : FARPROC;
  44.  
  45.  
  46.  
  47. { TDynamicLibrary                                                              }
  48. type
  49.   TDynamicLibrary = class
  50.     Handle : HMODULE;
  51.  
  52.     Constructor Create (const FileName : String); overload;
  53.     Constructor Create (const FileNames : Array of String); overload;
  54.     Destructor  Destroy; override;
  55.  
  56.     Function  GetProcAddress (const ProcName : String) : FARPROC;
  57.   end;
  58.  
  59.  
  60.  
  61. implementation
  62.  
  63.  
  64.  
  65. {                                                                              }
  66. { Dynamic Libraries                                                            }
  67. {                                                                              }
  68. Function DoLoadLibrary (const FileName : String) : HMODULE;
  69.   Begin
  70.     Result := LoadLibrary (PChar (FileName));
  71.   End;
  72.  
  73. Function LoadDLL (const FileName : String) : HMODULE;
  74.   Begin
  75.     Result := DoLoadLibrary (FileName);
  76.     if Result = 0 then
  77.       raise EDynamicLibrary.Create ('Could not load DLL: ' + FileName + ': Error #' +
  78.                             IntToStr (GetLastError));
  79.   End;
  80.  
  81. Function LoadAnyDLL (const FileNames : Array of String) : HMODULE;
  82. var I : Integer;
  83.   Begin
  84.     For I := 0 to Length (FileNames) - 1 do
  85.       begin
  86.         Result := DoLoadLibrary (FileNames [I]);
  87.         if Result <> 0 then
  88.           exit;
  89.       end;
  90.     raise EDynamicLibrary.Create ('Could not load DLLs: Error #' +
  91.                           IntToStr (GetLastError));
  92.   End;
  93.  
  94. Procedure UnloadDLL (const Handle : HMODULE);
  95.   Begin
  96.     FreeLibrary (Handle);
  97.   End;
  98.  
  99. Function GetDLLProcAddress (const Handle : HMODULE; const ProcName : String) : FARPROC;
  100.   Begin
  101.     Result := GetProcaddress (Handle, PChar (ProcName));
  102.     if Result = nil then
  103.       raise EDynamicLibrary.Create ('Could not import function from DLL: ' + ProcName +
  104.                             ': Error #' + IntToStr(GetLastError));
  105.   End;
  106.  
  107. { TDynamicLibrary                                                              }
  108. Constructor TDynamicLibrary.Create (const FileName : String);
  109.   Begin
  110.     inherited Create;
  111.     Handle := LoadDLL (FileName);
  112.   End;
  113.  
  114. Destructor TDynamicLibrary.Destroy;
  115.   Begin
  116.     if Handle <> 0 then
  117.       UnloadDLL (Handle);
  118.     inherited Destroy;
  119.   End;
  120.  
  121. Constructor TDynamicLibrary.Create (const FileNames : Array of String);
  122.   Begin
  123.     inherited Create;
  124.     Handle := LoadAnyDLL (FileNames);
  125.   End;
  126.  
  127. Function TDynamicLibrary.GetProcAddress (const ProcName : String) : FARPROC;
  128.   Begin
  129.     Result := GetDLLProcAddress (Handle, ProcName);
  130.   End;
  131.  
  132.  
  133.  
  134. end.
  135.  
  136.