was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

30 Visitors Online


 
...Dlls statisch/dynamisch laden?
Autor: Thomas Stutz
[ Tip ausdrucken ]  

Tip Bewertung (35):  
     


{
  There are two possibilities to load a dll:

  1. Static loading of a DLL means that the DLL is loaded when the application is executed.
     This is the easier option to dynamic loading, as you'll see, however it means that if the DLL
     is missing, your program will not run.
}
  {Examples:}

  {a. Import a function called MYImportFunction from MYLIBRARY.dll}

  
procedure MYImportFunction; external 'MYLIBRARY.dll';

  {b. Import a routine under a different name from the one it has in the library. }
  
procedure MYImportFunction; external 'MYLIBRARY.dll' name 'MyNewImportFunctionName';

  {c. By ordinal value: Has to be the same value as when using the exports
      keyword when making the DLL}
  
procedure MYImportFunction; external 'MYLIBRARY.dll' index 10;


{
  2. Dynamic loading

  By dynamically loading a DLL you decide at runtime which DLL to use.
  This means you can give your program different functionality depending on which
  DLL's are present (freeware or shareware versions of a program).
  Or also if you want to load a library whose name or path must be computed at
  run time or generated from user input.

  Dynamic loading of a DLL loads the DLL in your application when
  it is needed and unload it once you its work is done.
  It requires more code to use,
  however it more resource friendly than static loading.
}

{**********************************************************}

{
  Es gibt zwei Möglichkeiten, eine DLL zu laden.

  1. Statisches Laden

  Beim statischen Laden wird die zu importierende Prozedur/Funktion mit "external" deklariert.
  Die Datei 'MYLIBRARY.DLL' wird dann beim Programmstart geladen.
}

  {Beispiele:}

  {a. Importiert eine Funktion MYImportFunction aus der Dll MYLIBRARY.dll }
  
procedure MYImportFunction; external 'MYLIBRARY.dll';

  {b. Man kann auch eine Routine unter einem anderen Namen importieren als der in der dll. }
  
procedure MYImportFunction; external 'MYLIBRARY.dll' name 'MyNewImportFunctionName';

  {c. Einen Index angeben: }
  
procedure MYImportFunction; external 'MYLIBRARY.dll' index 10;


  // 2. Dynamisches Laden

{
  Beim Dynamischen Laden erfolgt der Zugriff auf die Routinen mit
  direkten Windows-API-Aufrufen (LoadLibrary, FreeLibrary, GetProcAddress).
  Die importierten Routinen werden über prozedurale Variablen referenziert.

  Die importierten DLLs werden erst bei der Ausführung des Quelltextes geladen.
  Somit wird Speicherplatz eingespart und das Programm wird auch ausgeführt,
  wenn einige DLLs fehlen.
}

{**********************************************************}

// Example for dynamically loading a DLL
// Beispiel um eine Dll dynamisch zu laden:


type
  
TDLLFunction = function(someParam: TSomeType): TSomeOtherType;

{
  To execute such a function by name from a named DLL one could use a
  "caller" function like
}

function CallDLLFunction(const dllname, functionname: string;
  theParameter: TSomeType): TSomeOtherType;
var
  
hDLL: THandle;
  theFunction: TDLLFunction;
  buf: array [0..144] of Char;
begin
  
// Get a handle to the DLL module.
  // das Handle zum DLL Modul ermitteln.
  
hDLL := LoadLibrary(StrPCopy(buf, dllname));
  // If the handle is valid, try to get the function address.
  // Wenn das Handle gültig ist, versuche die Adresse der Funktion zu ermitteln
  
if hDLL <> 0 then
  begin
    
// Return the address of the specified exported (DLL) function.
    // Adresse der Dll-Funktion ermitteln
    
try
      
@theFunction := GetProcAddress(hDll, StrPCopy(buf, functionname));
      // If the function address is valid, call the function.
      // Wenn die Funktion gefunden wurde...
      
if @theFunction <> nil then
        
Result := theFunction(theParameter)
      else
        raise 
EDLLException.CreateFmt('Unable to link to function %s in DLL %s!',
          [functionname, dllname]);
    finally
      
// Free the DLL module.
      // Dll wieder freigeben.
      
FreeLibrary(hDLL);
    end;
  end
  else
    raise 
EDLLException.CreateFmt('Unable to load DLL %s!'#13#10 +
      'Reason: %s.', [dllname, DLLErrors[hDLL]]);
end;


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners