char与TCHAR相互转化

char与TCHAR相互转化

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;
}

  

原文地址:https://www.cnblogs.com/meizixiong/p/4113477.html