Sign in Join
Wiki copytemporary

callback

Callback functions are in widespread usage in Lazarus; in fact any Event is made available using 'callback'.

pascal
 type
  TForm = class(TForm)
    btn: TButton;
    procedure btnClick(Sender: TObject);
  end;
 ...
  procedure TForm.btnClick(Sender: TObject);
  begin
   // dosomething
  end;

The btnClick method is a callback from within the TButton implementation.

Apart from their use in the userinterface of a form, callbacks are used in many libraries that were linked.

Callback from library

c
DWORD LpprogressRoutine(
  LARGE_INTEGER TotalFileSize,
  LARGE_INTEGER TotalBytesTransferred,
  LARGE_INTEGER StreamSize,
  LARGE_INTEGER StreamBytesTransferred,
  DWORD dwStreamNumber,
  DWORD dwCallbackReason,
  HANDLE hSourceFile,
  HANDLE hDestinationFile,
  LPVOID lpData
)
pascal

TMyForm = class( TForm )
  progressbar: TProgressbar;
  bIsAborted: boolean;
  ...
end;

  // the callback function as needed by CopyFileEx
function _CopyCallback( 
  TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: Int64; 
  dwStreamNumber, dwCallbackReason: DWORD;
  hSourceFile, hDestinationFile: THandle; 
  f: TMyForm ): DWORD; stdcall;
var
  newpos: Integer;
const
  PROCESS_CONTINUE = 0;
begin
  result := PROCESS_CONTINUE;
  if dwCallbackReason = CALLBACK_CHUNK_FINISHED then begin
    newpos := Round( TotalBytesTransferred / TotalFileSize * 100 );
    with f.Progressbar do
      if newpos <> Position then
        Position := newpos;
    Application.ProcessMessages();
  end;
end;

...

  // copy 'fromfile' to 'tofile' showing progress in myFor.Progressbar and obey to abort in myForm.bIsAborted
result := CopyFileEx(  'fromfile', 'tofile', @_CopyCallback, pointer(myForm), @myForm.bIsAborted, 0 );

From the Free Pascal wiki, page "callback", as it stood on 2022-01-03 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.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.