I am trying to display the contents of a listbox using the SendMessage function. However when I press the button to execute the piece of code below, I get the following segfault in my code when I call
SendMessage(hwndLBox,LB_GETTEXT,k,(LPARAM)lBoxitems);
Segfault (dbgheap.c):
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
The Code that causes this segfault:
else if(LOWORD(wParam)==ID_BUTTON_CALC){
int getLboxitems = SendMessage(hwndLBox,LB_GETCOUNT,NULL,NULL);
char * lBoxitems = new char[getLboxitems];
for(unsigned int k=0; k < getLboxitems; k++){
SendMessage(hwndLBox,LB_GETTEXT,k,(LPARAM)lBoxitems);
MessageBox(hwnd,lBoxitems,"Listbox says",NULL);
}
delete[] lBoxitems;
}
I am using Visual Studio Beta 2011 to compile my code.
You need to allocate space for your strings.
char * lBoxitems = new char[getLboxitems];allocates a char array that is the size of how many items you have in the list.The following should work I think:
However, using
std::vector<std::vector<char>>orstd::vector<std::string>would be easier: