sloppycode.net
Using windows native DLLs
Static and dynamic loading of a DLL
Home
›
Code snippets
›
Delphi
›
Using windows native DLLs
This example uses the DLL made in the 'Creating a DLL' snippet. It shows how to use both static and dynamic loading of a DLL. 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. The external keyword can import procedures and functions in 3 different ways - by actual name, by a numerical value, by renaming. In the example below the procedure is imported using its original name, although the comments show you how to use the other 2 methods. The declaration of the procedure, as you can see in the code, goes after the type declaration. Its also worth noting that the procedures *ARE* case sensitive when you import them - unlike the normal rules of Object Pascal.
Dynamic loading of a DLL load 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. The second section of code below shows you how to dynamically load the DLL created in the 'Creating a DLL' snippet. Both bits of code assume that you have done File>New>Application. Both require a single button, the procedure for this is where the 'demoProcedure' procedure is called, in the case of dynamic loading, this is also where the DLL is loaded in, and unloaded.
------------ STATIC LOADING ---------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; procedure demoProcedure(TheForm : TForm); external 'Project1.dll'; { example other ways of importing the DLL: - By ordinal value, has to be the same value as when using the exports keyword when making the DLL procedure newName(TheForm : TForm); external 'Project1.dll' index 20; - By renaming it procedure demoProcedure(TheForm : TForm); external 'Project1.dll' name 'newProcedureName'; } implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); begin demoProcedure(Self); end; end. ------------ DYNAMIC LOADING ---------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); type TdemoProcedure = procedure(TheForm : TForm); var DLLInstance : THandle; demoProcedure : TdemoProcedure; begin DLLInstance := LoadLibrary('Project1.dll'); if DLLInstance = 0 then begin ShowMessage('Unable to load Project1.dll'); end else begin @demoProcedure := GetProcAddress(DLLInstance,'demoProcedure'); if @demoProcedure <> nil then demoProcedure(Self) else ShowMessage('Unable to find demoProcedure procedure'); end; end; end.
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search