Mega Code Archive

 
Categories / Delphi / Examples
 

[] Bir dll yi yüklemek

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