| | |
Unexplained INVALID_HANDLE_VALUE from CreateFile()
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
Dear DaniWeb,
I am working on a source code editor using Scintilla and am currently having some issues with the CreateFile() function. It does not return an error when I am saving as a new file, but when I am saving changes to the current file, it returns INVALID_HANDLE_VALUE. I am using the function exactly how I have used it before in other projects.
I have my own error report system setup, so I know exactly where the error is coming from. But suprisingly this does not help me very much. I have looked up the CreateFile() syntax on MSDN, and every parameter has the correct type of value. Also, my compiler does not throw an error. I am using Windows Vista.
Does anybody know why this line of code might be throwing INVALID_HANDLE_VALUE?
I am working on a source code editor using Scintilla and am currently having some issues with the CreateFile() function. It does not return an error when I am saving as a new file, but when I am saving changes to the current file, it returns INVALID_HANDLE_VALUE. I am using the function exactly how I have used it before in other projects.
I have my own error report system setup, so I know exactly where the error is coming from. But suprisingly this does not help me very much. I have looked up the CreateFile() syntax on MSDN, and every parameter has the correct type of value. Also, my compiler does not throw an error. I am using Windows Vista.
C++ Syntax (Toggle Plain Text)
file = CreateFile(fileName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (file != INVALID_HANDLE_VALUE)
Does anybody know why this line of code might be throwing INVALID_HANDLE_VALUE?
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
I take it you have tried using GetLastError() function & seeing what that is? (what is the value of GetLastError() for you?) Because the return value of CreateFile() is INVALID_HANDLE_VALUE only if the function fails.
As a 2nd "guess" are you using the CreateFile() on a file that is deemed already open with the "CREATE_ALWAYS" parameter? I don't know for sure but that might cause a problem?
As a 2nd "guess" are you using the CreateFile() on a file that is deemed already open with the "CREATE_ALWAYS" parameter? I don't know for sure but that might cause a problem?
•
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
•
•
•
•
I take it you have tried using GetLastError() function & seeing what that is? (what is the value of GetLastError() for you?) Because the return value of CreateFile() is INVALID_HANDLE_VALUE only if the function fails.
As a 2nd "guess" are you using the CreateFile() on a file that is deemed already open with the "CREATE_ALWAYS" parameter? I don't know for sure but that might cause a problem?
Also, no, I closed any previous handles.
Last edited by killdude69; May 30th, 2009 at 6:52 am.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Yes, I haved used GetLastError(), when I try to use it just crashes my application. GetLastError() returns a DWORD, how could I retrieve the description from it? Is there another function that I could use?
Also, no, I closed any previous handles.
cpp Syntax (Toggle Plain Text)
DWORD MsgID = GetLastError(); char *TextSize; std::cerr << "Error has occured: "; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, MsgID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &TextSize, 0, 0); std::cout << TextSize << "\n"; LocalFree(TextSize);
•
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
The error says (The system cannot find the path specified).
I use a variable called: LPSTR fileName = "";
This is my open code:
The functions used in the code and the code itself works fine.
This is the save code:
The SaveText function is returning FALSE due to the INVALID_HANDLE_VALUE.
Why is the path not being stored right in my variable?
Using LPSTR fileName[MAX_PATH]; does not work, because it is incompatible with the path string I try to assign it.
I use a variable called: LPSTR fileName = "";
This is my open code:
C++ Syntax (Toggle Plain Text)
LPSTR tmp_file = OpenDlg(hwnd, hEdit); if (tmp_file != NULL || tmp_file != "") { fileName = tmp_file; OpenText(hEdit, fileName); }
The functions used in the code and the code itself works fine.
This is the save code:
C++ Syntax (Toggle Plain Text)
if (isSaved == false) { if (fileName != "") { if (!SaveText(hEdit, fileName)) { error("The file could not be saved."); } } else { SendMessage(hwnd, WM_COMMAND, CTRL_MENU_FILE_SAVEAS, 0); } }
The SaveText function is returning FALSE due to the INVALID_HANDLE_VALUE.
Why is the path not being stored right in my variable?
Using LPSTR fileName[MAX_PATH]; does not work, because it is incompatible with the path string I try to assign it.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
The error says (The system cannot find the path specified).
I use a variable called: LPSTR fileName = "";
This is my open code:
C++ Syntax (Toggle Plain Text)
LPSTR tmp_file = OpenDlg(hwnd, hEdit); if (tmp_file != NULL || tmp_file != "") { fileName = tmp_file; OpenText(hEdit, fileName); }
The functions used in the code and the code itself works fine.
This is the save code:
C++ Syntax (Toggle Plain Text)
if (isSaved == false) { if (fileName != "") { if (!SaveText(hEdit, fileName)) { error("The file could not be saved."); } } else { SendMessage(hwnd, WM_COMMAND, CTRL_MENU_FILE_SAVEAS, 0); } }
The SaveText function is returning FALSE due to the INVALID_HANDLE_VALUE.
Why is the path not being stored right in my variable?
Using LPSTR fileName[MAX_PATH]; does not work, because it is incompatible with the path string I try to assign it.
cpp Syntax (Toggle Plain Text)
LPSTR tmp_file = OpenDlg(hwnd, hEdit); if (tmp_file != NULL || tmp_file != "") { fileName = tmp_file; OpenText(hEdit, fileName); } if (hEdit == INVALID_HANDLE_VALUE) //handle error ("Handle is invalid")
Try that & see if it triggers it there immediately after, as the problem looks as though it's more during the original attempt to get the handle that something is going wrong.
•
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
•
•
•
•
What is the path you're trying to save too? (I mean the absolute path) is it somewhere like Program Files or elsewhere where elevated privileges are required from the caller? As that ends up with that error even if you know for sure that path exists. The thing is when you 1st use it to say create/open the file, it will appear to do it, the next time you try to use the handle however is when you'll get the error about the handle. You could try to test for the invalid handle immediately after the open code, for example
cpp Syntax (Toggle Plain Text)
LPSTR tmp_file = OpenDlg(hwnd, hEdit); if (tmp_file != NULL || tmp_file != "") { fileName = tmp_file; OpenText(hEdit, fileName); } if (hEdit == INVALID_HANDLE_VALUE) //handle error ("Handle is invalid")
Try that & see if it triggers it there immediately after, as the problem looks as though it's more during the original attempt to get the handle that something is going wrong.
Also, I don't assign an absolute value to it, the user chooses the file.
I always choose: .../Documents/helloworld.txt
Which does actually exist because it is visible in the dialog.
There is nothing wrong with the open code. The text shows in the edit control fine.
Also, hEdit is not the file handle, it is the Edit Control
EDIT: Okay, I can't try to check it immediatley because the file handle is in include.h and I need to check it in main.cpp in order to do what you are telling me.
Last edited by killdude69; May 30th, 2009 at 7:43 am.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Okay, I will check the handle immediatley after. But that is the open code, which works fine.
Also, I don't assign an absolute value to it, the user chooses the file.
I always choose: .../Documents/helloworld.txt
Which does actually exist because it is visible in the dialog.
There is nothing wrong with the open code. The text shows in the edit control fine.
Also, hEdit is not the file handle, it is the Edit Control
With the handle testing I mainly just meant test it's state just after you 1st attempt to retrieve the handle to see if the problem is with not where you think. (in this case it would be before that test & not with the save code. As I get the idea it's more likely that the problem lay where you are retrieving the handle)
Alright I've just read your edit... How is it declared & when is the 1st time it attempts to retrieve a valid handle, (or atleast the time it attempts to retrieve a handle before you use it to save the file) can you track that down? (or post the code) As that is probably the best time to test the state, the idea is finding out where the problem is, I'm more wondering if it's before you try to use the handle like back when it tries to retrieve it, or not, as determining when it's happening is usually a good key to fixing it. I'm also pretty tired, up all night myself so excuse the rambling & the mess of text I tend to write at this time.
•
•
Join Date: Jul 2008
Posts: 45
Reputation:
Solved Threads: 1
Okay, here is a link to a zip file containing my 3 source files, and the exe with the problem. Because it would take too much time to explain where and how it is defined. And if I were to post the code it would be way too long.
Don't worry, the exe is not bad enough to crash your computer, just itself, but if you are still conserned you can look at the source before you run it.
The file "include.h" is the most important, it is the file containing the function that returns FALSE due to the INVALID_HANDLE_VALUE.
Here is the link to the source: RelikSCE.zip - 169.8 Kb
Don't worry, the exe is not bad enough to crash your computer, just itself, but if you are still conserned you can look at the source before you run it.
The file "include.h" is the most important, it is the file containing the function that returns FALSE due to the INVALID_HANDLE_VALUE.
Here is the link to the source: RelikSCE.zip - 169.8 Kb
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Okay, here is a link to a zip file containing my 3 source files, and the exe with the problem. Because it would take too much time to explain where and how it is defined. And if I were to post the code it would be way too long.
Don't worry, the exe is not bad enough to crash your computer, just itself, but if you are still conserned you can look at the source before you run it.
The file "include.h" is the most important, it is the file containing the function that returns FALSE due to the INVALID_HANDLE_VALUE.
Here is the link to the source: RelikSCE.zip - 169.8 Kb
Anyway when I click Save As & put in an absolute path (C:\Users\<Username>\Desktop, the file saves fine as it should.You will have to excuse all the former talk about where the handle were 1st retrieved as it has just occured to me that due to being up all night I weren't thinking straight & that has nothing to do with it since the handle is obtained when attempting to save the file & the problem lay within fileName (obviously) which would be the path, by the looks of it anyway, so yea please excuse me going off the rails there!

I'm just looking at it now, can see the SaveDlg, the text is put into SaveFileDialog Save; & returned is Save.File(); so I'm just going to look for how that connects & all, but as said an absolute (full directory path) seems to work. So a way round this could be to default a path & anything put into the dialog box would get appended onto it, of course it'd need the proper checking to ensure valid filename & all but it can be done. I'm just going to go & have a look at Save.File() & how it eventually gets to SaveText(HWND, LPCTSTR). (Just to see)
![]() |
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 beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





