[fpc-pascal] Converting StrLCopy to widestrings

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Sat Nov 17 10:38:05 CET 2007


Hello,

In a LCL code we have:

var
  FileNameBuffer: PChar;
  FileNameWide: WideString;
  FileNameWideBuffer: PWideChar;
  FilterBuffer: WideString;
begin
{$ifdef WindowsUnicodeSupport}
  if UnicodeEnabledOS then
    FileNameWideBuffer := AllocMem(FileNameBufferLen * 2 + 2)
  else
    FileNameBuffer := AllocMem(FileNameBufferLen + 1);
{$else}
  FileNameBuffer := AllocMem(FileNameBufferLen + 1);
{$endif}

.....
  {$ifdef WindowsUnicodeSupport}
    if UnicodeEnabledOS then
    begin
      FileNameWide := UTF8Decode(FileName);

      { StrLCopy is a PChar function, so it won't create a proper 2-byte
        sized ending and we ensure that it's there by cleaning the string }
      FillChar(FileNameWideBuffer^, FileNameBufferLen * 2 + 2, #0);

      StrLCopy(PChar(FileNameWideBuffer),
PChar(PWideChar(FileNameWide)), FileNameBufferLen * 2);
    end
    else
      StrLCopy(FileNameBuffer, PChar(UTF8ToAnsi(FileName)), FileNameBufferLen);
  {$else}
    StrLCopy(FileNameBuffer, PChar(FileName), FileNameBufferLen);
  {$endif}

...
  {$ifdef WindowsUnicodeSupport}
    if UnicodeEnabledOS then
    begin
      lpStrFile := PChar(FileNameWideBuffer);
.....

Which doesn't work. FileName can be, for example: C:\Programs\file.txt

And then the dialog will show only C, as if it would show only the first letter.

The original code uses StrLCopy to copy a PChar with a maximal length.
I tryed to manually build a StrLCopy for PWideChar, but it would
crash, so I then tryed to improvise an way to use StrLCopy with
PWideChar strings

In fact, there are lot's of places on LCL where the helper PChar
functions are used. Are there PWideChar equivalents??? It really get's
much harder to use PWideChar strings without those helper functions.

thanks,
-- 
Felipe Monteiro de Carvalho



More information about the fpc-pascal mailing list