title: Win32 LB_GETTEXT returns garbage
body: <p>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?</p>
  
  <p>Below is the code and an example of what I get.</p>
  
  <pre><code>std::string Listbox::GetSelected() {
  int index = -1;
  int count = 0;
  
  count = SendMessage(control, LB_GETSELCOUNT, 0, 0);
  
  if(count &gt; 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 &gt;= 0) {
      count = GetItemCount();
  
      if(index &lt; 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;
  }
  </code></pre>
  
  <p>GetItemCount just does that. It just gets the number of items currently in the listbox.</p>
  
  <p>The string I was grabbing from the Listbox is "Test String" and it returned ¨±é» Tzã</p>
  
  <p>Any help is appericated, thanks.</p>
  
  <p>Ok, I narrowed it down to my GetSelected function as GetString returns the correct string.</p>