Hi all,

I'm having a problem with the CTreeCtrl::SortChildrenCB() method. The problem is in my callback compare function. The function itself gets called by the tree's sort routine to compare tree items but some parameters received by the function don't seem to be valid. The function receives 3 parameters:

LPARAM lParam1 - 1st HTREEITEM to compare
LPARAM lParam2 - 2nd HTREEITEM to compare
LPARAM lParamSort - tree control


First, lParamSort gets cast to a CTreeCtrl. This works. The result of this operation is a valid CTreeCtrl pointer. Second, the two tree item handles must be cast to HTREEITEMs in order to access the tree method CTreeCtrl::GetItemText(). This is where the problems occur. The result of the castings seem ok (valid pointers) but the CTreeCtrl::GetItemText() methods return garbage. Also, what's more puzzling is at one point, a handle passed in is negative (which causes an "Access Violation")???

Has anybody ever encountered this problem? What am I not doing with the LPARAMs to access the HTREEITEMs they represent? Thank you.


// Sort the item in reverse alphabetical order.
static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the tree control.
// The lParam of an item is just its handle.

CTreeCtrl* pmyTreeCtrl = (CTreeCtrl*) lParamSort;
CString strItem1 = pmyTreeCtrl->GetItemText((HTREEITEM) lParam1); //returns garbage
CString strItem2 = pmyTreeCtrl->GetItemText((HTREEITEM) lParam2); //returns garbage
return strcmp(strItem2, strItem1);
}

// The pointer to my tree control.
extern CTreeCtrl* pmyTreeCtrl;

TVSORTCB tvs;

// Sort the tree control's items using my
// callback procedure.
tvs.hParent = TVI_ROOT;
tvs.lpfnCompare = MyCompareProc;
tvs.lParam = (LPARAM) pmyTreeCtrl;

pmyTreeCtrl->SortChildrenCB(&tvs);