Thread: GetSaveFileName exception

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    GetSaveFileName exception

    I don't mean to annoy the readers, but I need some help with an exception problem with the function (I'm sure a lot of you have used this function, but I'm still inexperienced with it). Below is my code:

    Code:
    BOOL AskUser(IN HWND hDlg, 
    			 OUT PEXTDEVMODE pdevmode)
    {
    	OPENFILENAME fn;
    
    	// initialize OPENFILENAME structure
    	ZeroMemory(&fn, sizeof(fn));
    
    	fn.lStructSize = sizeof(OPENFILENAME);
    	fn.Flags = OFN_HIDEREADONLY;
    	fn.lpstrTitle = L"PDF SaveFile";
    	fn.hwndOwner = NULL;
    
    	// ???
    	fn.lpstrCustomFilter = NULL;
    	fn.nMaxCustFilter = 0;
    
    	// output format
    	fn.lpstrFilter = L"*.pdf";
    	fn.nFilterIndex = 0; //formatIdx+1;     // the index here is not zero-based
    
    	// output file name + path + ext
    	fn.lpstrFile = L"";
    	fn.nMaxFile = 1000;
    
    	// output file name + ext
    	fn.lpstrFileTitle = L"";
    	fn.nMaxFileTitle = 500;
    
    	// output folder
    	fn.lpstrInitialDir = NULL;
    
    	GetSaveFileName(&fn);
    
    	return TRUE;
    }
    I'm getting exception errors at run-time after I have pressed "OK" button on the Save File dialog box.
    Code:
    First-chance exception at 0x763b1ff8 (comdlg32.dll) in Savefile.exe: 0xC0000005: Access violation writing location 0x0041597c.
    Unhandled exception at 0x763b1ff8 (comdlg32.dll) in Savefile.exe: 0xC0000005: Access violation writing location 0x0041597c.
    After searching through the forum, there's a similar problem with an exception getting thrown (http://cboard.cprogramming.com/showt...Name+exception). However, I'm not sure 100% if this is the case. I'm not sure what is fundamentally wrong with how I've set up the openfilename structure as I have attempted to follow MSDN documentation as diligently as I could. The next step for me perhaps would be using a debugger such as windbg or Visual Studio debugger to look at the register values to get some clue about the exception, but I'm not sure how to make sense out of it. Any suggestions would be much appreciated. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I don't have much experience with OPENFILENAME either so I don't know how much help I will be, but where you have "output filename + path + ext" there is nothing for fn.lpstrFile. Do you need a valid path such as C:\ in order to save the file correctly?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    No, I haven't included a filepath. I have the user specify the filepath, and the parameter being passed is acting both as an input and output.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No, I haven't included a filepath. I have the user specify the filepath, and the parameter being passed is acting both as an input and output.
    Yes, but that's exactly the problem:
    Code:
    	// output file name + path + ext
    	fn.lpstrFile = L"";
    	fn.nMaxFile = 1000;
    Now, nMaxFile:
    Quote Originally Posted by MSDN
    Specifies the size, in TCHARs, of the buffer pointed to by lpstrFile.
    First, you're passing a pointer to const memory (a string literal, which is const) that will be used as an output buffer. Then you tell windows that it has space for 1000 bytes. (It has space for exactly 1, and it's read-only.)

    You need to actually allocate memory, either in heap or stack, for lpstrFile, and pass the appropriate size of that memory to nMaxFile.
    Ditto for lpstrFileTitle, although this one is optional. Set it to NULL if you don't plan to use it.
    Finally, the string you are passing for lpstrFilter is incorrectly formatted. Read the documentation.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thanks for all your advices. After following your advices and rereading the documentation and more sample code, I have managed to achieve what I wanted to do to retrieve the filepath.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM