Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with GetSaveFileName().. no dialog appears?

Posted on 2006-10-25
14
Medium Priority
?
548 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
Technology Partners: 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 200 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 80 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

722 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