Calling a function in an external DLL (2. Late binding) in Delphi
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type // late binding, that is, the dynamic call of external functions mainly uses the following three commands: // LoadLibrary: Get DLL // GetProcAddress: Get function // FreeLibrary: release // Define a procedure type, the parameters must be consistent with the required function TMB = function(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private MB: TMB; {declaration function MB} inst: LongWord; {declare a variable to record the DLL handle to use} public { Public declarations } end; where Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin inst := LoadLibrary('user32.dll'); if inst <> 0 then MB := GetProcAddress(inst, 'MessageBoxW'); // MB: = GetProcAddress (inst, 'MessageBoxA'); {Delphi 2009 version used this sentence} end; // Call test: procedure TForm1.Button1Click(Sender: TObject); where t,b: PChar; begin t := 'title'; b := 'content'; MB(0, b, t, 0); end; procedure TForm1.FormDestroy(Sender: TObject); begin FreeLibrary (inst); {Remember to release} end; end.