All pastes #932571 Raw Edit

writefile

public text v1 · immutable
·published 2008-03-07 20:51 UTC
rendered paste body
//Write To The File
	INTERNET_BUFFERS BufferIn = { 0 };
	DWORD dwBytesRead;
	DWORD dwBytesWritten;
	BYTE pBuffer[1024]; // Read from file in 1K chunks
	BOOL bRead, bRet;
	BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
	HINTERNET hRequest = HttpOpenRequestA( url, "PUT", "log/log.txt", NULL, NULL, NULL,  0, 0 );
	HANDLE hFile = CreateFileA( "Log.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
	
	if( hFile == INVALID_HANDLE_VALUE )
	{
		printf( "\nFailed to open local file %s.", "log/log.txt" );
		InternetCloseHandle( url );
	}

	BufferIn.dwBufferTotal = GetFileSize( hFile, NULL );
	printf( "File size is %d\n", BufferIn.dwBufferTotal );

	if( !HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0 ) )
	{
		printf( "Error on HttpSendRequestEx %lu\n", GetLastError( ) );
	}

	DWORD sum = 0;

	do
	{
		if( !( bRead = ReadFile( hFile, pBuffer, sizeof( pBuffer ), &dwBytesRead, NULL ) ) )
		{
			printf ("\nReadFile failed on buffer %lu.", GetLastError( ) );
		}
		if( !( bRet = InternetWriteFile( hRequest, pBuffer, dwBytesRead, &dwBytesWritten ) ) )
		{
			printf ( "\nInternetWriteFile failed %lu", GetLastError( ) );
		}
		
		sum += dwBytesWritten;
	}
	while( dwBytesRead == sizeof( pBuffer ) );

	CloseHandle( hFile );
	printf( "Actual written bytes: %d\n", sum );

	if( !HttpEndRequest( hRequest, NULL, 0, 0 ) )
	{
		printf( "Error on HttpEndRequest %lu \n", GetLastError( ) );
	}