Thread: listing files in dialog box

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    23

    listing files in dialog box

    I'm using DlgDirListComboBox(), to try and get files listed in my dialogs combo box.

    this is the code for it,

    Code:
    BOOL CALLBACK DlgProc(HWND dhwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    
        switch(Message)
        {
    
    	case WM_INITDIALOG:
    
                   char *filelist;
                   filelist = (char*)GlobalAlloc(GPTR,5);
                   filelist = "*.bmp";
    
    	DlgDirListComboBox((HWND)IDD_DIALOG1,//dialog's HWND?
    			filelist,//just bmp's from current dir
    			IDC_COMBO1,//combo box
    			0,
    			DDL_READWRITE);
    
                   return FALSE;
    
    
                   case WM_COMMAND:
                   switch(LOWORD(wParam))
                   {
                    case IDC_BUTTON1:
                    EndDialog(dhwnd, IDC_BUTTON1);//test button
                    break;
    
                    case IDCANCEL:
                    EndDialog(dhwnd, IDCANCEL);
                    break;
                    }
                    break;
    
            default:
                return FALSE;
        }
        return FALSE;
    
    
    }//end of dialog routine
    
    
    
    //dialogue ini, from main window proc
    
            case WM_LBUTTONDOWN:
    			{
                    int ret = DialogBox(GetModuleHandle(NULL), 
    	MAKEINTRESOURC(IDD_DIALOG1),dhwnd,
                    DlgProc);
    			}
    
    		return 0;
    the problem is the files aren't showing, also when I used dhwnd instaed of (HWND)IDD_DIALOG1, for the dialogs handle, a mwmory error message came up
    Thanks, hope some one can help.
    Last edited by Stevo; 06-02-2004 at 06:37 AM. Reason: fix code tags

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    char *filelist;
    filelist = (char*)GlobalAlloc(GPTR,5);
    filelist = "*.bmp";
    This is your problem. To copy a string into an array, you must use strcpy or lstrcpy. When you do:
    Code:
    filelist = "*.bmp";
    you set filelist to point to a read-only string and lose its previous contents, in this case the pointer to the memory you allocated on the previous line. DlgDirListComboBox() requires a read-write string so you get the memory error you describe.

    Here is a better version:
    Code:
    // Note that this syntax creates a read-write string.
    // On the other hand char * filelist = "*.bmp"; creates a read-only string.
    char filelist[] = "*.bmp";
    DlgDirListComboBox(dhwnd, filelist, IDC_COMBO1, 0, DDL_READWRITE);
    P.S The end code tag is [/CODE] and the start code tag is [CODE]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    23
    Thanks, that works now.
    I saw GlobalAlloc() in a tutorial recently, I thought it was a windows way of doing strings or something.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display text in a Dialog Box
    By Dark_Phoenix in forum Windows Programming
    Replies: 9
    Last Post: 01-02-2009, 06:30 AM
  2. Getting data from Dialog Box Controls
    By jmd15 in forum Windows Programming
    Replies: 2
    Last Post: 08-05-2005, 04:56 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. edit control in dialog box problems
    By Bajanine in forum Windows Programming
    Replies: 11
    Last Post: 11-11-2002, 06:55 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM