VC 6++: CString and GetLength () behaviour

I have the following code to get one filename from a listbox control that displays a directory.

void CDOSDlg::OnDblclkList1()
{
// TODO: Add your control notification handler code here

CString str, str2;
int n;

for (int i=0;i < m_List.GetCount();i++)
{
if (m_List.GetSel( i ) > 0)
break;
}
n = m_List.GetTextLen( i );
m_List.GetText( i, str.GetBuffer(n) );

i = str.GetLength();
str2 = str.Right (i - 25);

AfxMessageBox (str2);
}

All is running well, I can get the expected string, i.e. str. But str.GetLength returns 0 (seen in the watch debug window) instead of the expected value.
Also, all CString functions like MID, LEFT, do not work. I suspect its because of the format of the CString. How could I resolve this?
[874 byte] By [nugroho2] at [2007-11-20 11:04:18]
# 1 Re: VC 6++: CString and GetLength () behaviour
Two things: When you create a buffer for a string include room for '\0' and call ReleaseBuffer() after buffer has been filled.

m_List.GetText( i, str.GetBuffer(n+1) );
str.ReleaseBuffer();
i = str.GetLength();
0xC0000005 at 2007-11-10 22:27:11 >
# 2 Re: VC 6++: CString and GetLength () behaviour
str.GetBuffer(n)
str.ReleaseBuffer (); // this sets the internal length of the new string
Skizmo at 2007-11-10 22:28:16 >
# 3 Re: VC 6++: CString and GetLength () behaviour
Thanks so much, folks ... It works now.

Just wondering, why they didn't automatically incorporate that line into the program if we have to use that line every time we call GetText.
nugroho2 at 2007-11-10 22:29:14 >