Recent

Author Topic: Combine String and array of WChar  (Read 832 times)

Romain

  • New member
  • *
  • Posts: 20
    • Attribute Changer Website
Combine String and array of WChar
« on: April 15, 2018, 08:00:06 pm »
Hi,

I have an issue combining a String type with an array of wchar.

The WIN32_FIND_DATAW structure has a property called cFilename of type array[MAX_PATH] of WCHAR and in my application I have a variable L_Path of type String.

The cFilename property contains a Non-English Unicode filename and I need to build the complete path :

L_Path :=  IncludeTrailingPathDelimiter(L_Path) + L_WIN32_FIND_DATAW.cFileName

This results in an "Implicit string type conversion with potential data loss from "WideString" to "AnsiString" .. I can confirm that the result is wrong.

How can I combine both without data loss ?

Many thanks,
Romain
 

jamie

  • Hero Member
  • *****
  • Posts: 967
Re: Combine String and array of WChar
« Reply #1 on: April 15, 2018, 09:16:19 pm »
Have you tried using a UTF8String instead?

It will still maintain a Byte wide string but will contain code points >= 128 if there.

totya

  • Sr. Member
  • ****
  • Posts: 453
Re: Combine String and array of WChar
« Reply #2 on: April 15, 2018, 09:19:45 pm »
Code: Pascal  [Select]
  1. L_Path :=  IncludeTrailingPathDelimiter(L_Path) + L_WIN32_FIND_DATAW.cFileName

Hi!

I'm not a codepage expert, but:

Code: Pascal  [Select]
  1. L_Path :=  IncludeTrailingPathDelimiter(L_Path) + UTF8Encode(UnicodeString(L_WIN32_FIND_DATAW.cFileName));

Romain

  • New member
  • *
  • Posts: 20
    • Attribute Changer Website
Re: Combine String and array of WChar
« Reply #3 on: April 15, 2018, 09:23:29 pm »
Have you tried using a UTF8String instead?

It will still maintain a Byte wide string but will contain code points >= 128 if there.

I tried L_Path :=  IncludeTrailingPathDelimiter(L_Path) + UTF8String(L_WIN32_FIND_DATAW.cFileName) and it works fine  :)

Thank you !

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 510
    • Lebeau Software
Re: Combine String and array of WChar
« Reply #4 on: April 16, 2018, 10:07:47 pm »
I tried L_Path :=  IncludeTrailingPathDelimiter(L_Path) + UTF8String(L_WIN32_FIND_DATAW.cFileName) and it works fine  :)

That depends on how you use L_Path.  Windows doesn't support UTF-8 encoded paths, only ANSI and UTF-16 encoded paths.  So, unless L_Path is also declared as UTF8String, and IncludeTrailingPathDelimiter() doesn't modify the contents of L_Path if it has non-ASCII characters in it, and L_Path is converted back to UTF-16 when passed to Win32 APIs, then there is still quite a potential for data loss in the above code snippet.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)