看来char*与char[]是两种不同的字符串。废话少说先上函数代码:
void W2C(const wchar_t* pwstr,char* pcstr,size_t len)
{
int nlength=wcslen(pwstr);
unsigned int nbytes=WideCharToMultiByte(0,0,pwstr,nlength,NULL,0,NULL,NULL);
if (nbytes>len)
nbytes=len;
WideCharToMultiByte(0,0,pwstr,nlength,pcstr,nbytes,NULL,NULL);
}
char* CStringToChar(CString* str)
{
char* ch=0x00; //赋初值避免警告
size_t len=str->GetLength();
wchar_t* wch=str->GetBuffer();
ch=new char[len+1];
W2C(wch,ch,len);
ch[len]='\0';
return ch;
}