BUG: HTREEITEM May Cause TypeLoadException in a Managed C++ Application (320082)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article was previously published under Q320082

SYMPTOMS

When you use HTREEITEM in a Managed C++ application, a TypeLoadException exception occurs when HTREEITEM is accessed.

CAUSE

This is caused because the _TREEITEM typedef in the commctrl.h file has no corresponding definition. The managed runtime cannot resolve this undefined type, and throws an exception.

RESOLUTION

Define an empty _TREEITEM structure in the application, such as the following:
struct _TREEITEM
{
};
				

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 Behavior

  1. Start Visual Studio .NET, and create a new MFC Application project.
  2. In the toolbox, click the Dialog Editor tab, and then add a tree control. Leave the control ID, IDC_TREE1, as the default.
  3. Add the following code to the bottom of the OnInitDialog function, just above the line that reads "return TRUE;":
    	CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
    	ASSERT(pCtrl != NULL);
    
    	TVINSERTSTRUCT tvInsert;
    	tvInsert.hParent = NULL;
    	tvInsert.hInsertAfter = NULL;
    	tvInsert.item.mask = TVIF_TEXT;
    	tvInsert.item.pszText = _T("United States");
    
    	HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert);
    
    	HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT,
    		_T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL);
    
    	HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"),
    		0, 0, hCountry, hPA);
    
    	pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT);
    	pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
    	pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT);
    
    	pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT);
    	pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
    	pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT);
    					
    NOTE: This code is taken from the MFC documentation for CTreeCtrl::InsertItem.

  4. Build and run the project. Note that there are no errors, and the tree nodes function. Close the executing program and return to Visual Studio .NET.
  5. On the Project menu, click Properties to open the Properties Pages dialog box.
  6. Under the Configuration Properties folder, click General, and then change Use Managed Extensions from No to Yes.
  7. Under the C/C++ folder, click General, and then change Debug Information Format to Program Database (/Zi).
    NOTE: A managed project does not currently support the Program Database for Edit and Continue (/ZI) option.
  8. Under the C/C++ folder, click Code Generation, and then change Enable Minimal Rebuild to No, and change Basic Runtime Checks to Default.
  9. Click Apply, and then click OK to apply the changes.
  10. Rebuild the project. The /CLR compiler option is now applied to the project, and it is now a managed assembly.
  11. Run the project. A TypeLoadException occurs with a description that begins as follows: "Could not load type _TREEITEM ...".

Modification Type:MinorLast Reviewed:4/30/2003
Keywords:kbbug kbManaged kbTreeView KB320082