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:
program CacheDirectory;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, DirectoryWatcherBuilder, DirectoryWatcherAPI
{ you can add units after this };
{$R *.res}
type
{ TTempObj }
TTempObj = object
public
procedure OnFileEvent(const FilePath: String; const EventType: TDirectoryEventType);
end;
const
CTargetDirectory = 'X:\tmp\Test';
var
DW: IDirectoryWatcher;
T: TTempObj;
{ TTempObj }
procedure TTempObj.OnFileEvent(const FilePath: String;
const EventType: TDirectoryEventType);
var
EventTypeString: String;
begin
WriteLn('File: ' + FilePath);
case EventType of
detAdded: EventTypeString := 'ADDED';
detRemoved: EventTypeString := 'REMOVED';
detModified: EventTypeString := 'MODIFIED';
end;
WriteLn('Type: ' + EventTypeString);
end;
begin
DW:= TDirectoryWatcherBuilder
.New
.WatchDirectory(CTargetDirectory)
.Recursively(True)
.OnChangeTrigger(@T.OnFileEvent)
.Build;
DW.Start;
WriteLn(
'Watching directory ', CTargetDirectory, LineEnding,
'Press ENTER to stop'
);
Readln;
DW:= nil;
end.
When I run this program in debug mode I am getting 'External: SIGSEGV' error on line 129 in file DirectoryWatcherThread.Windows. This line:
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?