[cpp] 胦랠杞ꊍ涓釜ASIC瀛ꚬ涓插낈瀹藉鞭绗舰寮㾏

Wang Yun a_hao at 21cn.com
Thu Jun 8 21:11:30 CST 2006


王峰 wrote:
> 如题,我看了下mfc中Cstring的源码:
> BSTR CString::AllocSysString() const
> {
> #if defined(_UNICODE) || defined(OLE2ANSI)
>     BSTR bstr = ::SysAllocStringLen(m_pchData, GetData()->nDataLength);
>     if (bstr == NULL)
>         AfxThrowMemoryException();
> #else
>     int nLen = MultiByteToWideChar(CP_ACP, 0, m_pchData,
>         GetData()->nDataLength, NULL, NULL);
>     BSTR bstr = ::SysAllocStringLen(NULL, nLen);
>     if (bstr == NULL)
>         AfxThrowMemoryException();
>     MultiByteToWideChar(CP_ACP, 0, m_pchData, GetData()->nDataLength,
>         bstr, nLen);
> #endif
>
>     return bstr;
> }
>
> 有几点不明白,请各位大虾指点下:
> 第一:为什么使用
> #if defined(_UNICODE) || defined(OLE2ANSI)
> 宏,使用和不使用时有什么区别
> 第二:在 #else 中函数怎么使用了2次函数
> MultiByteToWideChar
>
> -- 
> From 王峰
> ------------------------------------------------------------------------
>
> _______________________________________________
> Cpp mailing list
> Cpp at codingnow.com
> http://codingnow.com/mailman/listinfo/cpp
>   
如果定义了_UNICODE,说明程序支持unicode,这个时候cstring里的数据本来就是 
unicode的,不需要转换。

第一次MultiByteToWideChar是返回转换需要的目标缓冲区大小。



More information about the Cpp mailing list