Failure of CreateFile()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Failure of CreateFile()

 
0
  #1
Sep 1st, 2007
  1. BOOL setContent( LPSTR szContent )
  2. {
  3. HANDLE hFile;
  4. BOOL bSuccess = FALSE;
  5. DWORD dwTextLength;
  6.  
  7. hFile = CreateFile( "data.txt" , GENERIC_WRITE , 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL );
  8.  
  9. if( hFile != INVALID_HANDLE_VALUE )
  10. {
  11. DWORD dwWritten;
  12. dwTextLength = strlen( szPassword );
  13.  
  14. if( WriteFile( hFile , szPassword , dwTextLength , &dwWritten , NULL ) )
  15. bSuccess = TRUE;
  16. else
  17. LastErrorMessage( "setPassword()");
  18. }
  19. else
  20. {
  21. MessageBox(hwnd,"setPassword(): CreateFile failed.",0,0);
  22. LastErrorMessage( "setPassword()"); // Format and Display GetLastError
  23. }
  24. return bSuccess;
  25. }

CreateFile API here seems to always return INVALID_HANDLE_VALUE, and GetLastError reports that the operation completed successfully, but I still can't get a handle to the file.

What am I doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,445
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Failure of CreateFile()

 
0
  #2
Sep 1st, 2007
Do you have a minimally complete snippet that I might try to play along with?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Failure of CreateFile()

 
0
  #3
Sep 1st, 2007
The only thing I can think of is that another process or instance of this application already has the file open, and because you are not sharing the file this would not allow the handle to be created. Other than that, your code is correct.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,608
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1490
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Failure of CreateFile()

 
0
  #4
Sep 1st, 2007
And don't forget to close the file < CloseFile() > before leaving that function!
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Failure of CreateFile()

 
1
  #5
Sep 1st, 2007
Do you mean CloseHandle()?

Before when I call this function I am using a function to getContent() of a file. Within that function I do make sure to Close the handle to the file after reading the content into memory..

  1. char * getContent( )
  2. {
  3. HANDLE hFile;
  4. BOOL bSuccess = FALSE;
  5. LPSTR pszFileText;
  6.  
  7. hFile = CreateFile( PASSWORD_FILE , GENERIC_READ , FILE_SHARE_READ , NULL , OPEN_EXISTING , 0 , NULL );
  8.  
  9. if( hFile != INVALID_HANDLE_VALUE )
  10. {
  11. DWORD dwFileSize;
  12. dwFileSize = GetFileSize( hFile , NULL );
  13.  
  14. if( dwFileSize != 0xFFFFFFFF )
  15. {
  16. pszFileText = (LPSTR) GlobalAlloc( GPTR , dwFileSize + 1 );
  17.  
  18. if( pszFileText != NULL )
  19. {
  20. DWORD dwRead;
  21.  
  22. if( ReadFile( hFile , pszFileText , dwFileSize , &dwRead , NULL ) )
  23. {
  24. pszFileText [ dwFileSize ] = 0; // Add null terminator
  25. return pszFileText;
  26. }
  27. GlobalFree( pszFileText );
  28. }
  29. }
  30. CloseHandle( hFile );
  31. }
  32. return "";
  33. }

This works fine, but after calling it, the setContent does not work.

Actually after checking, both above functions work correctly by themselves, but its only after calling getContent that setContent does not work.

NOTE: I have modified the original getContent to include CloseHandle( hFile )
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Failure of CreateFile()

 
1
  #6
Sep 1st, 2007
Similar to goto statements, I've found multiple return paths are as equally problematic. If line 25 is executed then hFile is still open
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Failure of CreateFile()

 
0
  #7
Sep 2nd, 2007
Ooh, Yes I see where its gone wrong now. I'm returning before Closing the Handle to the file. So really I should return pszFileText at the end, and obviously remove GlobalFree( pszFileText )..

Cheers.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Failure of CreateFile()

 
0
  #8
Sep 2nd, 2007
Exactly, keep related stuff in the function or subroutine.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC