vs2008 unicode工程问题集

来源:互联网 发布:海洋污染的现状及数据 编辑:程序博客网 时间:2024/05/11 13:06
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 stringstd::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 Oxfefffor(int i = 0; i < nSize; i ++) pwszDst[i] = pwszDst[i+1]; wstring wcharString(pwszDst);delete pwszDst;return wcharString;}