954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Failure of CreateFile()

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?

bops
Posting Whiz in Training
214 posts since Aug 2005
Reputation Points: 23
Solved Threads: 5
 

Do you have a minimally complete snippet that I might try to play along with?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

And don't forget to close the file < CloseFile() > before leaving that function!

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

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 )

bops
Posting Whiz in Training
214 posts since Aug 2005
Reputation Points: 23
Solved Threads: 5
 

Similar to goto statements, I've found multiple return paths are as equally problematic. If line 25 is executed then hFile is still open

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

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.

bops
Posting Whiz in Training
214 posts since Aug 2005
Reputation Points: 23
Solved Threads: 5
 

Exactly, keep related stuff in the function or subroutine.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You