locked
GetOpenFileName() fails on Vista 64 bit RRS feed

  • Question

  • Hi All,


    My code is opening File Open common dialog using GetOpenFileName() API. Everything works fine except on Vista 64 bit.
    On 64 bit Vista machine the Open Dialog does not show.
    GetLastError() returned 87, "The parameter is incorrect."

    But I could not trace out which parameter is incorrect. I have pasted the code here.
    Any help regarding this is appreciated.

    Albin


    OPENFILENAME stFileInfo;
        char   szSelectFileName[MAX_PATH+1];
        char   szSelectFileTitle[MAX_PATH+1];
        
        ::memset(&stFileInfo, 0x00, sizeof stFileInfo);
        ::memset(szSelectFileName, 0x00, sizeof szSelectFileName);
        ::strcpy(szSelectFileName, "*.bat");
        ::memset(szSelectFileTitle, 0x00, sizeof szSelectFileTitle);
        stFileInfo.lStructSize = sizeof stFileInfo;
        stFileInfo.hwndOwner = hDlg;
        stFileInfo.lpstrFilter = "*.bat\0";
                    stFileInfo.nFilterIndex = 1;
        stFileInfo.lpstrFile = szSelectFileName;
        stFileInfo.nMaxFile = sizeof szSelectFileName;
        stFileInfo.lpstrFileTitle = szSelectFileTitle;
        stFileInfo.nMaxFileTitle = sizeof szSelectFileTitle;
        stFileInfo.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_LONGNAMES | OFN_NOCHANGEDIR | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST;

                   
        GetOpenFileName(&stFileInfo);

    Tuesday, May 19, 2009 9:34 AM

Answers

  • I found the problem. Struct Member Alignment on my visual studio setting was 1 Byte. Because of that sizeof(struct) became diffrent value. Because I converted the project from VS 6 to VS 2008 it became 1 Byte.

    I changed this value to Default and everything worked

    • Marked as answer by Albin Baby Thursday, May 21, 2009 4:38 AM
    Thursday, May 21, 2009 4:37 AM

All replies

  • Try using sizeof (OPENFILENAME) for the lStructSize member

    Noticed that you used

    stFileInfo.hwndOwner = hDlg;

    I don't know what hDlg stands for, but this member should contain the handle of the window that owns the openfilename dialog, which should be your application window handle, or the handle of another dialog box that  invokes the openfilename dialog.

    Replace

    stFileInfo.lpstrFilter = "*.bat\0";
     
    by

    stFileInfo.lpstrFilter = "Bat Files\0*.bat\0\0";


     

     

    Tuesday, May 19, 2009 10:17 AM
  • Are you sure the build is a NON-UNICODE build that you are doing on Vista-64 ?
    «_Superman_»
    Tuesday, May 19, 2009 10:18 AM
  • Hi Belloc,

    As you mentioned the erro is related to lStructSize only.

    Even after I tried your option, its not working.

    I checked the error code of CommDlgExtendedError(). Its returning CDERR_STRUCTSIZE which means
    "The lStructSize member of the initialization structure for the corresponding common dialog box is invalid."

    The same code works fins if it is built as 32 bit but fails when built as 64 bit.
    And I noticed that sizeof() operator returns size_t. And size_t is 32 bit on 32 bit plaform and 64 bit on 64 bit platform.
    Is there any relation to this???
    Tuesday, May 19, 2009 10:53 AM
  • Have you tried   stFileInfo.lpstrFilter = "Bat Files\0*.bat\0\0";

    stFileInfo.lpstrFilter = "*.bat\0";     // is wrong !
     

    Tuesday, May 19, 2009 11:37 AM
  • This is a NON-UNICODE build
    Tuesday, May 19, 2009 11:41 AM
  • You are also trying to use sizeof stFileInfo before defining the member stFileInfo.lStructSize !

    You don't need to zero out the structure stFileInfo before using it
    Tuesday, May 19, 2009 12:08 PM
  • I tried your code in a project.
    My machine runs Vista-64 bit.

    The Open File dialog showed for both Win32 and x64 builds.


    «_Superman_»
    Wednesday, May 20, 2009 6:29 AM
  • I don't see the problem either.  MAX_PATH+1 is wrong, as it the filter but that doesn't explain the error.  Consider a buffer overflow that destroyed the contents of stFileInfo.  That should be readily visible with the debugger.

    Hans Passant.
    Wednesday, May 20, 2009 1:12 PM
  • I found the problem. Struct Member Alignment on my visual studio setting was 1 Byte. Because of that sizeof(struct) became diffrent value. Because I converted the project from VS 6 to VS 2008 it became 1 Byte.

    I changed this value to Default and everything worked

    • Marked as answer by Albin Baby Thursday, May 21, 2009 4:38 AM
    Thursday, May 21, 2009 4:37 AM