Solved

Problem with GetSaveFileName().. no dialog appears?

Posted on 2006-10-25
14
538 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
Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 87
How to convert MFC::CString to UTF8 wchar_t* 10 388
Gaming Software 1 30
learn programming 8 73
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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