Menu

Small bug in CWnd::GetDlgItemText

2024-10-11
2024-10-12
  • John Slagel

    John Slagel - 2024-10-11

    If length returns 0 from ::GetWindowTextLength the code isn't happy with creating zero length strings. Wrapping an if (length > 0) around the creation of the new string fixes this.

        // Retrieves the title or text associated with a control in a dialog box.
        inline CString CWnd::GetDlgItemText(UINT dlgItemID) const
        {
            assert(IsWindow());
    
            int dlgItem = static_cast<int>(dlgItemID);
            int length = ::GetWindowTextLength(::GetDlgItem(*this, dlgItem));
            CString str;
            if ( length > 0 )   {
                VERIFY(::GetDlgItemText(*this, dlgItem, str.GetBuffer(length), length+1));
                str.ReleaseBuffer();
            }
            return str;
        }
    
     
  • David

    David - 2024-10-12

    Hi John,

    Thanks for bringing this to my attention.

    I've submitted an update that includes the fix for GetDlgItem. You can download the latest code snapshot from the Code section here on SourceForge.

    Best regards,
    David

     

Log in to post a comment.