Name
WideString Type
Syntax
type WideString;
Description
WideString is similar to
AnsiString, but instead of storing a string of
AnsiChar characters, it stores a Unicode string of
WideChar (16-bit) characters.
WideString keeps track of its length and
automatically appends a #0 character to the end of
the string so you can easily cast it to
PWideChar.
Internally, Delphi stores a WideString as a
pointer to a record, except that the pointer actually points to the
Data member, and the Length is
stored in the four bytes preceding the WideString
pointer.
type
// This is the logical structure of an WideString, but the
// declaration below is descriptive and cannot be compiled.
TWideString = record
Length: LongWord;
Data: array[1..Length+1] of WideChar;
end;Tips and Tricks
Like
AnsiString, Delphi automatically manages the memory forWideStringvariables. UnlikeAnsiString,WideStringdoes not have a reference count, so every assignment of aWideStringvalue results in a complete copy of the string. Thus, usingWideStringis less efficient than usingAnsiString.Delphi automatically converts between
AnsiStringandWideString, fromPWideChartoWideString, and from a zero-based array ofWideChartoWideString.You can cast a
WideStringtoPWideChar, which is often required for calling Windows API functions, but you must take the same care you take when casting anAnsiStringtoPChar. Delphi automatically frees the string when the string is no longer needed, but this also invalidates thePChar ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access