Inno Setup LoadStringFromFile fails when file is open in another process

后端 未结 2 925
借酒劲吻你
借酒劲吻你 2020-12-20 02:25

To check to see when a database (SQL Anywhere) is fired up and ready to receive requests I am outputting the database message window to a log (text) file and then attempting

2条回答
  •  無奈伤痛
    2020-12-20 03:00

    Use the TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone).

    In Unicode version of Inno Setup (the only version as of Inno Setup 6), its use is tricky due to a bad interface of the class.

    function BufferToAnsi(const Buffer: string): AnsiString;
    var
      W: Word;
      I: Integer;
    begin
      SetLength(Result, Length(Buffer) * 2);
      for I := 1 to Length(Buffer) do
      begin
        W := Ord(Buffer[I]);
        Result[(I * 2)] := Chr(W shr 8); { high byte }
        Result[(I * 2) - 1] := Chr(Byte(W)); { low byte }
      end;
    end;
    
    function LoadStringFromLockedFile(const FileName: string; var S: AnsiString): Boolean;
    var
      Buffer: string;
      Stream: TFileStream;
    begin
      Result := True;
      try
        Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
        try
          SetLength(Buffer, Stream.Size div 2);
          Stream.ReadBuffer(Buffer, Stream.Size);
          S := BufferToAnsi(Buffer);
        finally
          Stream.Free;
        end;
      except
        Result := False;
      end;
    end;
    

    The code is based on TLama's code posted in Read bytes from file at desired position with Inno Setup.

提交回复
热议问题