Solved

Using GetOpenFileName() correctly

Posted on 1998-12-01
4
426 Views
Last Modified: 2013-12-03
Below is the code I have for trying to use GetOpenFilename to pop-up a dialog for opening a file in my non-MFC application.

However, when the program gets to this line of code, and I try to step over it in the debugger, it brings the application down, without any error message, except 0xC0000005 Access Violation!! Help!

     S_filename.lStructSize       = sizeof( S_filename );

    S_filename.hwndOwner       = (HWND)Parent_Handle;    

    S_filename.hInstance       = NULL;

    S_filename.lpstrFilter       = "Castle Files\0*.set\0";    

    S_filename.lpstrCustomFilter = NULL;

//    S_filename.nMaxCustFilter    = NULL;    

    S_filename.nFilterIndex      = 1;

//    S_filename.lpstrFile;    

//    S_filename.nMaxFile;

//    S_filename.lpstrFileTitle;    

//    S_filename.nMaxFileTitle;

    S_filename.lpstrInitialDir       = current_filter;    

    S_filename.lpstrTitle       = NULL;

    S_filename.Flags             = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_NONETWORKBUTTON;    

//    S_filename.nFileOffset;

//    S_filename.nFileExtension;    

//    S_filename.lpstrDefExt;

//    S_filename.lCustData;    

//    S_filename.lpfnHook;

//    S_filename.lpTemplateName;



      status = GetOpenFileName( &S_filename );

0
Comment
Question by:gunn
  • 2
4 Comments
 
LVL 3

Expert Comment

by:plaroche
ID: 1416552
Have you called InitCommonControls in your application?
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1416553
You should set all the unused members of the struct to zero.

Here's a snippet from one of my programs:

    OPENFILENAME ofn = { sizeof(OPENFILENAME) };
    const char filter[] = "Text files\0*.TXT\0All files\0*.*\0";
    char file[4096] = "";

    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = filter;
    ofn.nFilterIndex = 1;
    ofn.lpstrFile = file;
    ofn.nMaxFile = sizeof file;
    ofn.lpstrInitialDir = directory;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_NONETWORKBUTTON;

    GetOpenFileName(&ofn);

0
 

Author Comment

by:gunn
ID: 1416554
Thanks a lot alexo. That did the trick!

0
 

Author Comment

by:gunn
ID: 1416555
Nah, I never did call InitCommonControls...? But it works fine now..
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now