0

This is the part of my code causing the mistake:

LVITEM item = { LVIF_PARAM };
m_trackList.GetSelectedItem(&item);

I want to get the pszText of the item, but I constantly get a NULL value after GetSelectedItem is being performed

This part of the code is called, when I doubleclick an item in my CListViewCtrl. It clearly has a text, and when I set a breakpoint at the part where the entry is being created, the LVITEM there clearly has a valid pszText as well. This is the part where it is being created:

std::wstring displayWString = m_tracksVectorIterator->GetDisplayName(); /*this returns a valid std::wstring */
std::string displayString(displayWString.begin(), displayWString.end());

LVITEM lv;
lv.mask = LVIF_TEXT;
lv.iItem = 0; 
lv.iSubItem = 0;
lv.pszText = const_cast<char *>(displayString.c_str());

//When I set a breakpoint here, the LVITEM contains correct values    

SendMessage(m_trackList, LVM_INSERTITEM, 0, (LPARAM)&lv);

Please feel free to tell me if there is any additional information needed. I don't really know what the problem could be, so I am not sure what I would have to include as well. I feel like the rest of my code is not really relevant to this behaviour.

Thanks in advance.

Sossenbinder
  • 4,852
  • 5
  • 35
  • 78
  • 1
    Since you are requesting the *lParam* (by specifying the `LVIF_PARAM`) this is hardly surprising. If you want to retrieve an item's text value, you need to pass a *mask* containing the `LVIF_TEXT` value. Also, passing a **temporary** value (`lv.pszText = const_cast(displayString.c_str());`) is highly dubious. – IInspectable Jan 03 '16 at 15:01
  • You should add `LVIF_TEXT`, and then... [If the LVIF_TEXT flag is set in the mask member of the LVITEM structure, the pszText member must point to a valid buffer and the cchTextMax member must be set to the number of characters in that buffer. Applications should not assume that the text will necessarily be placed in the specified buffer. The control may instead change the pszText member of the structure to point to the new text, rather than place it in the buffer.](https://msdn.microsoft.com/en-us/library/windows/desktop/bb774953) – Roman R. Jan 03 '16 at 15:05
  • @RomanR. That works. Thank you! – Sossenbinder Jan 03 '16 at 15:11

0 Answers0