0

I need to sign an .exe file with ..\x64\signtool.exe. However, this obviously does not work while the .exe program is running which assumingly locks the .exe file. So I try to detect whether the .exe file is locked with this function:

function IsExeFileLocked(const AFilename: string): Boolean;
var
  F: TFileStream;
begin
  try
    F:=TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
    try
      Result:=False;
    finally
      F.Free;
    end;
  except
    Result:=True;
  end;
end;

But this function always gives back False, even if the .exe program is running!

So how can I detect whether an .exe file is locked by running?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 2
    You appear to be asking the wrong question. Since you attempt to sign an executable, just sign that executable. If it fails, implement appropriate recovery strategies. Singling out a single cause of failure doesn't look like a tractable solution. Introducing a TOCTTOU race isn't going to prove advantageous either. – IInspectable Sep 13 '18 at 11:40
  • you not need detect this. simply try do with file what you need todo. if file is locked - you got relevant error – RbMm Sep 13 '18 at 11:41
  • So is there no way to detect whether exe file is locked by running? – user1580348 Sep 13 '18 at 11:49
  • Say for example the exe file is very large. In this case it takes a relatively long time before the signtool failes. Asking whether the exe file is locked by running would be much faster. – user1580348 Sep 13 '18 at 11:51
  • @user1580348 - but when you try do something with it (say open for write) and give error sharing violation - this is detect – RbMm Sep 13 '18 at 11:52
  • 1
    Your code tries to open the executable for reading. Of course this succeeds even if the executable is running. The system doesn't deny reading. – zett42 Sep 13 '18 at 11:58
  • You may enumerate all running processes to find out if your executable is running. See [this SO article](https://stackoverflow.com/questions/1102407/enumerate-running-processes-in-delphi) to find out how to do this. – Willy K. Sep 13 '18 at 13:07
  • @WillyK. This does not work in all cases. I have even tried with GetModuleFileNameEx which in many cases works only if the app has elevated privileges. I have been behind this for a full day of intensive research. - I will follow the advice of IInspectable. – user1580348 Sep 13 '18 at 13:25
  • @WillyK. The EXE may also be locked for other reaons than running. E. g. an antivirus program scanning it. – zett42 Sep 13 '18 at 14:37
  • *Say for example the exe file is very large. In this case it takes a relatively long time before the signtool failes.* - no. signtool fail just - when it try open file with write access. as result size of file not play role at all here – RbMm Sep 13 '18 at 15:15
  • @RbMm You are right. – user1580348 Sep 13 '18 at 17:07
  • signtool.exe will return an error code to the caller when signing fails for any reason. Use this returned code to correctly handle the failure. – NineBerry Sep 13 '18 at 17:26

0 Answers0