问下关于CString类的GetLength的疑问
本帖最后由 ling1874 于 2013-01-11 14:24:43 编辑 代码如下:
TRACE("CStringTest \n");
CString str="";
UCHAR *buf=new UCHAR[1024];
str+=(UCHAR)0;
str+=(UCHAR)0x1;
str+=(UCHAR)0x31;
str+=(UCHAR)0;
str+=(UCHAR)0x99;
memcpy(buf,str.GetBuffer(str.GetLength()),str.GetLength());
for(int i=0;i<str.GetLength();i++)
{
TRACE("0x%02x ",buf[i]);
}
TRACE("\n");
void AppendChar( _In_ XCHAR ch )
{
UINT nOldLength = GetLength();
int nNewLength = nOldLength+1;
PXSTR pszBuffer = GetBuffer( nNewLength );
pszBuffer[nOldLength] = ch;
ReleaseBufferSetLength( nNewLength );
}
void ReleaseBufferSetLength( _In_ int nNewLength )
{
ATLASSERT( nNewLength >= 0 );
SetLength( nNewLength );
}
void SetLength( _In_ int nLength )
{
ATLASSERT( nLength >= 0 );
ATLASSERT( nLength <= GetData()->nAllocLength );
if( nLength < 0
[解决办法]
nLength > GetData()->nAllocLength)
AtlThrow(E_INVALIDARG);
GetData()->nDataLength = nLength;
m_pszData[nLength] = 0;
}
int GetLength() const throw()
{
return( GetData()->nDataLength );
}