上下文环境:wchPassword是一个宽字符的字符串,想要转换成窄的被老代码调用。
代码:
char *chCKeyPassWord;
int nCKeyLength = WideCharToMultiByte(CP_ACP,0,wchPassword,-1,chCKeyPassWord,0,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,wchPassword,-1,chCKeyPassWord,nCKeyLength ,NULL,NULL);
编译环境:vs2010
第一次调用WideCharToMultiByte,求出字符串长度为7,第二次调用WideCharToMultiByte时候会崩溃。
原因是声明指针时候没有分配空间,使用时候崩溃。
可以换成:
char chCKeyPassWord[20];
或 char *chCKeyPassWord = new char[20]; .......delete[] chCKeyPassWord ;
本文详细介绍了在使用WideCharToMultiByte函数将宽字符字符串转换为窄字符字符串时遇到的问题及解决方案。通过案例分析,阐述了错误的指针声明方式可能导致的内存访问错误,并提供了正确的代码实现方式来避免崩溃。
341

被折叠的 条评论
为什么被折叠?



