Inno Setup FileExists unable to find existing file

橙三吉。 提交于 2019-12-06 10:32:50

To debug the issue, try adding following code. Then check the log file of the installer and the output of the dir command:

#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

function GetFileAttributes(lpFileName: string): DWORD;
  external 'GetFileAttributes{#AW}@kernel32.dll stdcall'; 

function GetLastError() : LongInt;
 external 'GetLastError@kernel32.dll stdcall';

const
  INVALID_FILE_ATTRIBUTES = $FFFFFFFF;

procedure ...;
var
  UpdatePath: string;
  ExePath: string;
  FindRec: TFindRec;
  Attrs: DWORD;
  LastError: LongInt;
  ResultCode: Integer;
begin
  Log('InitializeWizard');
  UpdatePath := ExpandConstant('{app}');
  ExePath := UpdatePath+'\Application.exe';

  if FileExists(ExePath) then
  begin
    Log(ExePath + ' exists');
  end
    else
  begin
    LastError := GetLastError;
    Log(ExePath + ' does not exist - '  +
      Format('System Error. Code: %d. %s', [LastError, SysErrorMessage(LastError)]));
  end;

  if not FindFirst(UpdatePath + '\*', FindRec) then
  begin
    LastError := GetLastError;
    Log(UpdatePath + ' not found - ' +
      Format('System Error. Code: %d. %s', [LastError, SysErrorMessage(LastError)]));  
  end
    else
  begin
    repeat
      Log('Found file: ' + FindRec.Name + ' in ' + UpdatePath);
    until not FindNext(FindRec);
  end;

  Attrs := GetFileAttributes(ExePath);
  if Attrs <> INVALID_FILE_ATTRIBUTES then
  begin
    Log(ExePath + ' attributes = ' + IntToStr(Attrs));
  end
    else
  begin
    LastError := GetLastError;
    Log(Format('Cannot get attributes of ' + ExePath + ': System Error. Code: %d. %s', [
          LastError, SysErrorMessage(LastError)]));  
  end;

    Exec(ExpandConstant('{cmd}'), '/k dir "' + UpdatePath + '"', '', SW_SHOW,
      ewWaitUntilTerminated, ResultCode);
end;

The FileExists internally uses FindFirst/FindNext and GetFileAttributes. So this is to find out what causes the problem.


My wild guess is that the target machine is 64-bit and file system redirection jumps in for some reason.

Try using EnableFsRedirection to disable the redirection before you call FileExists:

EnableFsRedirection(False);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!