源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C++中char*转换为LPCWSTR的解决方案

  • 时间:2022-04-19 02:30 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C++中char*转换为LPCWSTR的解决方案
[b]前言[/b] 大家在学习或者使用Windows编程中,经常会碰到字符串之间的转换,char*转LPCWSTR也是其中一个比较常见的转换。下面就列出几种比较常用的转换方法。大家可以根据自己的需求选择相对应的方法,下面来一起学习学习吧。 [b]1、通过MultiByteToWideChar函数转换 [/b] MultiByteToWideChar函数是将多字节转换为宽字节的一个API函数,它的原型如下:
int MultiByteToWideChar( 
 UINT CodePage,   // code page 
 DWORD dwFlags,   // character-type options 
 LPCSTR lpMultiByteStr, // string to map 
 int cbMultiByte,  // number of bytes in string 
 LPWSTR lpWideCharStr, // wide-character buffer 
 int cchWideChar  // size of buffer 
); 
LPCWSTR实际上也是CONST WCHAR *类型
  char* szStr = "测试字符串"; 
WCHAR wszClassName[256]; 
memset(wszClassName,0,sizeof(wszClassName)); 
MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName, 
 sizeof(wszClassName)/sizeof(wszClassName[0])); 
[b]2、通过T2W转换宏[/b]
  char* szStr = "测试字符串";  
CString str = CString(szStr); 
USES_CONVERSION; 
LPCWSTR wszClassName = new WCHAR[str.GetLength()+1]; 
wcscpy((LPTSTR)wszClassName,T2W((LPTSTR)str.GetBuffer(NULL))); 
str.ReleaseBuffer(); 
[b]3、通过A2CW转换[/b]
char* szStr = "测试字符串";  
CString str = CString(szStr); 
USES_CONVERSION; 
LPCWSTR wszClassName = A2CW(W2A(str)); 
str.ReleaseBuffer(); 
上述方法都是UniCode环境下测试的。 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部