Microsoft KB Archive/828649

= BUG: Cast from CListCtrl to CListView is not valid with Microsoft Foundation Classes (MFC) version 7.0 and version 7.1 in Visual C++ .NET or MFC version 8.0 in Visual C++ 2005 =

Article ID: 828649

Article Last Modified on 12/30/2005

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.



SYMPTOMS
When you use Microsoft Foundation Classes (MFC) to create the CListCtrl object by using the Create method of the CListCtrl class, the application may not respond. Or, the application may throw Debug Assertion Failure messages.

When you implement this same application, and you use MFC version 4.2 in Microsoft Visual Studio 6.0, the application throws Debug Assertion Failure messages. However, when the assertion message is ignored, the application creates an Application window, and then a List Control object is created in the Application Window.



CAUSE
The following code sample implementation was valid with MFC 4.2: _AFXCVIEW_INLINE CListCtrl& CListView::GetListCtrl const  { return *(CListCtrl*)this; } However, the previous code sample implementation is no longer valid in MFC version 7.0, in MFC version 7.1, and in MFC version 8.0. The CListCtrl::Create method is not a virtual function in MFC version 4.2. However, the CListCtrl::Create method is a virtual function in MFC version 7.0 and later.

When the Create method is called on a CListCtrl object, the application may crash. This problem occurs because the vtable entry is not correct. The code does not run because the Create functions of all the classes that are derived from the CWnd class are changed to virtual functions in MFC version 7.0, in MFC version 7.1, and in MFC version 8.0.



WORKAROUND
To work around this problem, use the Create method of the CListView object to create the List Control object. To do this, follow these steps:  Start Microsoft Visual Studio .NET 2002, Visual Studio .NET 2003, or Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual C++ Projects.

Note In Visual Studio 2005, click Visual C++ under Project Types. Under Templates, click MFC Application. In the Name text box, type MFCProject . In the Location text box, type C:\Test, and then click OK.</li> In the MFC Application Wizard - MFCProject dialog box, click Application Type.</li> Under Application type, click Dialog based, and then click Finish.</li> In the MFCProject project folder, expand the folders.</li> In the Source Files folder, double-click the MFCProjectDlg.cpp file.</li>  After the #include statements, add the following code at the top of the code window: Note You must include the Afxcview.h file after you include the Stdafx.h file. </li> In the upper-right part of the code window, click OnInitDialog in the Event list.</li>  Add the following code before the return statement in the function code: CListView* v= new CListView; CListCtrl& c= v->GetListCtrl; //c.Create(WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999); v->Create (NULL, &quot;Hi&quot;,WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999); Note In Visual Studio 2005, change v->Create (NULL, &quot;Hi&quot;,WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999); to v->Create (NULL, L&quot;Hi&quot;,WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999);. </li> Press CTRL+SHIFT+B to build the application.</li> Press F5 to run the application in debug mode.
 * 1) include &quot;afxcview.h&quot;

The application runs. The List Control object is created in the Application window.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to reproduce the problem
<ol> Complete step 1 to step 13 in the &quot;Workaround&quot; section.</li>  Uncomment the following line of code that you added to step 13 of the &quot;Workaround&quot; section: //c.Create(WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999); </li>  Comment the following line of code that you added to step 13 of the &quot;Workaround&quot; section: v->Create (NULL, &quot;Hi&quot;,WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999); </li> Press CTRL+SHIFT+B to build the application.

The application builds successfully. There are no compilation errors.</li> Press F5 to run the application in debug mode.

The application throws Debug Assertion Failure messages. The Application window is not created even when you ignore the asserts.</li></ol>

Additional query words: CListCtrl CListView Invalid Cast GetListCtrl Create AfxCview Debug Assertion Failure vtable entry

Keywords: kbbug kbctrlcreate kbcontrol KB828649

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.