Solved

Problem with GetSaveFileName().. no dialog appears?

Posted on 2006-10-25
14
541 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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