CString 强制转化为const char*,作为形参带入函数内出错。用CW2A解决

来源:互联网 发布:python 将list拆分 编辑:程序博客网 时间:2024/06/05 15:39

CString csPath = _T("C:/Program Files/Adobe");

 

将csPath 带入函数:

 

bool GetFileVersion(const char* lpszFileName, bool bVersion); 作为其第一个参数。

 

CString 转为const char* ,加(char *)(LPCTSTR),即:

 

GetFileVersion((char *)(LPCTSTR)csPath , true);

 

但在GetFileVersion函数中调试发现,第一个参数只剩下首地址的一个字符。

 

正确做法如下:(方法1)

CString csPath = _T("C:/Program Files/Adobe");
 CW2A temp1(csPath );
 const char *buffer1 = temp1;
 AppUpdater::GetFileVersion(buffer1, true);

 

(CW2A:将宽字符集(Unicode)转化为多字符集(ASCII);
 CA2W:反之)


MultiByteToWideChar:映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。

WideCharToMultiByte:该函数映射一个unicode字符串到一个多字节字符串。

 

(方法2)

CString str(_T(ansdnandn测试!));
  int len = WideCharToMultiByte( CP_ACP , 0 , str , str.GetLength() , NULL , 0 , NULL , NULL );
  char* pAscii =new char[len+1];
  len = WideCharToMultiByte(  CP_ACP , 0 , str , str.GetLength() , pAscii , len +1 , NULL ,NULL );
  pAscii[len] = 0;

 

  const char* cTemp = (const char*)pAscii;

  char a = cTemp[0];
  char b = cTemp[1];
  char c = cTemp[2];
。。。。。。。