Solved

Problem with GetSaveFileName().. no dialog appears?

Posted on 2006-10-25
14
524 Views
Last Modified: 2012-06-27
I have this code:

      OPENFILENAME of;
      of.lpstrCustomFilter = "*.csv";
      of.hwndOwner = AfxGetMainWnd()->m_hWnd;
      of.lpstrFile = "count.csv";
      of.lpstrInitialDir      = "e:\\";
      of.lpstrDefExt = ".csv";
      
      BOOL bRes = ::GetSaveFileName(&of);


it compiles and runs, but when I call GetSaveFileName, no dialog ever appears and bRes is false.

GetLastError() returns 87, "the paramater is incorrect"

but it's compiling and running, so what's the problem?

All I want to do is open a dialog where the user can choose a path and .CSV filename

thanks
0
Comment
Question by:PMH4514
  • 9
  • 3
  • 2
14 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17804688
>>so what's the problem?

Try

     OPENFILENAME of;
     ZeroMemory(&of,sizeof(of));
     of.lpstrCustomFilter = "*.csv";
     of.hwndOwner = AfxGetMainWnd()->m_hWnd;
     of.lpstrFile = "count.csv";
     of.lpstrInitialDir     = "e:\\";
     of.lpstrDefExt = ".csv";
     
     BOOL bRes = ::GetSaveFileName(&of);

to set all other struct members to NULL.
0
 

Author Comment

by:PMH4514
ID: 17804747
nope, still no dialog, still error 87
0
 
LVL 86

Expert Comment

by:jkr
ID: 17804771
Ooos, I forgot, you need to set the most important member:

     OPENFILENAME of;
     ZeroMemory(&of,sizeof(of));
     of.lStructSize = sizeof(of);
0
 

Author Comment

by:PMH4514
ID: 17804792
nope, same thing still.
0
 

Author Comment

by:PMH4514
ID: 17804823
it doesn't like the file extension stuff.. This pops the dialog :

      OPENFILENAME of;
      ZeroMemory(&of,sizeof(of));
      of.lStructSize = sizeof(of);
//      of.lpstrCustomFilter = "*.csv";
      of.hwndOwner = AfxGetMainWnd()->m_hWnd;
//      of.lpstrFile = "count.csv";
      of.lpstrInitialDir   = "e:\\";
      of.lpstrDefExt = ".csv";

but if those commented lines are included, I get error 87
0
 

Author Comment

by:PMH4514
ID: 17804847
also, in the instance above where I can the dialog to open, if I do enter a filename and then click OK, bRes is true, but none of the OF fields are populated with the filename I provided.
0
 

Author Comment

by:PMH4514
ID: 17804876
additionally (I still need to limit and default to .CSV) but with those lines commented out, the file I provided isn't specified in the OPENFILENAME struct (As I said above) but the GetLastError() is 2 - "The system cannot find the file specified. "

of course it can't, I'm trying to let the user specify a name of a new file! Am I completely mis understanding something here??
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 17804922
Try

of.lpstrFilter = "csv";

"lpstrCustomFilter" is something different:

lpstrCustomFilter
Pointer to a static buffer that contains a *pair* of null-terminated filter strings for preserving the filter pattern chosen by the user.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 20 total points
ID: 17805421
>>>> of.lpstr

All these lpstr members need to be writeable buffers cause they are mostly used for in/out operations. If you assign

   of.lpstrFilter = "*.csv";

you assign a (local) const char* to a char* what normally should give a compile error. I would suggest to use writeable char arrays instead or buffers from CString::GetBuffer();

   char szCustomFilter[256] = { '\0' };   // make all chars zero
   strcpy(szCustomFilter, "*.csv");
   of.lpstrCustomFilter = szCustomFilter;

Note, as jkr mentioned some of these members get a list of entries and need to be (finally) terminated by two zero characters. The above technique guarantees that there are (at least) two final zero characters. Moreover, all buffers were writeable.

Doing the same using CString is

   CString strCustomFilter("*.csv\0\0", sizeof("*.csv\0\0"));  // creates a string with length 8 and enough trailing zeroes
   of.lpstrCustomFilter = strCustomFilter.GetBuffer(256);    // don't be stingy

Regards, Alex
0
 

Author Comment

by:PMH4514
ID: 17806091
>> JKR Wrote:
>>Try
>>of.lpstrFilter = "csv";

that did work.

Alex - both of your suggestions created the same error.
0
 

Author Comment

by:PMH4514
ID: 17806110
so, any thoughts as to why once I do get the dialog opened successfully and enter a filename, that the return value is false, with GetLastError() is 2 - "The system cannot find the file specified. "

??
0
 

Author Comment

by:PMH4514
ID: 17806535
uhhh. duh!

nevermind.. I need to be using CFileDialog to allow the user to select a non-existing file.

points awarded
thanks!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17809519
>>>> both of your suggestions created the same error.

The error is/was due to the *contents* of your inputs. My suggestions above were regarding the fact that you shouldn't assign literals to input/output arguments cause that could lead to a crash.

The next point is that you should read the documentation thoroughly  cause some of the inputs require two zero characters for termination what might happen by accident when assigning literals but should be made explicity to make it safe for a release version as well..

Regards, Alex


0
 

Author Comment

by:PMH4514
ID: 17811829
gotcha. I missed a few points.
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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

18 Experts available now in Live!

Get 1:1 Help Now