<span style="font-family:courier new,monospace">Hi Sven,<br><br>{$mode objfpc}{$H+}<br> TConfiguration = packed record<br>
DataFolder: string;<br>
end;<br>
<br>does not work, but:<br><br> TConfiguration = packed record<br>
DataFolder: array [0..MaxPathLen] of Char;<br>
end;<br>
<br>worked. My save procedure is:<br><br>function SaveParams(var Buffer; Count: Integer; FileName: string): Boolean;<br>var<br> fs: TFileStream;<br>begin<br> Result := False;<br> if not ForceDirectories(ExtractFilePath(FileName)) then Exit;<br>
try<br> fs := TFileStream.Create(FileName, fmCreate);<br> try<br> fs.Write(Buffer, Count);<br> Result := True;<br> finally<br> fs.Free;<br> end;<br> except<br> end;<br>end;<br><br>It is called on the finalization section of config.pas:<br>
<br>SaveParams(cfg, SizeOf(cfg), cfn); <br>//cfg is TConfgiruation, cfn is the filename.<br><br>The symptoms of error are:<br>1. The DataFolder value is not saved to configuration file, although file saving seems succeeded.<br>
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).<br><br>Anything that I did wrong? or, AnsiString is not supported this way anyway?<br>
<br></span><div class="gmail_quote">2013/3/3 Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 03.03.2013 10:57, Xiangrong Fang wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Ok, My purpose is to write the record into stream, so I cannot use<br>
variable length. Now I use an array [0..MaxPathLen] of Char.<br>
</blockquote>
<br></div>
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).<br>
<br>
E.g.<br>
<br>
=== code begin ===<br>
<br>
type<br>
TConfiguration = packed record<br>
//<br>
end;<br>
<br>
=== code end ===<br>
<br>
See also here: <a href="http://community.freepascal.org/docs-html/ref/refsu15.html" target="_blank">http://community.freepascal.<u></u>org/docs-html/ref/refsu15.html</a><div class="HOEnZb"><div class="h5"><br>
<br>
Regards,<br>
Sven<br>
<br>
--<br>
______________________________<u></u>_________________<br>
Lazarus mailing list<br>
<a href="mailto:Lazarus@lists.lazarus.freepascal.org" target="_blank">Lazarus@lists.lazarus.<u></u>freepascal.org</a><br>
<a href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://lists.lazarus.<u></u>freepascal.org/mailman/<u></u>listinfo/lazarus</a><br>
</div></div></blockquote></div><br>