Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 LB_GETTEXT returns garbage

I have a problem which is most likely a simple problem, but neverthe less still a problem for me. I am using the Listbox in Win32 / C++ and when getting the selected text from my listbox the string returned is just garbage. It is a handle to a struct or similar?

Below is the code and an example of what I get.

std::string Listbox::GetSelected() {
int index = -1;
int count = 0;

count = SendMessage(control, LB_GETSELCOUNT, 0, 0);

if(count > 0) {
    index = SendMessage(control, LB_GETSEL, 0, 0);
}

return GetString(index);
}


std::string Listbox::GetString(int index) {
int count = 0;
int length = 0;
char * text;

if(index >= 0) {
    count = GetItemCount();

    if(index < count) {
        length = SendMessage(control, LB_GETTEXTLEN, (WPARAM)index, 0);
        text = new char[length + 1];

        SendMessage(control, LB_GETTEXT, (WPARAM)index, (LPARAM)text);
    }
}
std::string s(text);
delete[] text;

return s;
}

GetItemCount just does that. It just gets the number of items currently in the listbox.

The string I was grabbing from the Listbox is "Test String" and it returned ¨±é» Tzã

Any help is appericated, thanks.

Ok, I narrowed it down to my GetSelected function as GetString returns the correct string.

like image 982
Cory Avatar asked Apr 10 '09 22:04

Cory


1 Answers

The LB_GETSEL message does not return the index of a selected item, it returns the selected STATE of the ITEM you pass in WPARAM.

You also have a serious bug where if no items are selected you will attempt to retrieve the string of the item at index -1, which is clearly wrong. Checking the return values of these SendMessage calls would have helped you diagnose the problem.

Here's an example of how to get the text of the first selected item;

// get the number of items in the box.
count = SendMessage(control, LB_GETCOUNT, 0, 0);

int iSelected = -1;

// go through the items and find the first selected one
for (int i = 0; i < count; i++)
{
  // check if this item is selected or not..
  if (SendMessage(control, LB_GETSEL, i, 0) > 0)
  {
    // yes, we only want the first selected so break.
    iSelected = i;
    break;
  }
}

// get the text of the selected item
if (iSelected != -1)
  SendMessage(control, LB_GETTEXT, (WPARAM)iSelected , (LPARAM)text);

Alternatively you can use LB_GETSELITEMS to get a list of the items that are selected.

like image 195
Andrew Grant Avatar answered Oct 21 '22 00:10

Andrew Grant