Menu

GetOpenFileName doesn't start the open dialog

2002-12-23
2012-09-26
  • Nobody/Anonymous

    Here is the code i use:

    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HWND hwnd;              // owner window
    HANDLE hf;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    // Display the Open dialog box.

    if (GetOpenFileName(&ofn))
        hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
            0, (LPSECURITY_ATTRIBUTES) NULL,
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
            (HANDLE) NULL);

    I have tried using OPENFILENAME_SIZE_VERSION_400 but it is not defined, how can i fix this problem?

     
    • Nobody/Anonymous

      The file dialogs are a bit sensitive to the contents of the OPENFILENAME structure.  (Note that when you declare a structure its contents are undefined.)

      It is best to set the OPENFILENAME structure to all ZEROES to ensure NULLS in all the unused fields.

      Put this line immediately after "// Initialize OPENFILENAME" line in your code:

      memset(&ofn, 0, sizeof(OPENFILENAME));

      This should solve your problem and I recommend you do this with all file dialogs.

      BlakJak :]

       
    • Nobody/Anonymous

      szFile must contain empty string.
      strcpy(szFile,"") or szFile[0]=0

       
    • Nobody/Anonymous

      The Last Post Worked, THANKS!!

       
    • Nobody/Anonymous

      It worked, THANKS!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.