Getting proper lParam value out of the selected treeview item when it is right clicked

I want to retrieve the lParam value stored in the tree view item when the item is right clicked. When any item is right clicked, the value stored in the last item is retrieved.

I have written the code as follows-

While storing the value -

TVITEM tvi;
TVINSERTSTRUCT tvins;
HTREEITEM hSelected = NULL;
HTREEITEM hNew = NULL;

tvi.mask = TVIF_PARAM | TVIF_TEXT | TVIF_HANDLE;
tvi.pszText = lpszItem;
tvi.cchTextMax = lstrlen(lpszItem);
tvi.lParam = (LPARAM) lpszParam;
tvins.item = tvi;
hSelected = TreeView_GetSelection(hTree);
tvins.hInsertAfter = hSelected;
tvins.hParent = hSelected;
TreeView_InsertItem(hTree, (LPTVINSERTSTRUCT) &tvins);

While retrieving the value in the WM_NOTIFY -
HTREEITEM hCurrent = NULL;
TVITEM tvi;
LPSTR lpszItem = NULL;
LPNMHDR pnmh = (LPNMHDR) lParam;

if ((pnmh->code == NM_RCLICK) && (pnmh->idFrom == ID_TREE_VIEW_CONTROL)) {
hCurrent = TreeView_GetSelection(hTree);
tvi.hItem = hCurrent;
tvi.mask = TVIF_PARAM;
TreeView_GetItem(hTree, &tvi);
lpszItem = (LPSTR) tvi.lParam;
}

Would be glad of help. Thankyou.
DeodharAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Roshan DavisConnect With a Mentor Commented:
LParam is only a 32 bit value.
You are assinging a string to that without allocating. So each time you assigning value to the same to "tvi.lParam = (LPARAM) lpszParam;" overwrites the previous string value.

So you have to allocate the string before you use InsertItem
like
char *psz = new char[length];
lstrcpy(psz, "Whatever");
tvi.lParam = (LPARAM) psz;

in order to avoid memory leak you have to add these to an array, or delete the string after use by traversing the tree...


Good Luck
0
 
DeodharAuthor Commented:
Thank you. I have just started writing code for windows SDK and couldn't have solved it on my own. Thank you for the speedy reply.
0
All Courses

From novice to tech pro — start learning today.