CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1

    Why doesn't this work?

    Why doesn't the BROWSE dialog appear?

    Code:
                         OPENFILENAME file_data;
    
                         file_data.lStructSize = 50;
                         file_data.hwndOwner = hwnd;
                         file_data.hInstance = hInst;
                         file_data.lpstrFilter = NULL;
                         file_data.lpstrCustomFilter = NULL;
                         file_data.nMaxCustFilter = 40;
                         file_data.nFilterIndex = NULL;
                         file_data.lpstrFile = NULL;
                         file_data.nMaxFile = 256;
                         file_data.lpstrFileTitle = NULL;
                         file_data.nMaxFileTitle = NULL;
                         file_data.lpstrInitialDir = NULL;
                         file_data.lpstrTitle = NULL;
                         file_data.Flags = OFN_ALLOWMULTISELECT;
                         file_data.nFileOffset = NULL;
                         file_data.nFileExtension = NULL;
                         file_data.lpstrDefExt = NULL;
                         file_data.lCustData = NULL;
                         file_data.lpfnHook = NULL;
                         file_data.lpTemplateName = NULL;
    
                         GetOpenFileName(&file_data);
                         MessageBox(hwnd, file_data.lpstrFile, "the file", 0);
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  2. #2
    Join Date
    Nov 1999
    Location
    Copenhagen, Denmark
    Posts
    265
    You should always check return codes. If GetOpenFileName returns FALSE you can obtain error information through:

    Code:
    DWORD CommDlgExtendedError(VOID).

    It look suspicous that you set lStructSize to 50. Are you sure that

    Code:
    sizeof(OPENFILENAME) == 50
    ?
    Best regards,
    S. Bro

    "I would be happy to deal with my problems one at the time if they would only line up!"
    -Paul Cilwa, "Borland C++ Insider".

    Other useful fora some of which I ruthlessly clipboarded from other peoples footers.

    MSDN: http://search.microsoft.com/us/dev/default.asp
    WIN 32 Assembler: http://board.win32asmcommunity.net/
    RDBMS: http://www.dbforums.com
    Robert's Perl Tutorial: http://www.sthomas.net/roberts-perl-tutorial.htm
    Merriam-Webster Online: http://www.m-w.com/home.htm
    Writing Unmaintainable Code: http://mindprod.com/unmain.html

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...there are several errors in your code...
    Code:
    file_data.lStructSize = 50;
    As already mentioned...this flag should be set in the following way:
    Windows NT 4.0: In an application that is compiled with WINVER and _WIN32_WINNT >= 0x0500, use OPENFILENAME_SIZE_VERSION_400 for this member.

    Windows 2000/XP: Use sizeof (OPENFILENAME) for this parameter.
    Code:
    file_data.hInstance = hInst;
    You are setting an instance handle, however, this member is ignored as long as neither the 'OFN_ENABLETEMPLATEHANDLE' nor the 'OFN_ENBALETEMPLATE' flag is set within the 'Flags' member which is not the case in your example...
    Code:
    file_data.lpstrCustomFilter = NULL;
    file_data.nMaxCustFilter = 40;
    With the two lines you are specifying that 'lpstrCustomFilter' points to an allocated memory buffer of 40 bytes which is not the case...this member gets ignored.
    Code:
    file_data.lpstrFile = NULL;
    file_data.nMaxFile = 256;
    With the two lines you are specifying that 'lpstrFile' points to an allocated memory buffer of 256 bytes which is not the case...
    Code:
    MessageBox(hwnd, file_data.lpstrFile, "the file", 0);
    With this line you are trying to display the selected filename. However, as I mentioned before you did not supply a valid buffer to the member 'lpstrFile' which you are trying to dereference now. This most-likely will crash your application immediately...

  4. #4
    Can anyone please give me an example?
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Devil Panther
    Can anyone please give me an example?
    Code:
    char         szExtBuffer[MAX_PATH] = "";
    OPENFILENAME ofn;
    
    strcpy(szExtBuffer, "*.txt; *.log");
    
    ofn.lStructSize       = sizeof(ofn);
    ofn.hwndOwner         = 0;
    ofn.hInstance         = 0;
    ofn.lpstrFilter       = 0;
    ofn.lpstrCustomFilter = 0;
    ofn.nMaxCustFilter    = 0;
    ofn.nFilterIndex      = 0;
    ofn.lpstrFile         = szExtBuffer;
    ofn.nMaxFile          = MAX_PATH;
    ofn.lpstrFileTitle    = 0;
    ofn.nMaxFileTitle     = 0;
    ofn.lpstrInitialDir   = 0;
    ofn.lpstrTitle        = "Open file";
    ofn.Flags             = 0;
    ofn.nFileOffset       = 0;
    ofn.nFileExtension    = 0;
    ofn.lpstrDefExt       = 0;
    ofn.lCustData         = 0;
    ofn.lpfnHook          = 0;
    ofn.lpTemplateName    = 0;
    
    // Open dialog
    if(::GetOpenFileName(&ofn) == FALSE)
      [COLOR=#008800]// Cancel pressed

  6. #6
    ****... it fails, and doesn't show any dialog box... why???

    btw, I'm using borland C++ 5.5.
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    The following works correctly. Also, this should work on any Windows compiler.
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
       char         szExtBuffer[MAX_PATH] = "";
       OPENFILENAME ofn;
       strcpy(szExtBuffer, "*.txt; *.log");
       memset(&ofn, 0, sizeof(ofn));
       ofn.lStructSize       = sizeof(ofn);
       ofn.lpstrFile         = szExtBuffer;
       ofn.nMaxFile          = MAX_PATH;
       ofn.lpstrTitle        = "Open file";
    
       // Open dialog
       if(::GetOpenFileName(&ofn) == FALSE)
       {
       }
       return 0;
    }
    Compiles cleanly, and displays the File open dialog.

    Regards,

    Paul McKenzie

  8. #8
    It does not show me anything...
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured