Thread: Common Dialog boxes GetOpenFileName()

  1. #1
    Registered User
    Join Date
    Nov 2006

    Question Common Dialog boxes GetOpenFileName()

    I've been looking at common dialog boxes, specifically how to open a file of a users choice.

    I'd like to have a function that I can call that returns me the path to the file the user chose.

    Here's what I have so far:

    char* openFile()
          OPENFILENAME ofn;       // common dialog box structure
          char szFile[MAX_PATH];       // buffer for file name
          // Initialize OPENFILENAME
          ZeroMemory(&ofn, sizeof(ofn));
          ofn.lStructSize = sizeof(ofn);
          ofn.hwndOwner = NULL;
          ofn.lpstrFile = szFile;
          // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
          // use the contents of szFile to initialize itself.
          ofn.lpstrFile[0] = '\0';
          ofn.nMaxFile = MAX_PATH;
          ofn.lpstrFilter = "All\0";
          ofn.nFilterIndex = 1;
          ofn.lpstrFileTitle = NULL;
          ofn.nMaxFileTitle = 0;
          ofn.lpstrInitialDir = NULL;
          // Display the Open dialog box. 
          if (GetOpenFileName(&ofn))
             MessageBox(NULL, szFile ,"File Chosen", MB_OK);  //This Line is important Call it line X
             return szFile;
          return "Error file choosen doesn't exist or no file was chosen";
    Called from main:

    int main()
        char result[MAX_PATH];
        strcpy( result, openFile() );
        MessageBox(NULL, result ,"File Chosen", MB_OK);
    This works just fine except for one little problem. If I remove the message box at line X then the open file dialog box never opens and the message box in main pops up empty. It's almost as if the message box in line x pauses code execution so a user can choose a file.

    Thanks for reading

  2. #2
    Registered User
    Join Date
    Nov 2006
    Never mind It works fine.

  3. #3
    Registered User
    Join Date
    Jun 2008
    The way I did it was to send the pointer of the string I want to contain the path to the file in the function call and return 0/1 based on whether or not a file was successfully chosen.

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    The most peaks over 10,000 feet!
    Shouldn't ofn.lpstrFilter be double null terminated.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-24-2008, 03:14 PM
  2. Common Dialog Boxes
    By KonArtis in forum Windows Programming
    Replies: 3
    Last Post: 04-22-2005, 10:52 AM
  3. Dialog Boxes
    By Thantos in forum Windows Programming
    Replies: 7
    Last Post: 08-26-2003, 12:49 PM
  4. Common Dialog Boxes problem
    By face_master in forum Windows Programming
    Replies: 6
    Last Post: 07-29-2002, 05:01 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM