PRB: CWnd::GetWindowText(CString &) Asserts w/ Drop-Down Combo

Last reviewed: July 17, 1997
Article ID: Q99199
1.00 1.50 1.51 1.52 WINDOWS kbprg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with: Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51 and 1.52

SYMPTOMS

An attempt to call CWnd::GetWindowText(CString & rString) fails and an assertion occurs in STRCORE.CPP on line 336 if the CWnd object specifies a drop list combobox (a combobox with the CBS_DROPDOWNLIST window style).

The assertion in Visual C++ occurs on line 276 of STRCORE1.CPP.

CAUSE

The CWnd::GetWindowText(CString& ) function calls GetWindowTextLength(). If this function is called for a drop list ComboBox, Windows incorrectly returns -1 (CB_ERR). This is a bug in Windows. Because a CString can't have a length of -1, the application framework of MFC correctly displays an assertion.

RESOLUTION

To work around this problem, use the CWnd::GetWindowText(LPSTR, INT) function; it does not call GetWindowTextLength(). If you want to use a CString in your application, use code like the following:

   CString s;
   char* p = s.GetBuffer(10); // Allocate space for 10 characters.
   pComboBox->GetWindowText(p, 10);
   s.ReleaseBuffer();


Additional reference words: 1.00 1.50 1.51 1.52 2.00 2.50 2.51 2.52
noupdate no32bit
KBCategory: kbprg kbprb
KBSubcategory: MfcUI
Keywords : kb16bitonly MfcUI kbprb kbprg
Technology : kbMfc
Version : 1.00 1.50 1.51 1.52
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.