Thread: problem with the open dialog box

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    problem with the open dialog box

    i was just working with files and opening them and i'm using the common open dialog box to get the filename and path from the user. anyways, there seems to be an error with my implementation of the open dialog box because the function returns false and it doesn't even bring up the open dialog box. here is how i call the function i wrote:
    Code:
    if(GetFileFromOpen(hDlg, szFileName, szTitleName) == FALSE)
                            MessageBox(NULL, TEXT("Could not open file name..."), TEXT("Error!"), 0);
    and here is the function i wrote to set up the OPENFILENAME struct and call GetOpenFileName:
    Code:
    BOOL GetFileFromOpen(HWND hwnd, PTSTR szFileName, PTSTR szTitleName)
    {
        OPENFILENAME ofn;
        static TCHAR szFilter[] = TEXT("Text Files (*.txt)\0*.txt\0");
    
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = hwnd;
        ofn.hInstance = NULL;
        ofn.lpstrFilter = szFilter;
        ofn.lpstrCustomFilter = NULL;
        ofn.nMaxCustFilter = 0;
        ofn.nFilterIndex = 0;
        ofn.lpstrFile = szFileName;
        ofn.nMaxFile = MAX_PATH;
        ofn.lpstrFileTitle = szTitleName;
        ofn.nMaxFileTitle = MAX_PATH;
        ofn.lpstrInitialDir = NULL;
        ofn.lpstrTitle = NULL;
        ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT;
        ofn.nFileOffset = 0;
        ofn.nFileExtension = 0;
        ofn.lpstrDefExt = TEXT("txt");
        ofn.lCustData = 0;
        ofn.lpfnHook = NULL;
        ofn.lpTemplateName = NULL;
    
        return GetOpenFileName(&ofn);
    }
    why is GetOpenFileName returning FALSE therefore causing GetFileFromOpen (the function i wrote) to return false? am i doing something wrong in setting up the fields of the OPENFILENAME struct? or am i calling something wrongly? please help.

    thanks!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Can't tell for sure, but this is how I do it:
    Code:
    BOOL DialogOpen(HWND Window, LPSTR FileName)
    {
       OPENFILENAME Ofn;
       ZeroMemory(&Ofn, sizeof(OPENFILENAME));
    
       Ofn.lStructSize = sizeof(OPENFILENAME);
       Ofn.hwndOwner = Window;
       Ofn.lpstrFilter = "Game level files (*.lvl)\0*.lvl\0All Files (*.*)\0*.*\0\0";
       Ofn.lpstrFile = FileName;
       Ofn.nMaxFile = MAXSTRINGLENGTH;
       Ofn.lpstrInitialDir = NULL;
       Ofn.lpstrTitle = "Open:";
       Ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    
       return GetOpenFileName(&Ofn);
    }
    Are you sure you're not pressing CANCEL instead of OK? (I doubt you do it though... )
    Last edited by Magos; 02-07-2003 at 05:32 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    that's the problem...the open dialog doesn't even come up for any action to take place.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Your code seems fine, so....

    What's the return value from CommDlgExtendedError?
    What compiler and os are you using?
    Have you tried Magos's example and found that failed too?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    what kind of function is that?

    and i'm using dev-c++

  6. #6

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    what i dont undersatnd is why the open dialog box doesnt even show up on the screen. it just returns false right away.

    thanks for the link

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    i'm struggling with the problem. still cant figure it out. would you like me to post all of my program code? there's not too much of it. i could just attach it as a txt file.

    thanks.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    i did that CommDlgExtendedError test and i got the error i think it's FNERR_INVALIDFILENAME and the description is an invalid filename. what does this mean? how do i fix it? are they talking about the filename field of the OPENFILENAME struct? what could possibly be wrong with it?

    thansk.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try a double NULL terminator for the lpstrFilter of your OPENFILENAME struct; - from msdn for the lpstrFilter parameter:
    Pointer to a buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    you won't believe the fix to this.

    in this function call:
    Code:
    GetFileFromOpen(hDlg, szFileName, szTitleName)
    the two strings szFileName and szTitleName were delcared as TCHAR arrays. one would think that's fine. but it didnt work, of course. and then i made them static TCHAR arrays and it worked. any idea why?

    i'm glad it's fixed, though. thanks for the help!

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    any idea why i needed to static the strings?

  13. #13
    CrOv
    Guest
    From MSDN
    lpstrFile
    Pointer to a buffer that contains a filename used to initialize the File Name edit control.
    The first character of this buffer must be NULL if initialization is not necessary.

    By making szFileName static it is initialized with NULLs so there is no error;

    TCHAR szFileName[MAX_PATH]="test.txt";
    (The dialog displays in the "File Name" edit box "test.txt")

    In your case szFileName[0] was not NULL and it took it for a file name
    so you got FNERR_INVALIDFILENAME as szFileName was uninitialized.

    You could write
    TCHAR szFileName[MAX_PATH]={0};

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    oh i see what you mean. but why does static make the string null? it doesnt do that for all data types, does it? i thought static just preserved values for the next call to that function.

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem creating a Dialog Box
    By Dark_Phoenix in forum Windows Programming
    Replies: 1
    Last Post: 05-30-2009, 08:16 AM
  2. Parent of a BrowseForFolder dialog box
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2007, 02:54 PM
  3. Dialog Box (Compile Problem)
    By Vicious in forum Windows Programming
    Replies: 6
    Last Post: 08-31-2004, 03:29 PM
  4. dialog box: open folder : how to?
    By toby1909 in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2002, 05:18 AM
  5. Dialog Box Problem....
    By minime6696 in forum Windows Programming
    Replies: 2
    Last Post: 01-08-2002, 08:24 PM