GetOpenFileName not working...

This is a discussion on GetOpenFileName not working... within the Windows Programming forums, part of the Platform Specific Boards category; Hi... I'm writing a C++ Win32 Application, and I need to let the user open a file. I found out ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    55

    GetOpenFileName not working...

    Hi... I'm writing a C++ Win32 Application, and I need to let the user open a file. I found out that to open the dialog box I had to use GetOpenFileName... However, I copied the code from MSDN but it appears it's doing nothing... To test it, I pasted it at the beginning of WinMain but it's like it does nothing... It never opens the window... This is the code:

    Code:
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    
    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;
    //
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    //
    ofn.lpstrFile[0] = '\0';
    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)==TRUE) 
        hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
            0, (LPSECURITY_ATTRIBUTES) NULL,
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
            (HANDLE) NULL);
    
    
    //I call my functions from here on...

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What is the return value of GetOpenFileName()? I assume it's resolving to false. Have you performed extensive error checking per MSDN's page on the function?

    MSDN -- GetOpenFileName()
    MSDN -- CommDlgExtendedError()

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    It's returning false indeed.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    OK, just let me put on this dentist's uniform.....

    OK, got it on.... Now where are those old pliers?

    Ah, got them. I'm ready now.

    So what did CommDlgExtendedError() return when you called it?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    CDERR_DIALOGFAILURE The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    HWND hwnd = NULL;

    It works now...

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Congratulations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21