Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  167

•  TDictionary Custom Sort  3 341

•  Fast Watermark Sources  3 095

•  3D Designer  4 851

•  Sik Screen Capture  3 350

•  Patch Maker  3 555

•  Айболит (remote control)  3 665

•  ListBox Drag & Drop  3 018

•  Доска для игры Реверси  81 731

•  Графические эффекты  3 948

•  Рисование по маске  3 253

•  Перетаскивание изображений  2 633

•  Canvas Drawing  2 761

•  Рисование Луны  2 586

•  Поворот изображения  2 194

•  Рисование стержней  2 170

•  Paint on Shape  1 569

•  Генератор кроссвордов  2 240

•  Головоломка Paletto  1 769

•  Теорема Монжа об окружностях  2 236

•  Пазл Numbrix  1 685

•  Заборы и коммивояжеры  2 059

•  Игра HIP  1 282

•  Игра Go (Го)  1 230

•  Симулятор лифта  1 475

•  Программа укладки плитки  1 219

•  Генератор лабиринта  1 548

•  Проверка числового ввода  1 367

•  HEX View  1 497

•  Физический маятник  1 359

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

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



Оформил: DeeCo

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

DLL in Resources

DLL Injector




Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте