Ask user to save file before exiting.

This is a discussion on Ask user to save file before exiting. within the Windows Programming forums, part of the Platform Specific Boards category; I dont have my Petzold book handy and I am having difficulty with asking the user to save the file ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Question Ask user to save file before exiting.

    I dont have my Petzold book handy and I am having difficulty with asking the user to save the file when File|Exit is selected and the file has changes that are unsaved.

    When I select the save dialog directly from the menu it works flawlessly, but I can't get it to ask the user to save the file, when a user selects 'exit' from the menu or the 'close' button.

    Dialog box asks: "Save current changes?"
    1. When I press: 'no' it closes the application as it should.
    2. When I press: 'cancel' the dialog box is dismissed and the application continues as it should!
    3. The trouble begins when I press: 'yes' the dialog box is destroyed and the application contiues running without exiting or running the IDM_FILE_SAVE.

    What have I done incorrectly?



    I am guessing my error is somewhere in the following code:

    Code:
    case IDM_FILE_EXIT:
    	SendMessage (hwnd, WM_CLOSE, 0, 0);
    	return 0 ;
    
    case WM_DESTROY:  // This case actually terminates the process.
    	PostQuitMessage (0);
    	return 0;
    
    case WM_CLOSE :
    	if ( !bFileNeedsSaved || IDCANCEL != AskAboutSave())
    		DestroyWindow(hwnd);
    	return 0;
    
    case WM_QUERYENDSESSION :
    	if ( !bFileNeedsSaved || IDCANCEL != AskAboutSave())
    		return 1;
    	return 0;
    
    
    int AskAboutSave ()
    {
         int   iReturn ;
         
    	 iReturn = MessageBox (hwnd, "Save current changes?", szAppName,
                               MB_YESNOCANCEL | MB_ICONQUESTION) ;
         
         if (iReturn == IDYES)
              if (!SendMessage (hwnd, WM_COMMAND, IDM_FILE_SAVE, 0))
                  iReturn = IDCANCEL ;
    
    	// SendMessage(hwnd, WM_COMMAND, IDM_FILE_SAVE, 0);
    
              
         return iReturn ;
    }
    
    case IDM_FILE_SAVE:
    	strcat(PathAndFilename, pDefaultSaveLocation);
    	if(GetSaveFileName(&ofn))
    	{
    		pPathAndFilename = ofn.lpstrFile;
    		if((fp = fopen (pPathAndFilename, "w"))==NULL)	//a+ append/create and or overwrite.
    		{
    			MessageBox (hwnd, TEXT ("Cannot save file!"), 
    				szAppName, MB_ICONEXCLAMATION | MB_OK);
    			exit(1);
    		}
    		// Write the file!
    		WriteData ();
    		fclose(fp);
    		bFileNeedsSaved = false;
    		//strip everything but filename from pPathAndFilename and store it into szFileName.  See 
    		char Temp[MAX_PATH];
    		char* pTemp = &Temp[0];
    		pTemp = strrchr(pPathAndFilename, '\\');
    		pTemp++;
    		char * szFileName = pTemp; 
    	}
    	MessageBeep (0) ;
    	return 0 ;
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    What's happening is you're sending a message that doesn't get the time to be processed before your window gets killed. What would be a wiser alternative would be to shell out IDM_FILE_SAVE and put it in a function, then call that function from IDM_FILE_SAVE, and also from the message box place.

    This way, you're ensured to save the file immediately instead of hoping windows processes your message before killing your program.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I think this could be the problem

    SendMessage (hwnd, WM_COMMAND, IDM_FILE_SAVE, 0)

    Is the hwnd correct?

    try....

    (include Windows.h)
    //in MAKEWPARAM send 1 for control, 0 for menu item
    // hwnd is for the parents callback

    SendMessage (hwnd, MAKEWPARAM( 1, IDM_FILE_SAVE), (LPARAM) GetDlgItem( hwnd, IDM_FILE_SAVE) );
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thanks for the input jverkoey and novacain, I'll give both your suggestions a whirl!

    ...
    This is what I ended up doing to get it to work! Hopefully it will help someone else out:

    Code:
    int AskAboutSave (HWND hwnd)
    {
         int   iReturn ;
         iReturn = MessageBox (NULL, "Save current changes?", szAppName,
                               MB_YESNOCANCEL | MB_ICONQUESTION) ;
         if (iReturn == IDYES)
    		 SendMessage(hwnd, WM_COMMAND, IDM_FILE_SAVE, 0);
         return iReturn ;
    }
    Last edited by Bajanine; 11-15-2004 at 09:32 PM.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21