Приветствую Вас Гость | RSS

Меню сайта

Реклама

Категории раздела
CD-ROM [11]
DLL и PlugIns [75]
Help файлы [20]
INI файлы [16]
RS232 [2]
Активные директории [1]
Директории [0]
Диски [0]
Корзина [5]
Порты [26]
Ресурсы [0]
Файлы [0]
Форматы файлов [15]
Ярлыки [0]

Наш опрос
Есть ли у Вас свой сайт?
Всего ответов: 48

Статистика

Онлайн всего: 1
Гостей: 1
Пользователей: 0

Форма входа

Главная » Статьи » Файловая система » DLL и PlugIns

Динамическая загрузка DLL
{ 
 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 и PlugIns | Добавил: Angel (06.07.2008)
Просмотров: 451 | Рейтинг: 0.0/0
  Delphi Lab   Главная   Регистрация   Вход  
Интересная Цитата

Поиск

Магазин


Copyright MyCorp © 2025 Хостинг от uCoz