RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
UnicodeString::t_str Method
Pascal
function t_str: char*;
C++
__fastcall char* t_str();

The default VCL string type is now UnicodeString instead of AnsiString. For backward compatibility, the method System::UnicodeString::t_str returns const char* (instead of const wchar_t*), by narrowing the wide data of the UnicodeString instance. This behavior can be unexpected, because code might not expect a call to t_str() to corrupt the underlying data. You can observe this behavior in code that displays the underlying data in the UI, such as a TListItem.  

For example, in the following case the TCHAR mapping option is set by default to char, and the data displays by the ListView are corrupted after the call to t_str() on the last line of the method:

    void ProcessSelectedItem(const char* item);
    void __fastcall TForm6::ListView1DblClick(TObject *Sender)
    {
      int index = ListView1->Selected->Index;
      TListItem *ClassItem = ListView1->Items->Item[index];
      ProcessSelectedItem(ClassItem->Caption.t_str());
    }

Whenever the TCHAR mapping option is set to char (the default setting), you should not use System::UnicodeString::t_str on fields or properties that do not expect the underlying data to be narrowed. Instead, you can change the behavior of System::UnicodeString::t_str by setting the TCHAR_mapping option to wchar_t. You can make this project setting on the ProjectOptionsDirectories and Conditionals dialog box, and it will ensure that t_str() returns the UnicodeString's wide data without modifying it. 

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!