Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения
KANSoftWare

Динамическая загрузка DLL

Delphi , Файловая система , DLL и PlugIns

Динамическая загрузка DLL

Оформил: DeeCo
Автор: http://www.swissdelphicenter.ch

{ 
  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;

Статья Динамическая загрузка DLL раздела Файловая система DLL и PlugIns может быть полезна для разработчиков на Delphi и FreePascal.


Комментарии и вопросы


Ваше мнение или вопрос к статье в виде простого текста (Tag <a href=... Disabled). Все комментарии модерируются, модератор оставляет за собой право удалить непонравившейся ему комментарий.

заголовок

e-mail

Ваше имя

Сообщение

Введите код




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: DLL и PlugIns ::


реклама



©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru Rambler's Top100

Время компиляции файла: 2024-04-24 22:55:34
2024-04-25 04:36:30/0.0066919326782227/2