Solved

Using GetOpenFileName() correctly

Posted on 1998-12-01
4
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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