关于unicode和多字节字符集的问题

无法支持的类型 2016-09-13 02:06:57
我用多字节字符集调用第三方dll,
错误信息如果用unicode字符集则正常显示。换成多字节字符集会乱码。
但是我里面有些功能必须用多字节字符集才能成功运行。求助大神这应该怎么办?
LIVESCAN_GetErrorInfo(ret,pzError);
str.Format("%s\n",pzError);
m_text=_T(str);
UpdateData(false);
res= 0;

这是获取错误信息代码;能不能告诉我怎么转换字符呢?
...全文
450 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
现在主要问题已经解决。现在结贴
crjwlaq 2016-09-19
  • 打赏
  • 举报
回复
引用 8 楼 xuddk727 的回复:
A2W W2A,用这个
这个用起来简单
赵4老师 2016-09-18
  • 打赏
  • 举报
回复
字符串那些事儿:BSTR-LPSTR-LPWSTR-CString-VARIANT-COleVariant-_variant_t-CComBSTR-_bstr_t http://blog.csdn.net/pizi0475/archive/2010/03/04/5346708.aspx
  • 打赏
  • 举报
回复
引用 12 楼 qq_29700255 的回复:
1.AllocSysString()的返回值是BSTR ,你却将它直接赋给了cstring,也许是因为这个 2.另外说一句,我也是新手,刚学3个月,我也从没用过AllocSysString()是刚刚查的,楼主应该多查查资料
对 因为我这个函数返回值就是BSTR格式的,所以字符串转BSTR好像只能用这个函数。
赵4老师 2016-09-14
  • 打赏
  • 举报
回复
CString::AllocSysString BSTR AllocSysString ( ) const; throw( CMemoryException ); Return Value Points to the newly allocated string. Remarks Allocates a new OLE Automation–compatible string of the type BSTR and copies the contents of the CString object into it, including the terminating null character. A CMemoryException is thrown if insufficient memory exists. This function is normally used to return strings for OLE Automation. Use ::SysFreeString in the rare case that you need to deallocate the returned string. Note You must link with MFCO42D.DLL to obtain this function if you are using MFC in a shared library and building in debug mode. You can cause the linker to get MFCO42D.DLL automatically by including <afxdisp.h> in your STDAFX.H file. For more information about OLE allocation functions in Windows, see::SysAllocString and::SysFreeString in the Win32 SDK OLE Programmer’s Reference. CString Overview | Class Members | Hierarchy Chart See Also ::SysAllocString,::SysFreeString, CMemoryException
  • 打赏
  • 举报
回复
引用 9 楼 qq_29700255 的回复:
http://blog.sina.com.cn/s/blog_73428e9a0102ux6k.html
多转宽,主要是MultiByteToWideChar
至于字符出错这个你自己调试源码看变量值的改变就行了而不是直接要答案

字符出错我调试过程中
一直到红线里面都是正确的。但关键是return以后
应该是AllocSysString这个函数的问题。但我又不知道该怎样改
用户 昵称 2016-09-14
  • 打赏
  • 举报
回复
不用cstring,用char和tchar,完全可控。
OnTheWay_Boy 2016-09-14
  • 打赏
  • 举报
回复
1.AllocSysString()的返回值是BSTR ,你却将它直接赋给了cstring,也许是因为这个 2.另外说一句,我也是新手,刚学3个月,我也从没用过AllocSysString()是刚刚查的,楼主应该多查查资料
OnTheWay_Boy 2016-09-13
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_73428e9a0102ux6k.html 多转宽,主要是MultiByteToWideChar 至于字符出错这个你自己调试源码看变量值的改变就行了而不是直接要答案
许文君 2016-09-13
  • 打赏
  • 举报
回复
A2W W2A,用这个
  • 打赏
  • 举报
回复
来人看一看啊
  • 打赏
  • 举报
回复
嗯 现在这个问题解决掉了。但是还有一个严重的问题
BSTR CDemoViewCtrl::InitFP()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CString str="{ 'code':'1' , 'content':'成功'}";
	BSTR str1;
	CString str3;
	CString str2="{ 'code':'0' , 'content':"+str3+"3}";
	
	// TODO: 在此添加调度处理程序代码
	int ret = mydlg.InitFP();
	if(ret==1){
		
		str1=str.AllocSysString();
	
	}else{
		m_pzErrorInfoEx=mydlg.pzErrorInfo;
		str3=m_pzErrorInfoEx;
		str1=str2.AllocSysString();
		
	}

	TRACE("%p,%s",str1,str1);
	MessageBox(str3);
	return str1;

}
为什么在网页上调用返回的是str1的地址,还有str1的值为什么不是"{ 'code':'0' , 'content':"+str3+"3}";而是"{ 'code':'0' , 'content':3}" 用TRACE打印str1 的值就一个{ 这是为什么?
SureGOGOGO 2016-09-13
  • 打赏
  • 举报
回复
引用 4 楼 wangkuo106 的回复:
[quote=引用 1 楼 lishunihaoa 的回复:] #include <STDLIB.H> #include <LOCALE.H> #include <Windows.h> int main(int argc, char* argv[]) { setlocale(LC_ALL,"");    // 否则无法输出中文 char    ansiStr[] = "这是一个ANSI字符串"; printf( "ansiStr: %s\n", ansiStr ); wchar_t wideStr[] = L"这是一个UNICODE字符串"; wprintf( L"wideStr: %s\n", wideStr ); // 转换ANSI字符串到UNICODE字符串 int len = MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, NULL, 0);  // 先取得转换后的UNICODE字符串所需的长度 wchar_t* buf1 = (wchar_t*)calloc(len, sizeof(wchar_t));         // 分配缓冲区 MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, buf1, len);    // 开始转换 wprintf(L"转换后的UNICODE字符串: %s\n", buf1);                 // 输出转换后的字符串 free(buf1);                                                                             // 释放缓冲区 // 转换UNICODE字符串到ANSI字符串 len = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);  // 先取得转换后的ANSI字符串所需的长度 char* buf2 = (char*)calloc(len, sizeof(char));                                             // 分配缓冲区 WideCharToMultiByte(CP_ACP, 0, wideStr, -1, buf2, len, NULL, NULL);   // 开始转换 printf("转换后的ANSI字符串: %s\n", buf2);                                                 // 输出转换后的字符串 free(buf2);                                                                                                 // 释放缓冲区 return 0; }
嗯 转换成功了。但是我想问一下 我能不能把char* buf2 = (char*)calloc(len, sizeof(char));         声明为全局变量,因为我后面还要调用这个属性。[/quote] 可以啊,不过你自己要记得及时释放该分配的内存。
  • 打赏
  • 举报
回复
引用 1 楼 lishunihaoa 的回复:
#include <STDLIB.H> #include <LOCALE.H> #include <Windows.h> int main(int argc, char* argv[]) { setlocale(LC_ALL,"");    // 否则无法输出中文 char    ansiStr[] = "这是一个ANSI字符串"; printf( "ansiStr: %s\n", ansiStr ); wchar_t wideStr[] = L"这是一个UNICODE字符串"; wprintf( L"wideStr: %s\n", wideStr ); // 转换ANSI字符串到UNICODE字符串 int len = MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, NULL, 0);  // 先取得转换后的UNICODE字符串所需的长度 wchar_t* buf1 = (wchar_t*)calloc(len, sizeof(wchar_t));         // 分配缓冲区 MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, buf1, len);    // 开始转换 wprintf(L"转换后的UNICODE字符串: %s\n", buf1);                 // 输出转换后的字符串 free(buf1);                                                                             // 释放缓冲区 // 转换UNICODE字符串到ANSI字符串 len = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);  // 先取得转换后的ANSI字符串所需的长度 char* buf2 = (char*)calloc(len, sizeof(char));                                             // 分配缓冲区 WideCharToMultiByte(CP_ACP, 0, wideStr, -1, buf2, len, NULL, NULL);   // 开始转换 printf("转换后的ANSI字符串: %s\n", buf2);                                                 // 输出转换后的字符串 free(buf2);                                                                                                 // 释放缓冲区 return 0; }
嗯 转换成功了。但是我想问一下 我能不能把char* buf2 = (char*)calloc(len, sizeof(char));         声明为全局变量,因为我后面还要调用这个属性。
  • 打赏
  • 举报
回复
引用 2 楼 zgl7903 的回复:
WCHAR pzError[1024];
LIVESCAN_GetErrorInfo(ret,pzError);
str = pzError;


不行
zgl7903 2016-09-13
  • 打赏
  • 举报
回复
WCHAR pzError[1024]; LIVESCAN_GetErrorInfo(ret,pzError); str = pzError;
SureGOGOGO 2016-09-13
  • 打赏
  • 举报
回复
#include <STDLIB.H> #include <LOCALE.H> #include <Windows.h> int main(int argc, char* argv[]) { setlocale(LC_ALL,"");    // 否则无法输出中文 char    ansiStr[] = "这是一个ANSI字符串"; printf( "ansiStr: %s\n", ansiStr ); wchar_t wideStr[] = L"这是一个UNICODE字符串"; wprintf( L"wideStr: %s\n", wideStr ); // 转换ANSI字符串到UNICODE字符串 int len = MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, NULL, 0);  // 先取得转换后的UNICODE字符串所需的长度 wchar_t* buf1 = (wchar_t*)calloc(len, sizeof(wchar_t));         // 分配缓冲区 MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, buf1, len);    // 开始转换 wprintf(L"转换后的UNICODE字符串: %s\n", buf1);                 // 输出转换后的字符串 free(buf1);                                                                             // 释放缓冲区 // 转换UNICODE字符串到ANSI字符串 len = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);  // 先取得转换后的ANSI字符串所需的长度 char* buf2 = (char*)calloc(len, sizeof(char));                                             // 分配缓冲区 WideCharToMultiByte(CP_ACP, 0, wideStr, -1, buf2, len, NULL, NULL);   // 开始转换 printf("转换后的ANSI字符串: %s\n", buf2);                                                 // 输出转换后的字符串 free(buf2);                                                                                                 // 释放缓冲区 return 0; }

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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