943,840 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6040
  • C RSS
Sep 1st, 2007
0

Failure of CreateFile()

Expand Post »
  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?
Similar Threads
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Sep 1st, 2007
0

Re: Failure of CreateFile()

Do you have a minimally complete snippet that I might try to play along with?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 1st, 2007
0

Re: Failure of CreateFile()

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.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Sep 1st, 2007
0

Re: Failure of CreateFile()

And don't forget to close the file < CloseFile() > before leaving that function!
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Sep 1st, 2007
1

Re: Failure of CreateFile()

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 )
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Sep 1st, 2007
1

Re: Failure of CreateFile()

Similar to goto statements, I've found multiple return paths are as equally problematic. If line 25 is executed then hFile is still open
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Sep 2nd, 2007
0

Re: Failure of CreateFile()

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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Sep 2nd, 2007
0

Re: Failure of CreateFile()

Exactly, keep related stuff in the function or subroutine.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 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: Threading in C
Next Thread in C Forum Timeline: Check windows passwords





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


Follow us on Twitter


© 2011 DaniWeb® LLC