Skip to content

Instantly share code, notes, and snippets.

@RyanJeong
Last active February 16, 2025 15:27
Show Gist options
  • Save RyanJeong/04a4b92879ab10edfd9656d0aa4f3b77 to your computer and use it in GitHub Desktop.
Save RyanJeong/04a4b92879ab10edfd9656d0aa4f3b77 to your computer and use it in GitHub Desktop.
A way to convert char[] to/from TCHAR[] in C/C++(WinAPI)

TCHAR -> char

#define SIZE 1024

TCHAR src[SIZE] = L"This string will be char type";
char  dest[SIZE];

WideCharToMultiByte(CP_ACP, 0, src, lstrlen(src), dest, SIZE, NULL, NULL);

char -> TCHAR

#define SIZE 1024

char  src[SIZE]		= "This string will be TCHAR type";
TCHAR dest[SIZE]	= {0, };

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, strlen(src), dest, SIZE);
@Tal500
Copy link

Tal500 commented Feb 16, 2025

Thank you!
I found it useful to branch on #ifdef _UNICODE for a generic code, i.e.

#ifdef _UNICODE
// use your conversion
#else
// no-op
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment