错误做法:
WideCharToMultiByte(CP_ACP,0,strInput,strInput.GetLength() ,p,len ,NULL,NULL);
正确做法:
WideCharToMultiByte(CP_ACP,0,strInput,strInput.GetLength() + 1,p,len + 1,NULL,NULL);
或者:
WideCharToMultiByte(CP_ACP,0,strInput,-1,p,len + 1,NULL,NULL);
//第4个参数为-1时,字符串strInput将被设定为以NULL为结束符的字符串,并且自动计算长度。
乱码原因:str.GetLength()不计算结束符,因此正确做法要 +1.