看來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;
}