User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,420 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,017 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4988 | Replies: 13 | Solved
Reply
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
bops bops is offline Offline
Junior Poster

Windows programming - C - Save file function

  #1  
Feb 12th, 2006
Hello again everyone, Im having trouble with this function for saving a file from a richedit control within a child window in my application. I have put in extra messageboxes etc to helo me find the error. when I implement this function with valid parameters I recieve the "Invalid handle value" message box so I gathered that there is something wrong creating the handle to the file. However when I use this function for the first time for example when i create a file and save plain text into it, the function creats the file and writes to it successfully, but when i try again to save into this same file I recieve the "Invalid Handle Value" message. Here is the code for the function...

BOOL SaveFile(HWND owner,HWND hEdit, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;
	
	MessageBox(owner,pszFileName,0,0); // Testing if the parameter from pszFileName has been recieved

    // Create the file (overwrite existing files) and assign the handle to hFile
	hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		
	//########## RETURNS INVALID HANDLE VALUE ############
	//###### MAYBE PROBLEM WITH CREATING THE FILE ########
	//####################################################
	
    // If the handle to the file has been created sucessfully
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwTextLength;
        // Get text length of edit control within child window for allocating memory
		dwTextLength = GetWindowTextLength(hEdit);

		// If there is no text then there is nothing to write to the file that has been created
		if(dwTextLength > 0)
		{
			LPSTR pszText;
			// Amount of me mory to be allocated = textsize + 1 for null terminator
			DWORD dwBufferSize = dwTextLength + 1;

            // Allocate memory for the text
			pszText = GlobalAlloc(GPTR, dwBufferSize);
			if(pszText != NULL)
			{   
                // Get the Text to be saved to the file fromt he edit control
			    // within the child window.
				if(GetWindowText(hEdit, pszText, dwBufferSize))
				{
					DWORD dwWritten;
					
                    // Write the text to the file
					if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
						bSuccess = TRUE;
					
                    //Set Title of child window to the filename	
					SetWindowText(owner, pszFileName);
					MessageBox(owner,"File saved",0,0); // Using to find error
				}
				else
				{
                 MessageBox(owner,"Could not retrieve text",0,MB_ICONERROR); // Using to find error
                }
                // Free memory allocated for text
				GlobalFree(pszText); 
			}
			else
			{
             MessageBox(owner,"Could not allocate memory",0,MB_ICONERROR); // Using to find error
            }
		}
		else
		{
         MessageBox(owner,"No text",0,0);// Using to find error
        }
        // Close the handle to the file if it exists
		CloseHandle(hFile);
	}
	else
	{
         MessageBox(owner,"Invalid handle value",0,MB_ICONERROR); // Using to find error
    }
	return bSuccess;
}

Thanks in advance...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

  #2  
Feb 12th, 2006
Why dont you use the GetLastError Function to get the detailed error number? Then you will be able to find why it is failing.
Reply With Quote  
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

  #3  
Feb 12th, 2006
Oh Thanks, I have found out the error code...32: File cannot be accessed because it is being use by another process. Could this mean I have forgotten to close a handle to a file somewhere? I have checked around for this and still cannot find out why.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

  #4  
Feb 12th, 2006
Just as I thought...Most probably you have opened it using another application. Since it is a Rich-Edit Control, my best guess is that you have opened it using MS-Word and trying to overwrite it while it is open..
Reply With Quote  
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

  #5  
Feb 13th, 2006
That is what I thought as it stated another "Process" ie another application, but it is only being used by my application. The thing is...when i first use the function to save the file it works perfectly, but then when make more changes to the richedit control text and attempt to save it again it does not work...I thought this may be due to forgetting to close a handle to the file somewhere but i cant find any errors.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Windows programming - C - Save file function

  #6  
Feb 13th, 2006
If you are running Norton Antivirus turn it off during development. Sometimes it causes problems like that. Don't forget to turn it back on when you are done.
Reply With Quote  
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

  #7  
Feb 13th, 2006
In my application I have it so that when i click a save button on the toolbar or menu item it will check to see if the file exists and then decide whether to save the file using the filename from the child window title or create a save as dialog box. The savefileas function creates the dialog box using the OPENFILENAME structure and calling GetSaveFileName, from which i then pass the file specified by the user into my SaveFile function. When I save the file as.. it seems to work every time however, when i call the savefile function say when the file exists it gives this error...ie

//SaveFile Function...
BOOL SaveFile()
{
//Processing...
}

//SaveFileAs function
BOOL SaveFileAs()
{
 OPENFILENAME ofn;
 //initialise the ofn structure
 char szFile[MAX_PATH];
 ofn.lpstrFile = szFile; 
 //....Initialise rest of structure...
 //Pass the structure into GetSaveFileName
 
 //Then call SaveFile function with the filename specified by the user

 SaveFile(....szFile);

}

Therefore this leads me to think that when i am passing the parameters into savefile() when the file exists, something is going wrong...but when the parameters are passed through the saveas dialog box the parameters are passwed correctly. It is confusing...any suggestions ?
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

  #8  
Feb 13th, 2006
It is difficult to point out the cause from here. It will be better if you set a breakpoint just after the call for the GetSaveFileName function and see if all the fields, especially the file name of the OPENFILENAME structure, are filled properly. You better use the ofn.Flags = OFN_OVERWRITEPROMPT; Statement before the GetSaveFileName call if you are not doing so already.
Reply With Quote  
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

  #9  
Feb 13th, 2006
Yes I have set the OPENFILENAME structure correctly...It is the process of saving the file using the text from the child window title that does not work correctly...The procedure to save the file as however works fine. The savefileas procedure uses the savefile() function within it so therefore i dont think there is anything wrong with the savefile() function itself...rather the way I am passing parameters to it when not using the saveas procedure.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

  #10  
Feb 13th, 2006
Iam running out of options here. I also think that the SaveFile function is okay. If the filename is correctly returned from GetSaveFileName, the SaveFile function should behave identically in both cases. This is a long shot but try,
char szFile[MAX_PATH] = "";
 ofn.lpstrFile = szFile; 
//....Initialise rest of structure...
 //Pass the structure into GetSaveFileName

If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC