Retrieving strings from a listbox

This is a discussion on Retrieving strings from a listbox within the Windows Programming forums, part of the Platform Specific Boards category; I'm having a major problem retrieving strings from a listbox and i don't have a clue as to what's wrong. ...

  1. #1
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54

    Question Retrieving strings from a listbox

    I'm having a major problem retrieving strings from a listbox and i don't have a clue as to what's wrong. Actually, i know exactly what's wrong but i don't know why it is. Anyways, here's the code segment:

    Code:
    HWND hList = GetDlgItem(hwnd, IDC_LIST1);
    count = SendMessage(hList, LB_GETCOUNT, 0, 0);
    
    while(count >= 0)
    { 
         SendMessage(hList, LB_GETTEXT, count, (LPARAM)buf);
         fpROM = fopen(buf, "rb");
    
    ... continues.
    As you can see i'm trying to extract a string - specifically a file path - and then reading the file. The problem is that it doesn't even get to there! After running this through the debugger, when it hits the SendMessage line, it gives an error for the value hList, stating:
    unused CXX0030: Error: expression cannot be evaluated.
    I have NO idea why this is since i've used SendMessage before without error and SendMessage expects a HWND as the first argument.

    I'm totally stumped, any ideas??
    Not yet, have to think of one...

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    IDC_LIST1 is a listbox?

    or

    try casting the wpram, lparam

    SendMessage(hList, LB_GETCOUNT, (WPARAM)0, (LPARAM)0);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Originally posted by novacain
    IDC_LIST1 is a listbox?
    Yup.


    try casting the wpram, lparam

    SendMessage(hList, LB_GETCOUNT, (WPARAM)0, (LPARAM)0);
    I've tried that, but it seems it's having a problem with the handle to the listbox. I'm using Visual Studio.NET btw.
    Not yet, have to think of one...

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    When using the debugger, does hList have a value or is it zero? With the code fragments given, all we can do is assume that by the time you call SendMessage() the handle is valid.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    make sure hwnd is a handle to the dialog that the listbox is on, not the main window. just so i dont confuse myself i use hwnd for window handles and hDlg for dialog handles.

    otherwise with the code you posted it seems to be fine. if that doesnt work, then just post all the source code so we can work with it in our compilers.

  6. #6
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Here's my DialogProc:
    Code:
    BOOL CALLBACK DlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
    {
          switch(Message)
          {
          case WM_INITDIALOG:
                   SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON1)));
                   SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1)));		break;
          case WM_CLOSE:
    	EndDialog(hDlg, 0);
    	break;
          case WM_COMMAND:
    	switch(LOWORD(wParam))
    	{
    	case IDEXIT:
      	        EndDialog(hDlg, 0);
    	        break;
    	case ID_FILE_EXIT:
    	        EndDialog(hDlg, 0);
                            break;
    	case ID_HELP_ABOUT:
    	        DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hDlg, AboutDlgProc);
    	        break;
    	case IDCLEAR:
    	        SendDlgItemMessage(hDlg, IDC_LIST1, LB_RESETCONTENT, 0, 0);
    	        break;
    	case IDADD:
    	{
    	        char filename[MAXBUFFER] = "";
    	        OPENFILENAME ofn;
    	        ZeroMemory(&ofn, sizeof(ofn));
    
    	        ofn.lStructSize = sizeof(OPENFILENAME);
    	        ofn.hwndOwner   = hDlg;
    	        ofn.lpstrFilter = "GameBoy Roms (*.gb)\0*.gb\0";
    	        ofn.lpstrFile   = filename;
    	        ofn.nMaxFile    = MAXBUFFER;
    	        ofn.Flags       = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
    	        ofn.lpstrDefExt = "gb";
    
    	        if(GetOpenFileName(&ofn))
    	       {
    	               if(ofn.nFileOffset < lstrlen(ofn.lpstrFile))
    	              {
    		    SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)ofn.lpstrFile);
    	               }
    		else
    	               {
    		  char *temp = ofn.lpstrFile + ofn.nFileOffset;
    		  while(*temp != '\0')
    		  {
    		       SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)temp);
    							                        temp += strlen(temp) + 1;
    		   }
    		}
    	}
    	break;
             }
             case IDREMOVE:
            {
                   int index = SendMessage(hList, LB_GETCURSEL, 0, 0);
                   SendMessage(hList, LB_DELETESTRING, (WPARAM)index, 0);
    	break;
             }
             case IDGENERATE:
             {
                   int count, position, c, temp = 0, index = 0, fpos = 0;
                   char romName[16], *buf = "";
    				
                   FILE *fpBIN, *fpBINhead, *fpROM;
    
                   fpBINhead = fopen("tmpBINhead", "wb");
                   fpBIN     = fopen("tmpBIN", "wb");
    
                   HWND hList = GetDlgItem(hDlg, IDC_LIST1);
                   count = SendMessage(hList, LB_GETCOUNT, 0, 0);
    
                   while(count >= 0)
                   { 
    	      SendMessage(hList, LB_GETTEXT, (WPARAM)count, (LPARAM)buf);      // <--- THIS IS THE PROBLEM.  "buf" stays empty and, therefore, the next line produces an error and crashes the program.
    	
    	      fpROM = fopen(buf, "rb");
    
    ... continues.
    edit: sorry if the code looks a bit sloppy in format, hard to get it to look right. Anyways, It's the single SendMessage call that doesn't do what it's supposed to, every other SendMessage call work perfectly.
    Last edited by -leech-; 02-27-2003 at 08:48 AM.
    Not yet, have to think of one...

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> SendMessage(hList, LB_GETTEXT, (WPARAM)count, (LPARAM)buf);

    You get the count of the number of items, say 5, then ask to retrieve the text for item 5, but the items are numbered 0 - 4. This should fail with LB_ERR.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    I just did a check (a simple if() block) and apperently that single SendMessage ALWAYS returns LB_ERR. Why? I have no idea...

    Man, this is frustrating...
    Not yet, have to think of one...

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Where are you allocating the storage for your string that is returned? You create a pointer, but it is not pointing to any memory.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Originally posted by adrianxw
    Where are you allocating the storage for your string that is returned? You create a pointer, but it is not pointing to any memory.
    I've changed it from
    Code:
    char *buf = "";
    to
    Code:
    char buf[255];
    ... still doesn't work.
    Not yet, have to think of one...

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> Man, this is frustrating...

    What is frustrating is not being able to see any of the values while the thing is running, I expect it would take me seconds to see what was wrong if I had the thing running in the debugger!

    count: is this value correct after the LB_GETCOUNT call, (i.e. you have 5 items in the box it sets count to 5)? If it is, then we know that the problem is not with your handle. I did ask you about that earlier, but you didn't say.

    If it is correct, are you now sure you are passing the right value to the LB_GETTEXT message, i.e. LB_GETCOUNT - 1, (or not sending LB_GETTEXT at all if count = 0)?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Just ran a check and, yes, the 'count' variable keeps an accurate number of listbox entries.

    edit:

    If it is correct, are you now sure you are passing the right value to the LB_GETTEXT message, i.e. LB_GETCOUNT - 1, (or not sending LB_GETTEXT at all if count = 0)?
    Yea, that was it!! Shoot, damn logic errors, thanks a lot for your help.
    Not yet, have to think of one...

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I did actually mention that in my 5:44 message - I assumed you had fixed that.

    Still, all's well that ends well as they say.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Strings to ListBox in a Dialog
    By kevins963 in forum C Programming
    Replies: 5
    Last Post: 09-10-2008, 04:36 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21