我在VS2005里面建了一个控制台项目,设置Project -> Property -> General Charset 为 not set
程序如下:
wchar_t wszTest[100];
wsprintfW(wszTest, L"F:\\Program Files\\Windows NT\\附件");
size_t origsize = wcslen(wszTest) + 1; 
size_t convertedChars = 0; 
char * CharString;
CharString = new char(origsize); 
CString strDebug;
strDebug = "我为我";
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wszTest,-1,NULL,0,NULL,FALSE);
WideCharToMultiByte (CP_OEMCP,NULL,wszTest,-1,CharString,dwNum,NULL,FALSE);
strDebug = CharString;
//strDebug.Format("%S", CharString);
strDebug.Format("%S", CharString);
DEBUG_ADD((LPCTSTR)strDebug);
//strDebug.Format("我的");
return;直接将汉字赋值给CString,strDebug = "我为我" 正常执行
在执行strDebug = CharString; 和strDebug.Format("%S", CharString); strDebug.Format("%S", CharString);
这三处时都会出现异常,我查看了CString在Not Set情况下为TCHAR类型
敢问各位大虾,在Charset选择为Not Set情况下如何解决问题,谢谢

解决方案 »

  1.   

    CharString = new char(origsize); 
    ??CharString = new char[origsize]; WideCharToMultiByte (CP_OEMCP,NULL,wszTest,-1,CharString,dwNum,NULL,FALSE); 
    这句话后,就应该出异常,越界了
      

  2.   

    你的CharString应该根据WideCharToMultiByte 的返回值分配大小,否则你看看dwNum是否大于origsize导致CharString溢出了。
    另外格式化Format参数对于char*应该用%s吧。
      

  3.   

    Format里面大写的%S 和 小写的%s 在NotSet情况下有什么区别
      

  4.   

    如果我把wchar_t写到CString中像如下这样
    strDebug.Format("%S", wszTest);为什么会异常