char与TCHAR之间的转化主要用到函数MultiByteToWideChar和WideCharToMultiByte.
char转TCHAR
如果不是Unicode字符集,就不需要转换,直接复制即可,如果不确定是否使用Unicode字符集,可以这样写:
char strUsr[10] = "Hello";
TCHAR Name[100];
#ifdef UNICODE
MultiByteToWideChar(CP_ACP, 0, strUsr, -1, Name, 100);
#else
strcpy(Name, strUsr);
#endif
TCHAR转char
char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn)
{
LPSTR pszOut = NULL;
if (lpwszStrIn != NULL)
{
int nInputStrLen = wcslen (lpwszStrIn);
// Double NULL Termination
int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char [nOutputStrLen];
if (pszOut)
{
memset (pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
}
return pszOut;
}