DLL dynamically load: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: DLL dynamically load
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The tutorial shows how a DLL (Dynamic Link Library) is loaded dynamically . | |
| 2 | + | ||
| 3 | + | The DLL (DLLTest.dll) referred to in the example below: | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | library info; | |
| 7 | + | ||
| 8 | + | {$mode objfpc} {$H+} | |
| 9 | + | ||
| 10 | + | uses | |
| 11 | + | SysUtils; | |
| 12 | + | ||
| 13 | + | {$R *.res} | |
| 14 | + | ||
| 15 | + | // The DLL subroutine | |
| 16 | + | function funStringBack(strIn : string) : PChar; | |
| 17 | + | begin | |
| 18 | + | funStringBack := PChar(UpperCase(strIn)); | |
| 19 | + | end ; | |
| 20 | + | ||
| 21 | + | // Exported subroutine(s) | |
| 22 | + | exports | |
| 23 | + | funStringBack; | |
| 24 | + | ||
| 25 | + | begin | |
| 26 | + | end. | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | What should I do: | |
| 30 | + | ||
| 31 | + | - Define memory | |
| 32 | + | - A data type must be created that corresponds exactly to the (external) subroutine that is to be imported from the DLL. | |
| 33 | + | - Reserve memory | |
| 34 | + | - Memory must be reserved for a variable (data field) to which the above data type is assigned. | |
| 35 | + | - Memory must be reserved for a handle, to which the DLL handle will be assigned later. | |
| 36 | + | - Assign DLL and external subroutine and accept the data | |
| 37 | + | - Call the DLL and assign the handle of the DLL to the handle. | |
| 38 | + | - The pointer for the variables must be changed to the memory of the external subroutine. | |
| 39 | + | - The result of the external subroutine is to be accepted. | |
| 40 | + | - Free all memory | |
| 41 | + | - The pointer for the variables must point to an invalid memory area again (:= nil) to release the external subroutine. | |
| 42 | + | - The memory of the DLL must be released again. | |
| 43 | + | ||
| 44 | + | Integrate, use and release the DLL subroutine in your own program: | |
| 45 | + | ||
| 46 | + | ``` pascal | |
| 47 | + | uses | |
| 48 | + | Windows, ...; | |
| 49 | + | ||
| 50 | + | ... | |
| 51 | + | ||
| 52 | + | Include function funDll : string; | |
| 53 | + | type | |
| 54 | + | // Definition of the subroutine to be called, as defined in the DLL to be used | |
| 55 | + | TfunStringBack = function(strIn : string) : PChar; stdcall; | |
| 56 | + | ||
| 57 | + | var | |
| 58 | + | // Creates a suitable variable (data field) for the DLL subroutine | |
| 59 | + | funStringBack : TfunStringBack; | |
| 60 | + | // Creates a handle for the DLL | |
| 61 | + | LibHandle : THandle; | |
| 62 | + | ||
| 63 | + | begin | |
| 64 | + | // Get the handle of the library to be used | |
| 65 | + | LibHandle := LoadLibrary(PChar('DLLTest.dll')); | |
| 66 | + | ||
| 67 | + | // Checks whether loading the DLL was successful | |
| 68 | + | if LibHandle <> 0 then | |
| 69 | + | begin | |
| 70 | + | // Assigns the address of the subroutine call to the variable funStringBack | |
| 71 | + | // 'funStringBack' from the DLL DLLTest.dll | |
| 72 | + | Pointer(funStringBack) := GetProcAddress(LibHandle, 'funStringBack'); | |
| 73 | + | ||
| 74 | + | // Checks whether a valid address has been returned | |
| 75 | + | if @funStringBack <> nil then | |
| 76 | + | Result := funStringBack('hello world'); | |
| 77 | + | end; | |
| 78 | + | ||
| 79 | + | // release memory | |
| 80 | + | funStringBack := nil; | |
| 81 | + | FreeLibrary(LibHandle); | |
| 82 | + | ||
| 83 | + | end; | |
| 84 | + | ||
| 85 | + | ... | |
| 86 | + | ``` | |
| 87 | + | ||
| 88 | + | --- | |
| 89 | + | *From the Free Pascal wiki, page "DLL dynamically load", as it stood on 2020-02-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
