944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11927
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 12th, 2006
0

Windows programming - C - Save file function

Expand Post »
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...

C++ Syntax (Toggle Plain Text)
  1. BOOL SaveFile(HWND owner,HWND hEdit, LPCTSTR pszFileName)
  2. {
  3. HANDLE hFile;
  4. BOOL bSuccess = FALSE;
  5.  
  6. MessageBox(owner,pszFileName,0,0); // Testing if the parameter from pszFileName has been recieved
  7.  
  8. // Create the file (overwrite existing files) and assign the handle to hFile
  9. hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
  10. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  11.  
  12. //########## RETURNS INVALID HANDLE VALUE ############
  13. //###### MAYBE PROBLEM WITH CREATING THE FILE ########
  14. //####################################################
  15.  
  16. // If the handle to the file has been created sucessfully
  17. if(hFile != INVALID_HANDLE_VALUE)
  18. {
  19. DWORD dwTextLength;
  20. // Get text length of edit control within child window for allocating memory
  21. dwTextLength = GetWindowTextLength(hEdit);
  22.  
  23. // If there is no text then there is nothing to write to the file that has been created
  24. if(dwTextLength > 0)
  25. {
  26. LPSTR pszText;
  27. // Amount of me mory to be allocated = textsize + 1 for null terminator
  28. DWORD dwBufferSize = dwTextLength + 1;
  29.  
  30. // Allocate memory for the text
  31. pszText = GlobalAlloc(GPTR, dwBufferSize);
  32. if(pszText != NULL)
  33. {
  34. // Get the Text to be saved to the file fromt he edit control
  35. // within the child window.
  36. if(GetWindowText(hEdit, pszText, dwBufferSize))
  37. {
  38. DWORD dwWritten;
  39.  
  40. // Write the text to the file
  41. if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
  42. bSuccess = TRUE;
  43.  
  44. //Set Title of child window to the filename
  45. SetWindowText(owner, pszFileName);
  46. MessageBox(owner,"File saved",0,0); // Using to find error
  47. }
  48. else
  49. {
  50. MessageBox(owner,"Could not retrieve text",0,MB_ICONERROR); // Using to find error
  51. }
  52. // Free memory allocated for text
  53. GlobalFree(pszText);
  54. }
  55. else
  56. {
  57. MessageBox(owner,"Could not allocate memory",0,MB_ICONERROR); // Using to find error
  58. }
  59. }
  60. else
  61. {
  62. MessageBox(owner,"No text",0,0);// Using to find error
  63. }
  64. // Close the handle to the file if it exists
  65. CloseHandle(hFile);
  66. }
  67. else
  68. {
  69. MessageBox(owner,"Invalid handle value",0,MB_ICONERROR); // Using to find error
  70. }
  71. return bSuccess;
  72. }

Thanks in advance...
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Feb 12th, 2006
0

Re: Windows programming - C - Save file function

Why dont you use the GetLastError Function to get the detailed error number? Then you will be able to find why it is failing.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 12th, 2006
0

Re: Windows programming - C - Save file function

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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Feb 12th, 2006
0

Re: Windows programming - C - Save file function

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..
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 13th, 2006
0

Re: Windows programming - C - Save file function

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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Feb 13th, 2006
0

Re: Windows programming - C - Save file function

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Feb 13th, 2006
0

Re: Windows programming - C - Save file function

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

C++ Syntax (Toggle Plain Text)
  1. //SaveFile Function...
  2. BOOL SaveFile()
  3. {
  4. //Processing...
  5. }
  6.  
  7. //SaveFileAs function
  8. BOOL SaveFileAs()
  9. {
  10. OPENFILENAME ofn;
  11. //initialise the ofn structure
  12. char szFile[MAX_PATH];
  13. ofn.lpstrFile = szFile;
  14. //....Initialise rest of structure...
  15. //Pass the structure into GetSaveFileName
  16.  
  17. //Then call SaveFile function with the filename specified by the user
  18.  
  19. SaveFile(....szFile);
  20.  
  21. }

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 ?
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Feb 13th, 2006
0

Re: Windows programming - C - Save file function

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 13th, 2006
0

Re: Windows programming - C - Save file function

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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Feb 13th, 2006
0

Re: Windows programming - C - Save file function

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,
C++ Syntax (Toggle Plain Text)
  1. char szFile[MAX_PATH] = "";
  2. ofn.lpstrFile = szFile;
  3. //....Initialise rest of structure...
  4. //Pass the structure into GetSaveFileName

If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reading a key without stoping the loop
Next Thread in C++ Forum Timeline: find strings in file.txt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC