[SOLVED]How to Hook to delete file operation in Win10 OS? lazarus / 21-General ---------------------------------------------------------------------- Jonvy · 2023-08-24 08:04 Dear all, I need make a program to monitor the file delete operation in my win 10 OS, the folder and the filename are both fixed. It's C:\Production\logs\Production.log. The scenario as this, in PC, the automation software(AS) running during machines in production, AS will continuously write log file to the C:\Production\logs, when the log file size reached defined size, such as 2M, it will be deleted, and same file name will be created again to continuoulsy write log. The logs can be used to analysis problems during machine line in production. But sometimes the machine runs to fast, when problem happens, log files has already been covered. So I need write a hook program to monintor when the file will be deleted, then copy and rename the file to the same folder,after this unhook it, let AS to delete and create new log file. It's difficult to find some examle to do this, mostly are SetWindowsHookEx for Mouse or Keyboard, no file related. Can someone give me an example for this? Thanks! Jonvy Best regards, Jonvy ---------------------------------------------------------------------- Thaddy · 2023-08-24 08:13 What you are describing is also called a "round-robin" pattern and the correct way to solve that is in the logging code itself: that simply needs to control the maximum file size, the delete (or empty and/or rename the previous file and start a new one) the file when the maximum file size is reached. Much simpler than hooking and rather basic computing. You are simply looking at your problem from a wrong perspective. ---------------------------------------------------------------------- Jonvy · 2023-08-24 08:47 Hello Thaddy, The logging code come from AS software, it's developed by 3rd company. I'm just user. Jonvy ---------------------------------------------------------------------- Thaddy · 2023-08-24 09:04 Quote from: Jonvy on August 24, 2023, 10:47:25 amHello Thaddy, The logging code come from AS software, it's developed by 3rd company. I'm just user. Jonvy Frankly I do not understand 3'th party logging software, because it is much easier to do yourself. Anyway, look a TAsyncProcess and see what you can do with that. If I have more information (code) I can give you a more informed answer. ---------------------------------------------------------------------- Jonvy · 2023-08-24 09:21 The AS software is a complete system for production,for data acquisition, communication between PLC and PC,control scanner scan barcode,check database and process,etc,very complicated software, output log files just of it's function. ---------------------------------------------------------------------- Fibonacci · 2023-08-24 13:59 You would need to inject a DLL and hook WinAPI, its overkill. Here is an idea to consider: Open file for read, read all its contents, then watch for changes. If change happens, read more data with already opened handle, keep it all in buffer. If you receive file deleted event then save your buffer to a new file, then start over. You can use this to monitor file changes: https://github.com/Wosi/DirectoryWatcher If you still want to hook WinAPI, you can use this: https://github.com/MahdiSafsafi/DDetours ---------------------------------------------------------------------- KodeZwerg · 2023-08-24 14:02 This just sounds ridiculous to me, "a complete system for production" that self-delete important log-files, never heard of before. Can you name it as a warning for others? Also I do think that you might have misconfigured this all-in-one product. ---------------------------------------------------------------------- 440bx · 2023-08-24 15:07 Quote from: Jonvy on August 24, 2023, 10:04:24 amSo I need write a hook program to monintor when the file will be deleted, then copy and rename the file to the same folder,after this unhook it, let AS to delete and create new log file. The keywords in that sentence are "will be deleted".  IOW, something that hasn't happened yet but is about to happen.  File changes are _not_ among those things that Windows notifies before they happen.  There is no Windows API that sends a notification about some file operation that is _about_ to happen. If you want to do something like that then the only way that comes to mind is to inject a dll in the process that manages the log file.  That dll can hook DeleteFileA/W (or whatever else is needed) and basically modify the behavior of the original program to be as desired. This is not simple.  If you want to see an example of how it's done, google "Matt Pietrek ApiSpy" (you need to know C.)  Note, Matt's approach is much simpler than the one used in Detour like utilities and it is all you need. One "solution" that is definitely _not_ a good solution would be to use a global hook because the dll that implements the global hook will be, by default, loaded into every process which makes it very high overhead to control something that is specific to one process. ---------------------------------------------------------------------- Thaddy · 2023-08-25 13:56 Quote from: 440bx on August 24, 2023, 05:07:58 pmThe keywords in that sentence are "will be deleted".  IOW, something that hasn't happened yet but is about to happen.  File changes are _not_ among those things that Windows notifies before they happen.  There is no Windows API that sends a notification about some file operation that is _about_ to happen.Of course there is: DeleteToRecycleBin(). I grant you that even on MSDN it is not very well documented.. ---------------------------------------------------------------------- Fibonacci · 2023-08-25 14:04 Quote from: Thaddy on August 25, 2023, 03:56:58 pmOf course there is: DeleteToRecycleBin(). I grant you that even on MSDN it is not very well documented.. Which DLL exports that function? ---------------------------------------------------------------------- Jonvy · 2023-08-27 00:33 Hello 440bx,  Looks like Matt Pietrek's ApiSpy will be useful, but it need more time to study it. Some guys use chatgpt give me one example for Delphi code. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Main; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, ShellAPI; type  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    FOldDeleteFile: function(lpFileName: PChar): BOOL; stdcall;    function NewDeleteFile(lpFileName: PChar): BOOL; stdcall;  public    { Public declarations }  end; var  Form1: TForm1; implementation {$R *.dfm} function RenameFileWithTimestamp(const FileName: string): string;var  NewFileName: string;begin  NewFileName := ChangeFileExt(FileName, '') + '_' +    FormatDateTime('yyyymmdd_hhnnss', Now) + ExtractFileExt(FileName);  RenameFile(FileName, NewFileName);  Result := NewFileName;end; function HookDeleteFile(lpFileName: PChar): BOOL; stdcall;var  FileName: string;begin  FileName := lpFileName;  // intercept delete operation,change file name and save...  RenameFileWithTimestamp(FileName);  Result := Form1.FOldDeleteFile(lpFileName);end; procedure TForm1.FormCreate(Sender: TObject);begin  // Get address of DeleteFile function  @FOldDeleteFile := GetProcAddress(GetModuleHandle('kernel32.dll'), 'DeleteFileA');  // Replace DeleteFile's address as our defined function  WriteProcessMemory(GetCurrentProcess, @DeleteFile, @NewDeleteFile, SizeOf(Pointer), nil);end; procedure TForm1.FormDestroy(Sender: TObject);begin  // Restore DeleteFile Original address  WriteProcessMemory(GetCurrentProcess, @DeleteFile, @FOldDeleteFile, SizeOf(Pointer), nil);end; function TForm1.NewDeleteFile(lpFileName: PChar): BOOL;begin  //  Here add you own logic  Result := FOldDeleteFile(lpFileName);end; end.  But after compiling, it has following error, main.pas(54,55) Error: Variable identifier expected   WriteProcessMemory(GetCurrentProcess, @DeleteFile, @NewDeleteFile, SizeOf(Pointer), nil);  looks like the define, FOldDeleteFile: function(lpFileName: PChar): BOOL; stdcall;    is not correct, but it give error for NewDeleteFile. Can someone help me solve error? Thanks! ---------------------------------------------------------------------- 440bx · 2023-08-27 01:34 Quote from: Jonvy on August 27, 2023, 02:33:16 amHello 440bx,  Looks like Matt Pietrek's ApiSpy will be useful, but it need more time to study it. Some guys use chatgpt give me one example for Delphi code. To start, that code does _not_ look right because it is not patching the IAT which is what should be patched.  The code should be scanning the import address table looking for the thunk that contains the address of DeleteFileA which may or may not be the address returned by GetProcAddress (Note: GetProcAddress never returns the address of the IAT thunk which is where the interception is implemented.) On a slightly separate note, trusting ChapGpt is like trusting a politician.  I wouldn't trust either one unless I was looking for something that won't work. Attached to this post is a Pascal version of Matt Pietrek's APISPY which I tested on Windows XP but no other version of Windows, IOW, I have no idea if it works on any version of Windows after Windows XP.  Note also that, the original code was written back in 1995 using Delphi 2.  It was never ported to a later version of Delphi nor any version of FPC.  I haven't even tried compiling it with FPC but, you may find it useful as a guide. Lastly, PDFs of Matt Pietrek's book "Windows 95 System Programming Secrets" can be found on the net (which is obviously a copyright violation), in the book Matt explains in great detail how to hook Windows APIs.  I suggest you make it a point to read the chapter where he explains how the api spy is implemented. HTH. ---------------------------------------------------------------------- jamie · 2023-08-27 02:31 Just what I was looking for, code that won't allow the user to delete malware and maybe some advertising apps too. ---------------------------------------------------------------------- Fibonacci · 2023-08-27 02:46 You dont know what are you doing (well you used ChatGPT so its obvious). You need to run you code INSIDE TARGET PROCESS, so you need to INJECT your code there. Easiest way to do it is to inject DLL. I will give you an example how it should look like, this is the code FOR THE DLL, you need to make injector sepearately and inject this DLL to target process. Look how simple it is with DDetours I linked to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$modeswitch anonymousfunctions} uses Windows, DDetours; var  proc, trampoline: pointer; begin  proc := GetProcAddress(LoadLibrary('user32.dll'), 'MessageBoxA');   trampoline := InterceptCreate(proc, @function(a: dword; b, c: pchar; d: dword): dword stdcall  type    f = function(a: dword; b, c: pchar; d: dword): dword; stdcall;  var    t: f;  begin    //do some stuff here    //...     //call original function    pointer(t) := trampoline;    result := t(0, 'calling original MessageBoxA', 'HOOKED', 0);  end);   //call MessageBoxA  MessageBoxA(0, 'MessageBoxA call', 'caption', 0);end. ---------------------------------------------------------------------- Jonvy · 2023-08-28 07:08 Hello Fibonacci, I want to try your suggestion as follows, it may seems easier, but I don't know how can I get file delete event? Does windows have this event? Quote from: Fibonacci on August 24, 2023, 03:59:58 pm Open file for read, read all its contents, then watch for changes. If change happens, read more data with already opened handle, keep it all in buffer. If you receive file deleted event then save your buffer to a new file, then start over. ---------------------------------------------------------------------- Fibonacci · 2023-08-28 07:15 Yes, but it is post-factum event. You may lose few last lines of the file. Its very simple with this unit: https://github.com/Wosi/DirectoryWatcher For starters I suggest you create an app that: - monitor events - on file modified event: read whole file and keep it in memory - on file deleted event: save data to a new file Then you can optimize this by reducing IO operations by reading only new data from the file instead of reading whole file every time ---------------------------------------------------------------------- Jonvy · 2023-09-12 03:08 Thanks Fibonacci,I tried DirectoryWatcher, it looks may work. When I test program manually,I just modify the txt file and check it, it's ok. But I met problem in real production. Maybe the log running is too fast. After running not more than 1 minute, my program will be no output, can not catch any event. It reports error. I can not find the reason. Here I attach my program and log files here, can you help me to check it? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit unitMain; {$mode Delphi}{$H+} interface uses  Classes, SysUtils, StrUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  ComCtrls, DirectoryWatcherBuilder, DirectoryWatcherAPI, Dateutils, Streamex; type   { TForm1 }   TForm1 = class(TForm)    memLog: TMemo;    sBarInfo: TStatusBar;    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    procedure OnFileEvent(const FilePath: String; const EventType: TDirectoryEventType);    procedure AppendLog();//append Log    procedure SaveLog();  public   end; var  Form1: TForm1;  DirectoryWatcher: IDirectoryWatcher;  LastSize: Integer;  LastContent,NewContent: ansistring;  Y,Mo,D,H,Mi,S,MS:Word;  iMemo:integer;//count Memo log,clear Memo for better performanceimplementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  FolderToWatch: String;begin  iMemo:=1;   FolderToWatch := 'C:\MES\logs';   DirectoryWatcher := TDirectoryWatcherBuilder                        .New                      .WatchDirectory(FolderToWatch)                        .Recursively(True)                        .OnChangeTrigger(OnFileEvent)                        .Build;   DirectoryWatcher.Start;  memlog.Lines.Add('Watching changes in ' + FolderToWatch);   //Initialize log size;  LastSize:=0; end; procedure TForm1.FormDestroy(Sender: TObject);begin  DirectoryWatcher:=nil;end; procedure TForm1.OnFileEvent(const FilePath: String;  const EventType: TDirectoryEventType);var  EventTypeString: String;begin  iMemo:=iMemo+1;   EventTypeString:='*******************************';  if ContainsText(FilePath,'production.txt') then  //if FilePath='c:\Global_MES\ATraQ\logs\ATraQ_production' then  begin    case EventType of      detAdded: EventTypeString := 'ADDED';      detRemoved: EventTypeString := 'REMOVED';      detModified:        begin        EventTypeString := 'MODIFIED';        AppendLog;        end;    end;  end;   if iMemo=20 then    begin    iMemo:=0;    memlog.Clear;    end;   memlog.Lines.Add(FormatDateTime('yyyy/mm/dd hh:nn:ss ',Now)+FilePath+' Get event,Type: ' + EventTypeString);end;procedure TForm1.AppendLog();var  FileStream: TFileStream;  StreamReader: TStreamReader;begin  // Create TFileStream to open txt file  FileStream := TFileStream.Create('c:\MES\logs\5005_production.txt', fmOpenRead,fmShareDenyNone);  //Start from 0 position  FileStream.Position:=0;   //LastSize:=FileStream.Size;   if (FileStream.Size'' then  FileStream.Read(NewContent[1],FileStream.Size);      LastContent:=NewContent;    finally      // Release TStreamReader    StreamReader.Free;    end;  finally    // Release TFileStream    FileStream.Free;  end; end; procedure TForm1.SaveLog();var  sFile:TFileStream; //Save content to file  strSave:string;begin  DecodeDateTime(Now,Y,Mo,D,H,Mi,S,MS);  strSave:='ATraQ_production'+For matDateTime('yyyy_mm_dd_hh_nn_ss',Now)+IntToStr(MS)+'.txt';  sFile:=TFileStream.Create(strSave,fmCreate);  //AssignFile(sFile,strSave);  memlog.Lines.Add('File '+strSave+' Created!');  try    sFile.Write(LastContent[1],Length(LastContent));  sBarInfo.SimpleText:='Old Log has refreshed,save file:'+strSave+'.';  finally    sFile.Free;  end;end; end.   Thanks! ---------------------------------------------------------------------- Fibonacci · 2023-09-12 19:16 Compile it with debug info so you can see on which line is the error. You dont do error checking here: FileStream := TFileStream.Create('c:\MES\logs\5005_production.txt', fmOpenRead,fmShareDenyNone); Maybe at some point your app is not able to open the file and that causes the error, so put it in try-except block. You get the modified event but its for file that contains "production.txt" in the name, but the filename can be different than your filename in AppendLog (5005_production.txt), so.. 1) Add a path String parameter to your AppendLog, then when you call it in OnFileEvent, do AppendLog(FilePath) 2) Inside AppendLog, first thing to do is to check for file existence, then try to open it but as I said with try-except block 3) Inside AppendLog, instead of using static path, do TFileStream.Create(path) AppendLog: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.AppendLog(path: String);var  FileStream: TFileStream;  StreamReader: TStreamReader;begin  if not FileExists(path) then exit;   try    // Create TFileStream to open txt file    FileStream := TFileStream.Create(path, fmOpenRead,fmShareDenyNone);    //Start from 0 position    FileStream.Position:=0;     //REST OF YOUR CODE HERE  except  end;end; OnFileEvent: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       detModified:        begin        EventTypeString := 'MODIFIED';      AppendLog(FilePath);         end; ---------------------------------------------------------------------- Jonvy · 2023-09-13 07:22 Thanks Fibonacci, it works now. But during running, occasionally it catchs error: Unable to open file "C:\MES\logs\5005_production.txt":The process cannot access the file because it is being used by another process. Maybe it comes from the main program itself, it doesn't share the file during some operation, is it correct? ---------------------------------------------------------------------- Fibonacci · 2023-09-13 07:30 Its fine (if its catched by try-except and not causing app to crash). Another process is currently writing to the file and locked it, you will read it on next event. Optionally you can add little Sleep, say 250ms in your AppendLog procedure, just after "begin". ---------------------------------------------------------------------- Jonvy · 2023-09-14 05:14 Ok, thanks again, the program work fine, I'll continuely test it in production.