| | |
Unexplained INVALID_HANDLE_VALUE from CreateFile()
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Just so you know, the SaveAs works perfect, it is just the Save that is the problem.
Edit: It seems that after you've set an absolute path to save the 1st time, for example C:\Users\<Username>\Desktop, even if you close the program & go back into it, every subsequent save will save to that directory & is essentially working as it should. The only problem comes in with the invalid handle is when you cancel, or the 1st time you save you don't give it an absolute path.
Last edited by Yiuca; May 30th, 2009 at 8:52 am.
•
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
The save is supposed to save the changes to the file if one is already open. If one is not, it will send the command message for SaveAs to prompt a dialog.
I have made a program like this before, and I have never needed any higher priviledge to save in Documents. It has always worked before.
My guess is that the fileName variable (LPSTR fileName = "") is not getting the value, but I am unsure. Because when I use tmp_file for the SaveAs, it seems to work. Also, I am using the same file writing function for Save as I am SaveAs. So the handles are exactly the same, so if it is invalid, the SaveAs should report the same error, but it does not.
So therefor the error cannot be in "SaveText()", it must be in the WM_COMMAND case for CTRL_MENU_FILE_SAVE.
And as to why that is I am still completely clueless.
I have made a program like this before, and I have never needed any higher priviledge to save in Documents. It has always worked before.
My guess is that the fileName variable (LPSTR fileName = "") is not getting the value, but I am unsure. Because when I use tmp_file for the SaveAs, it seems to work. Also, I am using the same file writing function for Save as I am SaveAs. So the handles are exactly the same, so if it is invalid, the SaveAs should report the same error, but it does not.
So therefor the error cannot be in "SaveText()", it must be in the WM_COMMAND case for CTRL_MENU_FILE_SAVE.
And as to why that is I am still completely clueless.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
The save is supposed to save the changes to the file if one is already open. If one is not, it will send the command message for SaveAs to prompt a dialog.
I have made a program like this before, and I have never needed any higher priviledge to save in Documents. It has always worked before.
My guess is that the fileName variable (LPSTR fileName = "") is not getting the value, but I am unsure. Because when I use tmp_file for the SaveAs, it seems to work. Also, I am using the same file writing function for Save as I am SaveAs. So the handles are exactly the same, so if it is invalid, the SaveAs should report the same error, but it does not.
So therefor the error cannot be in "SaveText()", it must be in the WM_COMMAND case for CTRL_MENU_FILE_SAVE.
And as to why that is I am still completely clueless.
So fileName = "", in the Window Procedure it checks if it is "" or not it sends a message for CTRL_MENU_SAVEAS, which calls the function SaveDlg expecting a LPSTR returned. Save Dialog returns the value put into the textbox via Save.File(); Which implementation is in dlgs.h
cpp Syntax (Toggle Plain Text)
char* SaveFileDialog::File(void) { if(Opened) { if(Success) return FileName; else { if(ShowErrors == true) { MessageBox(_Parent, "An error occured or the operation was canceled!", "ERROR", MB_OK | MB_ICONERROR); } } } }
Assuming Opened & Success are true it returns FileName which ends up returned back in the Windows Procedure in tmp_file, it then calls SaveText with the tmp_file in hand. You're right this seems like a merry go round!
I guess next it's to see if Opened & Success are both evaluating to true. Try shoving a messagebox & a few elses in the SaveFileDialog::File() function, just to see, since if either of them aren't, it'll pretty much skip rest of the function without returning anything. (might be the problem)
To which I will take leave of this problem for the moment here, since I really need some sleep now
I would imagine someone else will be able to sort it since there are far better programmers than myself here. So good luck. •
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
Okay, Yuicia.
But actually, Save.File() is only used in the SaveAs, which works perfect.
Save.File() is only used by SaveAs, so that has nothing to do with it.
Here is the outlook if anyone else would like to help me (which seems unlikely since only 1 other person has posted in this thread):
Open: This works, opens the file, shows the text.
SaveAs: This works, Saves the file, no errors returned.
Save: Says the file handle is invalid, even when it is the very same used in SaveAs.
But actually, Save.File() is only used in the SaveAs, which works perfect.
Save.File() is only used by SaveAs, so that has nothing to do with it.
Here is the outlook if anyone else would like to help me (which seems unlikely since only 1 other person has posted in this thread):
Open: This works, opens the file, shows the text.
SaveAs: This works, Saves the file, no errors returned.
Save: Says the file handle is invalid, even when it is the very same used in SaveAs.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Okay, Yuicia.
But actually, Save.File() is only used in the SaveAs, which works perfect.
Save.File() is only used by SaveAs, so that has nothing to do with it.
Here is the outlook if anyone else would like to help me (which seems unlikely since only 1 other person has posted in this thread):
Open: This works, opens the file, shows the text.
SaveAs: This works, Saves the file, no errors returned.
Save: Says the file handle is invalid, even when it is the very same used in SaveAs.
I would like to note that I don't get an Invalid Handle when I run the program I only get invalid file name which once I've changed to a full path, filename & extention saves just fine even with save. (since it calls save as) if I open a file & click save then I get an error about the file name, directory name or volume label has incorrect syntax & can't be saved. I think that is generated by GetLastError() you'll have to let me know if comparing it with strlen or similar sorts the error, as it may just be mistaken comparison, it usually is something small, silly, easy to miss.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Oh, Yiucia.
This thread is now solved, I decided to completely re-write my save, new, open, and SaveAs code. It was a pain in the ass but it helped. And then when you and ArkM answered my other question, it solved the rest of this one. Thanks again, and for your interest in this question.
![]() |
Similar Threads
- Send data on a serial port (C++)
- Failure of CreateFile() (C)
- how to call these functions? (C#)
- small program request (C++)
- Winsock Send File To Client Help (C++)
- Help with multithread server (C)
- Flushing write buffer in VB6 (Visual Basic 4 / 5 / 6)
- data file help (C)
Other Threads in the C++ Forum
- Previous Thread: Emptying char MAX_PATH array?
- Next Thread: problem in converting txt to binary
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





