| | |
Failure of CreateFile()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
c Syntax (Toggle Plain Text)
BOOL setContent( LPSTR szContent ) { HANDLE hFile; BOOL bSuccess = FALSE; DWORD dwTextLength; hFile = CreateFile( "data.txt" , GENERIC_WRITE , 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL ); if( hFile != INVALID_HANDLE_VALUE ) { DWORD dwWritten; dwTextLength = strlen( szPassword ); if( WriteFile( hFile , szPassword , dwTextLength , &dwWritten , NULL ) ) bSuccess = TRUE; else LastErrorMessage( "setPassword()"); } else { MessageBox(hwnd,"setPassword(): CreateFile failed.",0,0); LastErrorMessage( "setPassword()"); // Format and Display GetLastError } return bSuccess; }
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?
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
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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..
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 )
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..
c Syntax (Toggle Plain Text)
char * getContent( ) { HANDLE hFile; BOOL bSuccess = FALSE; LPSTR pszFileText; hFile = CreateFile( PASSWORD_FILE , GENERIC_READ , FILE_SHARE_READ , NULL , OPEN_EXISTING , 0 , NULL ); if( hFile != INVALID_HANDLE_VALUE ) { DWORD dwFileSize; dwFileSize = GetFileSize( hFile , NULL ); if( dwFileSize != 0xFFFFFFFF ) { pszFileText = (LPSTR) GlobalAlloc( GPTR , dwFileSize + 1 ); if( pszFileText != NULL ) { DWORD dwRead; if( ReadFile( hFile , pszFileText , dwFileSize , &dwRead , NULL ) ) { pszFileText [ dwFileSize ] = 0; // Add null terminator return pszFileText; } GlobalFree( pszFileText ); } } CloseHandle( hFile ); } return ""; }
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 )
![]() |
Similar Threads
- 1782 disk controller failure (Storage)
- Harddrive failure and 100% CPU under Win2K, Help Needed! (Windows NT / 2000 / XP)
- Bios ini failure at startup!! (Windows NT / 2000 / XP)
- Delayed Write Failure (Windows NT / 2000 / XP)
- cache memory failure (Motherboards, CPUs and RAM)
- constant beep and boot failure on Armada (Troubleshooting Dead Machines)
Other Threads in the C Forum
- Previous Thread: Threading in C
- Next Thread: Check windows passwords
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h






