[Lazarus] LongString does not work?

Xiangrong Fang xrfang at gmail.com
Sun Mar 3 13:49:31 CET 2013


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?

2013/3/3 Sven Barth <pascaldragon at googlemail.com>

> On 03.03.2013 10:57, Xiangrong Fang wrote:
>
>> Ok, My purpose is to write the record into stream, so I cannot use
>> variable length. Now I use an array [0..MaxPathLen] of Char.
>>
>
> If you want to write a complete record to stream (and not write it's
> elements manually) you should declare your record as "packed" as only then
> the records are guaranteed to be the same on different plattforms (with the
> exception of different endianess).
>
> E.g.
>
> === code begin ===
>
> type
>   TConfiguration = packed record
>     //
>   end;
>
> === code end ===
>
> See also here: http://community.freepascal.**
> org/docs-html/ref/refsu15.html<http://community.freepascal.org/docs-html/ref/refsu15.html>
>
>
> Regards,
> Sven
>
> --
> ______________________________**_________________
> Lazarus mailing list
> Lazarus at lists.lazarus.**freepascal.org<Lazarus at lists.lazarus.freepascal.org>
> http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20130303/5a709c84/attachment-0003.html>


More information about the Lazarus mailing list