[Lazarus] String vs WideString

Juha Manninen juha.manninen62 at gmail.com
Sun Aug 13 12:51:19 CEST 2017


On Sun, Aug 13, 2017 at 1:21 AM, Bo Berglund via Lazarus
<lazarus at lists.lazarus-ide.org> wrote:
> So AnsiString is not safe either....

That is a little misleading.
Actually using the Windows system codepage is not safe any more.
The current Unicode system in Lazarus maps AnsiString to use UTF-8.
Text with Windows codepage must be converted explicitly.
This is a breaking change compared to the old Unicode suppport in
Lazarus 1.4.x + FPC 2.6.x.
The right solution is to use Unicode everywhere. Windows codepages can
be seen as a historical remain, retained for backwards compatibility.
Now is year 2017, Unicode has been used for decades. Everybody should
use it by now.

Marcos Douglas, please change the encoding in your text file to UTF-8.
Every decent text editor, including the editor in Lazarus, has a
feature to do it.
Once the data is Unicode, it is all smooth sailing.
Data is converted between UTF-8 and UTF-16 losslessly.

One more thing:
Data for WideString/UnicodeString parameters in WinAPI functions are
converted automatically. You can ignore the warning or suppress it by
a type cast as Mattias showed.
However for PWideChar parameters you should create an explicit
temporary variable, usually UnicodeString but WideString for OLE.
Assigning to it from your "String" data converts encoding.
Then cast the new variable as the required pointer type.

Juha


More information about the Lazarus mailing list