Search code examples
listviewwinapilistviewitem

ListView_GetItem() returns FALSE


I have a Windows application in which I call ListView_GetItem() and it returns FALSE (error). See code below. The values passed are line 10 for sel_index, and 1 for Col, buf is large enough, GetLastError() returns 0.
The Windows online documentation for ListView_GetItem macro does not tell what can be the reasons for failure or possible error codes. Can someone tell me what could be possibly wrong?
I created the ListView with

hList = CreateWindow(WC_LISTVIEW, "", WS_CHILD | WS_BORDER | LVS_REPORT | WS_HSCROLL | WS_VSCROLL | WS_EX_CLIENTEDGE, ... etc


{
   LV_ITEM Item;              // List view item structure
   char buf[10];

   Item.mask=LVIF_TEXT;
   Item.pszText = buf;     // buffer
   Item.iItem = sel_index;    // selected line
   Item.iSubItem = Col;       // want subitem
   if(!ListView_GetItem(hList, &Item))
   {
       PrintErr("\r\nGetSubItem failed, error=%d",GetLastError());
       return "";
   }
   return (Item.pszText);
}

Solution

  • You left most of the field uninitialised. Not setting cchTextMax is what hurts the most. Initialise the struct like this:

    LV_ITEM Item = {0}; 
    

    and set cchTextMax to 10.

    Item.pszText = buf;  
    Item.cchTextMax = 10;