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 (828649)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

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:
  1. Start Microsoft Visual Studio .NET 2002, Visual Studio .NET 2003, or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C++ Projects.

    Note In Visual Studio 2005, click Visual C++ under Project Types.
  4. Under Templates, click MFC Application.
  5. In the Name text box, type MFCProject.
  6. In the Location text box, type C:\Test, and then click OK.
  7. In the MFC Application Wizard - MFCProject dialog box, click Application Type.
  8. Under Application type, click Dialog based, and then click Finish.
  9. In the MFCProject project folder, expand the folders.
  10. In the Source Files folder, double-click the MFCProjectDlg.cpp file.
  11. After the #include statements, add the following code at the top of the code window:
    #include "afxcview.h"
    Note You must include the Afxcview.h file after you include the Stdafx.h file.
  12. In the upper-right part of the code window, click OnInitDialog in the Event list.
  13. 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, "Hi",WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999);
    Note In Visual Studio 2005, change v->Create (NULL, "Hi",WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999); to v->Create (NULL, L"Hi",WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999);.
  14. Press CTRL+SHIFT+B to build the application.
  15. Press F5 to run the application in debug mode.

    The application runs. The List Control object is created in the Application window.

STATUS

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

MORE INFORMATION

Steps to reproduce the problem

  1. Complete step 1 to step 13 in the "Workaround" section.
  2. Uncomment the following line of code that you added to step 13 of the "Workaround" section:
    //c.Create(WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999);
  3. Comment the following line of code that you added to step 13 of the "Workaround" section:
    v->Create (NULL, "Hi",WS_CHILD | WS_VISIBLE,CRect(10,10,200,200),this,9999);
  4. Press CTRL+SHIFT+B to build the application.

    The application builds successfully. There are no compilation errors.
  5. 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.

Modification Type:MajorLast Reviewed:12/30/2005
Keywords:kbCtrlCreate kbControl kbbug KB828649 kbAudDeveloper