Solved

Problem with GetSaveFileName().. no dialog appears?

Posted on 2006-10-25
14
544 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
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

636 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