Deleting exe-file not working anymore in Windows 11 lazarus / 34-Windows ---------------------------------------------------------------------- JanRoza · 2024-09-05 23:58 I have a program that has been doing its job perfectly under Windows until I migrated to Windows 11. It is a small secondary program that checks if the main program has been closed and then renames the old main program and after that renames the new downloaded program file to the original programs name. This has been working under all versions of Windows but fails to delete the old main program in Windows 11. Has anyone experienced the same or know how to improve the code I use for this operation? 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   // Wait until GliderMaintenance is closed  while (GmIsRunning(strProgram) <> 0) do bActive := True;  // GliderMaintenance.exe   // Delete archived previous version  DeleteFile(PChar(strOldProgram));    // GliderMaintenance-old.exe     «-------- This fails in Windows 11   // Rename current main program to old and after that rename new main version to current name  if RenameFile(strProgram, strOldProgram)           // GliderMaintenance.exe -> GliderMaintenance-old.exe  then RenameFile(strNewProgram, strProgram);     // GliderMaintenance.new -> GliderMaintenance.exe   // Start new version of GliderMaintenance  ShellExecute(Handle, 'open', PChar(strProgram), nil, nil, 1);   // Close this update program  Close; ---------------------------------------------------------------------- Wallaby · 2024-09-06 01:47 Depending on how you check if it's running, this code is bad for two reasons: 1) Consuming CPU for no reason for repeated checks (can be thousands per second!) 2) A race condition is possible where your code thinks it's not running, but Windows hasn't finishing unloading and releasing the file Do this instead: 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 while FileExists(strOldProgram) and (not DeleteFile(PChar(strOldProgram))) do Sleep(100); // This will not proceed until the program is closed. ---------------------------------------------------------------------- marcov · 2024-09-06 07:34 This is fairly typical, and not windows 11 specific, e.g. an antivirus can keep the file locked. Or a second instance could be launched(e.g. in an elevated context). Solution is simple, rename to old after a timeout, and check on startup if files need deleting. (could be a problem if it is in an elevated directory though) ---------------------------------------------------------------------- JanRoza · 2024-09-06 22:39 I'm pretty sure that the main program is not running as the 'GmIsRunning(strProgram) <> 0' loop is ended (without any noticable delay). Also this code is running for more than 10 years in all previous versions of Windows and never failed me once. The problem is that the DeleteFile works in Windows < version 11 and does not work in Windows 11. I will try Wallaby's suggestion as well and see if it solves my problem, I will let you know the result. ---------------------------------------------------------------------- JanRoza · 2024-09-06 22:57 Guys, thanks for your advice, they were useful. But the real cause was me myself, I was dumb enough to forget to put the new main program version on the server.  So the process was still working but it updated the main program with the same version instead of the next version, so right after updating and restarting the main program it again showed the message that there was a new version available and asked me if I wanted to update. So apologies for my stupidity and I will try to be more awake next time. ---------------------------------------------------------------------- Remy Lebeau · 2024-09-07 18:37 Some small nitpicks: Quote from: JanRoza on September 06, 2024, 01:58:47 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Wait until GliderMaintenance is closedwhile (GmIsRunning(strProgram) <> 0) do bActive := True;  // GliderMaintenance.exe This is a busy loop that eats up CPU cycles. At least put a small sleep in the loop to yield cycles. A better option would be to open a handle to the process if it is running, and then wait on the handle to be signaled when the process exits.  That way, the wait can sleep the entire time the handle is unsignaled. Quote from: JanRoza on September 06, 2024, 01:58:47 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Delete archived previous versionDeleteFile(PChar(strOldProgram));    // GliderMaintenance-old.exe     «-------- This fails in Windows 11 What is the error code when that happens?  From your latest update, it was probably 2 (ERROR_FILE_NOT_FOUND).  Always check error codes when making system calls! Quote from: JanRoza on September 06, 2024, 01:58:47 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Rename current main program to old and after that rename new main version to current nameif RenameFile(strProgram, strOldProgram)             // GliderMaintenance.exe -> GliderMaintenance-old.exethen RenameFile(strNewProgram, strProgram);     // GliderMaintenance.new -> GliderMaintenance.exe You should consider using the Win32 ReplaceFile() function instead, eg: Code: [Select]ReplaceFile(PChar(strProgram), PChar(strNewProgram), PChar(strOldProgram), 0, nil, nil); Quote from: JanRoza on September 06, 2024, 01:58:47 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Start new version of GliderMaintenanceShellExecute(Handle, 'open', PChar(strProgram), nil, nil, 1); You should be using CreateProcess() instead. ShellExecute() is just going to call it anyway, so you can cut out the middle-man.  Yes, it is slightly more code to use CreateProcess(), but it is less overhead to avoid invoking the Shell. ---------------------------------------------------------------------- JanRoza · 2024-09-07 19:28 Thanks for the tips Remy, I will streamline things following your advice and test how that will work out. ---------------------------------------------------------------------- JanRoza · 2024-11-20 20:24 As I already explained, it was my mistake. Deleting the exe-file works fine so there is nothing left to investgate. ---------------------------------------------------------------------- d2010 · 2025-04-23 16:14 Anyone can test my routine bellow? (only optionally In my computer delete10 work very good, ) You must update at ??? only if you need now now , you need one solution-of DeleteFile. I do not start other topic, Ths topic is already very good inside the "lazarus.Forum.Pascal" Today for win8, win7  the vl_file_delete10 work very good. Foe win8, 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 Var con_systemdrive:char='D';Function vl_file_delete10(exew:word;stroldfile: string): integer;Var toc:integer;begin  Result := RTREJ;  if (length(stroldfile)>2048) then setlength(stroldfile,2048);  if (length(stroldfile)<3) then exit;  result:=RTCAN;    if (stroldfile[01]<='"')or(stroldfile[length(stroldfile)]<='/') then var_trimcen(stroldfile,'/');  sleep(1);  if (stroldfile[length(stroldfile)]='\') then setlength(stroldfile,length(stroldfile)-01);  result:=RTFAIL;  if (length(stroldfile)<3) then exit;  Result:=000;  if (fileexists(strold file)=false)and(stroldfile[2]=':')and(stroldfile[3]='\') then stroldfile[1]:=con_systemdrive;  toc:=(GetTickCount+1000);  while (fileexists(stroldfile))and(deletefile(stroldfile)=false) do    Begin FileSetAttr(stroldfile, 0); ???         sleep(50);         if (toc>GetTickcount) then system.break;    End;   if( fileexists(stdoldfile)) then result:=RTFAIL else result:=RTGOOD;end;  const  RTMODELESS = 5027;  RTNORM=5100;  RTGOOD=5200;  RTERROR = (-5001);  RTCAN = (-5002);  RTREJ = (-5003);  RTFAIL = (-5004);  RTKWORD = (-5005);  RTINPUTTRUNCATED = (-5008);  RTVARSETQ = 5028;