wcstombs_s 的一个问题

lanhxg 2011-08-01 11:06:56
这是在VS2008 下面的代码:问题如下
//m_csCurrentDir 是 CString 的类型 里面是我的文件目录的字符串:例如E:\FunshionMedia\字典情人\字典情人C.rmvb"
const size_t strSize = (m_csCurrentDir.GetLength()+1)*2;
char * pstr = new char[strSize];
size_t sz=0;
wchar_t *pSctring = m_csCurrentDir.GetBuffer() ;
errno_t test = wcstombs_s(&sz,pstr,strSize,pSctring, strSize);
switch(test)
{
case 0:
MessageBox(L"成功"); break;
case EINVAL:MessageBox(L"mbstr is NULL and sizeInBytes > 0 ; or "
L"wcstr is NULL");
break ;
case ERANGE:MessageBox(L"The destination buffer is too small"
L" to contain the converted string "
L"(unless count is _TRUNCATE; see Remarks below)");
break ;
case EILSEQ: MessageBox(L"Illegal byte sequence");break ; //这是返回的
default:
MessageBox(L"wcstombs_s未成功!");
};

//这个函数没有正确执行 wcstombs_s 返回值是42MSDN上说Illegal byte sequence
//请问我这么办!我试了好多方法了,就是没办法把 CString 的字符串转换到const char * 类型
//转换要么就剩一个字符了!!!求解决方法
...全文
541 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao_no 2013-03-12
  • 打赏
  • 举报
回复
引用 1 楼 fishion 的回复:
简单点,用T2A #include<AtlBase.h> USES_CONVERSION; char *psz = T2A(lpsz);
确实非常好用!赞一个,解决了我一个大问题~好人啊!
fishion 2011-08-01
  • 打赏
  • 举报
回复
CString m_csCurrentDir = _T("124sd3234233dadfklsajdfs");
const size_t strSize = (m_csCurrentDir.GetLength()+1)*2;
char * pstr = new char[strSize];
memset(pstr,0,strSize);
size_t sz=0;
//wchar_t *pSctring = m_csCurrentDir.GetBuffer() ;
errno_t test = wcstombs_s(&sz,pstr,strSize,m_csCurrentDir.GetBuffer(0), strSize);
m_csCurrentDir.ReleaseBuffer();

你要确保你当前工程是Unicode形式
fishion 2011-08-01
  • 打赏
  • 举报
回复
简单点,用T2A
#include<AtlBase.h>

USES_CONVERSION;
char *psz = T2A(lpsz);
lanhxg 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fishion 的回复:]
简单点,用T2A
#include<AtlBase.h>

USES_CONVERSION;
char *psz = T2A(lpsz);
[/Quote]
这个有用。。。。谢了。。。
Eleven 2011-08-01
  • 打赏
  • 举报
回复
CString 的字符串转换到const char * 类型
---------------------
如果你当前是Unicode编码的话,利用WideCharToMultiByte(CP_ACP, ...);
否则的话,直接转就可以了
lanhxg 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fishion 的回复:]
CString m_csCurrentDir = _T("124sd3234233dadfklsajdfs");
const size_t strSize = (m_csCurrentDir.GetLength()+1)*2;
char * pstr = new char[strSize];
memset(pstr,0,strSize);
size_t sz=0;
//wchar_t *……
[/Quote]
//还是一样的错

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧