Search code examples
c++socketssendwidestringtchar

Sending TCHAR buffer with send(sock, wszBuffer, ...)?


I have a wide-character XML message that I need to send over a Win32 socket in C++.

TCHAR wszBuffer[1024];

Should I sprintf(szSendBuffer, "%S", wszBuffer) the wide character buffer to a char array before sending it?

What is the correct way to send this?


Solution

  • Pass wszBuffer directly to the socket function, casting it to char*, with length = sizeof(wszBuffer), or 1024 * sizeof(TCHAR). In this case the buffer will be send as is.

    If you want to send this text as ANSI string, convert it by any string convertion function. sprintf is OK, another way is W2A. But some information may be lost.