[Lazarus] LongString does not work?

Sven Barth pascaldragon at googlemail.com
Sun Mar 3 14:23:25 CET 2013


On 03.03.2013 13:49, Xiangrong Fang wrote:
> Hi Sven,
>
> {$mode objfpc}{$H+}
>    TConfiguration = packed record
>        DataFolder: string;
>    end;
>
> does not work, but:
>
>    TConfiguration = packed record
>        DataFolder: array [0..MaxPathLen] of Char;
>    end;
>
> worked.  My save procedure is:
>
> function SaveParams(var Buffer; Count: Integer; FileName: string): Boolean;
> var
>    fs: TFileStream;
> begin
>    Result := False;
>    if not ForceDirectories(ExtractFilePath(FileName)) then Exit;
>    try
>      fs := TFileStream.Create(FileName, fmCreate);
>      try
>        fs.Write(Buffer, Count);
>        Result := True;
>      finally
>        fs.Free;
>      end;
>    except
>    end;
> end;
>
> It is called on the finalization section of config.pas:
>
> SaveParams(cfg, SizeOf(cfg), cfn);
> //cfg is TConfgiruation, cfn is the filename.
>
> The symptoms of error are:
> 1. The DataFolder value is not saved to configuration file, although
> file saving seems succeeded.
> 2. There are weird access violations, e.g. on exiting or on occasions
> that I try to manipulate the configuration at runtime (RunError(203) was
> reported).
>
> Anything that I did wrong? or, AnsiString is not supported this way anyway?

No, AnsiString is not supported in this way. As Jürgen wrote it's 
basically a pointer to the string data and not a static array of the 
string data. And the write and read procedures of TStream (but also the 
file I/O) will write the record's memory as is and thus the value of the 
pointer will be written in case of AnsiString.

Regards,
Sven





More information about the Lazarus mailing list