How to strncpy() to WCHAR

citizen_of_noobville picture citizen_of_noobville · Sep 6, 2012 · Viewed 7.8k times · Source

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 *

Answer

Michael Foukarakis picture Michael Foukarakis · Sep 6, 2012

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).