Converting string to tchar in VC++

User123422 picture User123422 · Dec 8, 2013 · Viewed 13.5k times · Source

how I can convert string to tchar in VC++?

string internetprotocol="127.4.5.6";

 TCHAR szProxyAddr[16]; 

i want to set:

szProxyAddr=internetprotocol;

how i can do it?

Answer

Alex F picture Alex F · Dec 8, 2013
#include <atlstr.h>


string internetprotocol="127.4.5.6";
TCHAR szProxyAddr[16]; 

_tcscpy_s(szProxyAddr, CA2T(internetprotocol.c_str()));

_tcscpy_s is generic strcpy version which works both in Unicode and Multi-Character configurations. CA2T converts const char* to TCHAR*, according to szProxyAddr variable type.

Be careful with destination variable length.