Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using GetOpenFileName() correctly

Posted on 1998-12-01
4
Medium Priority
?
452 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 200 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 free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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