Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#1

IniFile ReadBinaryStream bekomme ich nicht in Gange

  Alt 1. Nov 2018, 08:11
Hallo Community,
ich bekomme immer einen "Stream write error" beim Versuch einen Stream aus einer Ini-Datei zu lesen.

Woran kann das liegen? Ich bin mehreren Beispielen gefolgt aber entdecke einfach nicht was da falsch läuft.
Vielleicht hat jemand Rat für mich.

Hier meine bisherige Bastelei:
Delphi-Quellcode:
//unit IniHelper;
uses
  Windows, SysUtils, Classes, SHFolder, IniFiles;

...

function GetSpecialFolderPath( Folder: Integer ): string;
//const SHGFP_TYPE_CURRENT = 0;
var
  Path: Array [0..MAX_PATH] of WideChar;
begin
  if Windows.Succeeded( SHFolder.SHGetFolderPathW( 0, Folder, 0, SHGFP_TYPE_CURRENT, @Path[ 0 ] ) ) then
    Result := Path
   else
    Result := '';
end;

function ReadIniStream( const Filename, Section, Ident: String; const FolderOverride: String = '' ): TStream;
var
  FIniFile: TIniFile;
  Check: Integer;
  FName: String;
  MS: TStream;
begin
  FName := GetSpecialFolderPath( CSIDL_LOCAL_APPDATA );
  if FName = 'then
    Exit;
  if FolderOverride = 'then
    FName := FName + '\' + SysUtils.ChangeFileExt( SysUtils.ExtractFilename( ParamStr( 0 ) ), '.' ) + '\' + Filename
   else
    FName := FName + '\' + FolderOverride + '\' + Filename;
  if not FileExists( FName ) then
    Exit;
  FIniFile := TIniFile.Create( FName );
  try
    MS := TMemoryStream.Create();
    try
      FIniFile.ReadBinaryStream( Section, Ident, MS );
      MS.Position := 0;
      Result.CopyFrom( MS, MS.Size );
      Result.Position := 0;
    finally
      MS.Free;
    end;
  finally
    FIniFile.Free;
  end;
end;

function WriteIniStream( const Filename, Section, Ident: String; const Value: TStream; const FolderOverride: String = '' ): Boolean;
var
  FIniFile: TIniFile;
  FName: String;
begin
  Result := False;
  FName := GetSpecialFolderPath( CSIDL_LOCAL_APPDATA );
  if FName = 'then
    Exit;
  if FolderOverride = 'then
    FName := FName + '\' + SysUtils.ChangeFileExt( SysUtils.ExtractFilename( ParamStr( 0 ) ), '.' )
   else
    FName := FName + '\' + FolderOverride;
  if not SysUtils.ForceDirectories( FName ) then
    Exit;
  if Value.Size = 0 then
    Exit;
  FIniFile := TIniFile.Create( FName + '\' + Filename );
  try
    Value.Position := 0;
    FIniFile.WriteBinaryStream( Section, Ident, Value );
  finally
    FIniFile.Free;
    Result := True;
  end;
end;
und entsprechende Aufrufe
Delphi-Quellcode:
procedure TForm1.btnReadClick(Sender: TObject);
var
  S: String;
  ms: TStream;
begin
  ms := TMemoryStream.Create();
  try
    S := 'Stream-Error';
    ms := IniHelper.ReadIniStream( 'Testfile.cfg', 'Setup', 'StreamEntry' );
    S := IntToStr( ms.Size );
    Memo1.Lines.Add( 'Stream-Size: ' + S );
    ms.Free;
  except
    Memo1.Lines.Add( 'Stream-Size: ' + S );
    ms.Free;
  end;
end;

procedure TForm1.btnWriteClick(Sender: TObject);
var
  s: String;
  ms: TStream;
begin
  try
    ms := TMemoryStream.Create();
    try
      S := 'This is a Stream.';
      ms.WriteBuffer( S[1], Length( S ) * 2 );
      ms.Position := 0;
      if WriteIniStream( 'Testfile.cfg', 'Setup', 'StreamEntry', ms ) then
        begin
          Memo1.Lines.Add( 'StreamEntry Saved.' )
        end
        else
        begin
          Memo1.Lines.Add( 'Error: Testfile.cfg Stream could not be created.' )
        end;
    finally
      ms.Free;
    end;
  except
    Memo1.Lines.Add( 'Error: Fatal Error.' )
  end;
end;
Danke fürs Lesen.

Ps: Speichern klappt, hat nur eine Leseschwäche, genau wie ich :-]
Gruß vom KodeZwerg

Geändert von KodeZwerg ( 1. Nov 2018 um 08:14 Uhr)
  Mit Zitat antworten Zitat