Microsoft KB Archive/320082

= BUG: HTREEITEM May Cause TypeLoadException in a Managed C++ Application =

Article ID: 320082

Article Last Modified on 4/30/2003

-

APPLIES TO


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

-



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.



Steps to Reproduce the Behavior
 Start Visual Studio .NET, and create a new MFC Application project. In the toolbox, click the Dialog Editor tab, and then add a tree control. Leave the control ID, IDC_TREE1, as the default.  Add the following code to the bottom of the OnInitDialog function, just above the line that reads &quot;return TRUE;&quot;: 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(&quot;United States&quot;);

HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert);

HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT,       _T(&quot;Pennsylvania&quot;), 0, 0, 0, 0, 0, hCountry, NULL);

HTREEITEM hWA = pCtrl->InsertItem(_T(&quot;Washington&quot;),       0, 0, hCountry, hPA);

pCtrl->InsertItem(_T(&quot;Pittsburgh&quot;), hPA, TVI_SORT); pCtrl->InsertItem(_T(&quot;Harrisburg&quot;), hPA, TVI_SORT); pCtrl->InsertItem(_T(&quot;Altoona&quot;), hPA, TVI_SORT);

pCtrl->InsertItem(_T(&quot;Seattle&quot;), hWA, TVI_SORT); pCtrl->InsertItem(_T(&quot;Kalaloch&quot;), hWA, TVI_SORT); pCtrl->InsertItem(_T(&quot;Yakima&quot;), hWA, TVI_SORT); NOTE: This code is taken from the MFC documentation for CTreeCtrl::InsertItem.

 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. On the Project menu, click Properties to open the Properties Pages dialog box.</li> Under the Configuration Properties folder, click General, and then change Use Managed Extensions from No to Yes.</li> 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.</li> Under the C/C++ folder, click Code Generation, and then change Enable Minimal Rebuild to No, and change Basic Runtime Checks to Default.</li> Click Apply, and then click OK to apply the changes.</li> Rebuild the project. The /CLR compiler option is now applied to the project, and it is now a managed assembly.</li> Run the project. A TypeLoadException occurs with a description that begins as follows: &quot;Could not load type _TREEITEM ...&quot;.</li></ol>

Additional query words: HTREEITEM TypeLoadException _TREEITEM

Keywords: kbbug kbtreeview kbmanaged KB320082

-

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

© Microsoft Corporation. All rights reserved.