Solved

Using GetOpenFileName() correctly

Posted on 1998-12-01
4
435 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.Net Loading Word 11 103
Problem to adjust sheet 1 90
Strange - Deleted Resource Info appears in Azure Portal 6 90
Please explain "Promise Pattern" and how it's used 2 56
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.…
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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