3

I got a piece of code that works under Multi-Byte Character Set. However, I want to convert this piece of code to UNICODE. So I fixed lots of stuff, but failed at the strncpy() line. This is the line that I want to change:

strncpy(a.szTip, szToolTip, bLength);

I used lots of functions to fix this, but all of them failed.

Here's the error:

strncpy cannot convert parameter 1 from WCHAR[128] to char *
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • 1
    strncpy() will copy bytes until it finds a byte with the value 0 (or reaches the specified maximum number of bytes), and then stop copying. So even if you cast the parameters to force it to compile, it probably wouldn't do what you want -- in particular, any multi-byte character that contained a zero for one of its bytes would cause strncpy() not to copy the remainder of the string. Perhaps you want to use wstrncpy() instead? – Jeremy Friesner Sep 06 '12 at 16:13

1 Answers1

8

When dealing with wide characters, use wcsncpy() (and other relevant functions).

As Remy Lebeau points out, using the _tcsncpy() macro is safer if you want to support both ANSI & Unicode builds (it expands to the right function respectively).

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123