Author Topic: Cross platform DirectoryWatcher  (Read 14505 times)

constructor

  • Newbie
  • Posts: 1
Re: Cross platform DirectoryWatcher
« Reply #15 on: June 13, 2022, 08:06:49 pm »
when building, it's saying that OnFileEvent needs more parameters. On the demo app, the problem doesn't occur.
Any clue?

Hi,
I got Error: Wrong number of parameters specified for call to "OnFileEvent" when using
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
and replacing it with
Code: Pascal  [Select][+][-]
  1. {$mode Delphi}{$H+}
fixed it.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12995
  • FPC developer.
Re: Cross platform DirectoryWatcher
« Reply #16 on: June 14, 2022, 11:13:51 am »
Assignment of procedure/method types requires an @ in objfpc and fpc modes.

If you change to tp or delphi mode, you have to remove or ifdef them

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Cross platform DirectoryWatcher
« Reply #17 on: September 29, 2023, 07:44:18 am »
@marcov
this did the trick! thank you!


eny

  • Hero Member
  • *****
  • Posts: 1669
Re: Cross platform DirectoryWatcher
« Reply #18 on: September 28, 2024, 07:50:29 pm »
Warning for anyone who ends up here: there are quite some problems with the way it's implemented.
The most problematic one (on Windows 10): the dw cannot be stopped/destroyed.
Trying to do so will trigger an Access Violation error.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

LemonParty

  • Hero Member
  • *****
  • Posts: 651
Re: Cross platform DirectoryWatcher
« Reply #19 on: August 14, 2026, 03:45:45 pm »
I have Windows 11, and using trunk compiler (version 3.3.1). And I tryied to use this library.
My attempt was with this code:
Code: Pascal  [Select][+][-]
  1. program CacheDirectory;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes, DirectoryWatcherBuilder, DirectoryWatcherAPI
  10.   { you can add units after this };
  11.  
  12. {$R *.res}
  13. type
  14.  
  15.   { TTempObj }
  16.  
  17.   TTempObj = object
  18.   public
  19.     procedure OnFileEvent(const FilePath: String; const EventType: TDirectoryEventType);
  20.   end;
  21.  
  22. const
  23.   CTargetDirectory = 'X:\tmp\Test';
  24. var
  25.   DW: IDirectoryWatcher;
  26.   T: TTempObj;
  27.  
  28. { TTempObj }
  29.  
  30. procedure TTempObj.OnFileEvent(const FilePath: String;
  31.   const EventType: TDirectoryEventType);
  32. var
  33.   EventTypeString: String;
  34. begin
  35.   WriteLn('File: ' + FilePath);
  36.  
  37.   case EventType of
  38.   detAdded: EventTypeString := 'ADDED';
  39.   detRemoved: EventTypeString := 'REMOVED';
  40.   detModified: EventTypeString := 'MODIFIED';
  41.   end;
  42.  
  43.   WriteLn('Type: ' + EventTypeString);
  44. end;
  45.  
  46. begin
  47.   DW:= TDirectoryWatcherBuilder
  48.     .New
  49.     .WatchDirectory(CTargetDirectory)
  50.     .Recursively(True)
  51.     .OnChangeTrigger(@T.OnFileEvent)
  52.     .Build;
  53.   DW.Start;
  54.  
  55.   WriteLn(
  56.     'Watching directory ', CTargetDirectory, LineEnding,
  57.     'Press ENTER to stop'
  58.   );
  59.   Readln;
  60.   DW:= nil;
  61. end.
When I run this program in debug mode I am getting 'External: SIGSEGV' error on line 129 in file DirectoryWatcherThread.Windows. This line:
Code: Pascal  [Select][+][-]
  1.   EventArray[1] := Integer(TermEvent.Handle^);
from first sight it is strange that we dereferencing a handle but maybe I mistake.

Has anybody a fix to this library or maybe another code that can watch directories?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Hero Member
  • *****
  • Posts: 651
Re: Cross platform DirectoryWatcher
« Reply #20 on: August 14, 2026, 07:21:39 pm »
I was right. This code has a mistake. Original code is:
Code: Pascal  [Select][+][-]
  1.   EventArray[0] := FileEvent;
  2.   EventArray[1] := Integer(TermEvent.Handle^);
  3.   EventArray[2] := Integer(SuspEvent.Handle^);
The code that work for me in 64-bit application is:
Code: Pascal  [Select][+][-]
  1.   EventArray[0] := FileEvent;
  2.   EventArray[1] := PtrUInt(TermEvent.Handle);
  3.   EventArray[2] := PtrUInt(SuspEvent.Handle);
I will report this to issues on github.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Hero Member
  • *****
  • Posts: 651
Re: Cross platform DirectoryWatcher
« Reply #21 on: August 14, 2026, 07:41:15 pm »
It seems the same problem on line 52 of file DirectoryWatcher.Windows. The correct code is:
Code: Pascal  [Select][+][-]
  1.   PulseEvent(PtrUInt(StopEvent.Handle));
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018