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?

Recommended Answers

All 7 Replies

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

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.

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

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 )

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

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.

Exactly, keep related stuff in the function or subroutine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.