Unexplained INVALID_HANDLE_VALUE from CreateFile()

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #1
May 30th, 2009
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.

  1. file = CreateFile(fileName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  2. if (file != INVALID_HANDLE_VALUE)

Does anybody know why this line of code might be throwing INVALID_HANDLE_VALUE?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #2
May 30th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #3
May 30th, 2009
Originally Posted by Yiuca View Post
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?
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.
Last edited by killdude69; May 30th, 2009 at 6:52 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #4
May 30th, 2009
Originally Posted by killdude69 View Post
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.
Well the number it returns is the error, you can read it on the MS site, however there is a way to format the messages... A little function I wrote with the help of MSDN.

  1. DWORD MsgID = GetLastError();
  2. char *TextSize;
  3. std::cerr << "Error has occured: ";
  4.  
  5. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, MsgID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  6. (LPTSTR) &TextSize, 0, 0);
  7.  
  8. std::cout << TextSize << "\n";
  9.  
  10. LocalFree(TextSize);
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #5
May 30th, 2009
The error says (The system cannot find the path specified).
I use a variable called: LPSTR fileName = "";

This is my open code:
  1. LPSTR tmp_file = OpenDlg(hwnd, hEdit);
  2. if (tmp_file != NULL || tmp_file != "")
  3. {
  4. fileName = tmp_file;
  5. OpenText(hEdit, fileName);
  6. }

The functions used in the code and the code itself works fine.
This is the save code:

  1. if (isSaved == false) {
  2. if (fileName != "") {
  3. if (!SaveText(hEdit, fileName))
  4. {
  5. error("The file could not be saved.");
  6. }
  7. } else {
  8. SendMessage(hwnd, WM_COMMAND, CTRL_MENU_FILE_SAVEAS, 0);
  9. }
  10. }

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #6
May 30th, 2009
Originally Posted by killdude69 View Post
The error says (The system cannot find the path specified).
I use a variable called: LPSTR fileName = "";

This is my open code:
  1. LPSTR tmp_file = OpenDlg(hwnd, hEdit);
  2. if (tmp_file != NULL || tmp_file != "")
  3. {
  4. fileName = tmp_file;
  5. OpenText(hEdit, fileName);
  6. }

The functions used in the code and the code itself works fine.
This is the save code:

  1. if (isSaved == false) {
  2. if (fileName != "") {
  3. if (!SaveText(hEdit, fileName))
  4. {
  5. error("The file could not be saved.");
  6. }
  7. } else {
  8. SendMessage(hwnd, WM_COMMAND, CTRL_MENU_FILE_SAVEAS, 0);
  9. }
  10. }

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.
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

  1. LPSTR tmp_file = OpenDlg(hwnd, hEdit);
  2. if (tmp_file != NULL || tmp_file != "")
  3. {
  4. fileName = tmp_file;
  5. OpenText(hEdit, fileName);
  6. }
  7. if (hEdit == INVALID_HANDLE_VALUE)
  8. //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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #7
May 30th, 2009
Originally Posted by Yiuca View Post
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

  1. LPSTR tmp_file = OpenDlg(hwnd, hEdit);
  2. if (tmp_file != NULL || tmp_file != "")
  3. {
  4. fileName = tmp_file;
  5. OpenText(hEdit, fileName);
  6. }
  7. if (hEdit == INVALID_HANDLE_VALUE)
  8. //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.
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

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #8
May 30th, 2009
Originally Posted by killdude69 View Post
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
Well it's misleading then, I treated it as the handle because it had a h before Edit like Microsofts convention of handles. Try an absolute path just to see if it does work or doesn't work still as expected. (just to see, because if it does work then you have a new lead on what the problem is, sure worth trying before hours of debug time)

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #9
May 30th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: Unexplained INVALID_HANDLE_VALUE from CreateFile()

 
0
  #10
May 30th, 2009
Originally Posted by killdude69 View Post
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
Alright thanks, you know so much actual code (implementation) shouldn't really go in a header but I'm not about to go into that 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)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC