vs2008 unicode工程问题集

1。“void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char [11]”转换为“const wchar_t *”
出错处: CString strConnect;
strConnect.Format(_T("DSN=Hotel;"));该代码在vc6.0上是没有问题的,在vs2008上就出现上面的问题,解决方法:
解决方法:字符集问题,VC6工程默认是多字节的,VS2008默认是Unicode的。
如:Format ("%s",str);
修改为:Format (L"%s",str)或Format (_T("%s"),str)就可以了。
2.       "无法从“const char [11]”转换为“ATL::CStringT<BaseType,StringTraits>”"
解决方法:先定义,另外赋值。
如:CString str = "模式对话框";

修改为:CString str;str = "模式对话框";或CString str("模式对话框");就可以了啊。

3、 Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2664: “sprintf”: 不能将参数 2 从“const char”转换为“LPCWSTR”与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换


问题的原因是字符串ANSI和Unicode编码的区别: VC6与VS2003等默认使用ANSI编码,而VS2008默认采用Unicode。简单的说,ANSI用1个字节表示字符,Unicode用2个字节表示1个字符。


在VS2008中,设置如下:


a、alt+F7,进入工程属性设置菜单。

b、在左边下拉菜单中,选择configuration properties --> General --> 在左边project default 中character set中设置 not set。

c、OK! 错误消失。

4、使用TCHAR 代替char。

5、无法打开包括文件:"xxx.h": No such file or directory

如果是自己的函数:检查是否使用#include <xxx.h>改为#include "xxx.h"

如果是系统的函数:请使用#include <xxx>


另外附上unicode工程常需要的字符集转换方法:

util.h

#include <string>

char* cstring2pchar(CString strData)
{
	int nLength = strData.GetLength();
	//宽字符(unicode)转换为多字节(char*),首先获取长度
	int nBytes = WideCharToMultiByte(CP_ACP,0,strData,nLength,NULL,0,NULL,NULL);
	char* VoicePath = new char[ nBytes + 1];
	memset(VoicePath,0,nLength + 1);
	//真正进行转换
	WideCharToMultiByte(CP_OEMCP, 0, strData, nLength, VoicePath, nBytes, NULL, NULL); 
	VoicePath[nBytes] = 0;
	return VoicePath;
}
LPWSTR string2Unicode(string strData)
{
	int unicode_size = MultiByteToWideChar(CP_ACP, 0,strData.c_str(), -1, NULL, 0);
	LPWSTR wstr = new WCHAR[unicode_size + 1];

	wstr[unicode_size]=0;

	MultiByteToWideChar(CP_ACP, 0, strData.c_str(), -1, wstr, unicode_size);
	return wstr;

}

std::string utf2ansi(LPCSTR pszSrc, int nLen)
{
	int nSize = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pszSrc, nLen, 0, 0);
	if(nSize <= 0) return NULL;
	WCHAR *pwsz = new WCHAR[nSize+1];
	if( NULL == pwsz) return NULL;
	MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)pszSrc, nLen, pwsz, nSize);
	pwsz[nSize] = 0;
	char *psz = new char[nSize+1];
	WideCharToMultiByte(CP_ACP, 0, pwsz, nSize, psz, nSize, NULL, NULL);
	string str = psz;
	delete pwsz;
	delete psz;
	return str;
}

LPWSTR AnsiToUnicode(LPCSTR astr)
{
	int unicode_size = MultiByteToWideChar(CP_ACP, 0, astr, -1, NULL, 0);
	LPWSTR wstr = new WCHAR[unicode_size + 1];

	wstr[unicode_size]=0;

	MultiByteToWideChar(CP_ACP, 0, astr, -1, wstr, unicode_size);
	return wstr;
}
/**
LPCTSTR ansi2Char(string data)
{
	

	int nSize = WideCharToMultiByte(CP_ACP, 0, NULL, nSize, (LPTSTR)data.c_str(), 0, NULL, NULL);
	if(nSize <= 0) 
		return NULL;

	char *psz = new char[nSize+1];
	WideCharToMultiByte(CP_ACP, 0, NULL, nSize, psz, nSize, NULL, NULL);
	string str = psz;


	WCHAR *pwsz = new WCHAR[nSize+1];
	if( NULL == pwsz) 
		return NULL;
	MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)psz, nSize, pwsz, nSize);
	pwsz[nSize] = 0;

	delete pwsz;
	delete psz;
	return pwsz;
}

*/
std::string WChar2Ansi(LPCWSTR pwszSrc)
{
	int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
	if (nLen<= 0) return std::string("");
	char* pszDst = new char[nLen];
	if (NULL == pszDst) return std::string("");
	WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
	pszDst[nLen -1] = 0;
	std::string strTemp(pszDst);
	delete [] pszDst;
	return strTemp;
}
string ws2s(wstring& inputws){ return WChar2Ansi(inputws.c_str()); }

//Converting a Ansi string to WChar string
std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
{
	int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
	if(nSize <= 0) return NULL;
	WCHAR *pwszDst = new WCHAR[nSize+1];
	if( NULL == pwszDst) return NULL;
	MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
	pwszDst[nSize] = 0;
	if( pwszDst[0] == 0xFEFF) // skip Oxfeff
		for(int i = 0; i < nSize; i ++) 
			pwszDst[i] = pwszDst[i+1]; 
	wstring wcharString(pwszDst);
	delete pwszDst;
	return wcharString;
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值