VC++에서 Unicode/MBCS(Multi-Byte Character Set) 겸용으로 빌드시에 MultiByteToWideChar/WideCharToMultiByte 함수를 아래와 같이 처리하면 문제없이 빌드가 가능하다. 윈도우가 아닌 경우(리눅스 등)에도 빌드 처리가 된다.
#ifdef WIN32
#include <tchar.h>
#endif
#ifdef WIN32
#define STRNCPY _tcsncpy
#define STRLEN _tcslen
#ifdef UNICODE
#define MULTIBYTETOWIDECHAR MultiByteToWideChar
#define WIDECHARTOMULTIBYTE WideCharToMultiByte
#define SNPRINTF swprintf
#define SSCANF swscanf
#else
#define MULTIBYTETOWIDECHAR(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar) \
_snprintf(lpWideCharStr, cchWideChar, "%s", lpMultiByteStr)
#define WIDECHARTOMULTIBYTE(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar) \
_snprintf(lpMultiByteStr, cbMultiByte, "%s", lpWideCharStr)
#define SNPRINTF _snprintf
#define SSCANF sscanf
#endif
#else
#define MULTIBYTETOWIDECHAR(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar) \
snprintf(lpWideCharStr, cchWideChar, "%s", lpMultiByteStr)
#define WIDECHARTOMULTIBYTE(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar) \
snprintf(lpMultiByteStr, cbMultiByte, "%s", lpWideCharStr)
#define STRNCPY strncpy
#define STRLEN strlen
#define SNPRINTF snprintf
#define SSCANF sscanf
#define TCHAR char
#define TEXT(quote) quote
#define _T(quote) quote
#define __T(quote) quote
#endif
< 사용예 >
TCHAR filepathPrefix[260] = { 0 };
TCHAR fileExtension[260] = { 0 };
MULTIBYTETOWIDECHAR(CP_ACP, 0, m_szFilePathPrefix, sizeof(m_szFilePathPrefix), filepathPrefix, sizeof(filepathPrefix));
MULTIBYTETOWIDECHAR(CP_ACP, 0, m_szFileExtension, sizeof(m_szFileExtension), fileExtension, sizeof(fileExtension));
char filepathChar[256];
WIDECHARTOMULTIBYTE(CP_UTF8, 0, filepath, sizeof(filepath), filepathChar, sizeof(filepathChar), NULL, NULL);