Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using GetOpenFileName() correctly

Posted on 1998-12-01
4
Medium Priority
?
458 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

926 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