views:

362

answers:

2

Hi,

I'got a C++ app (similar to this one) that downloads files using URLDownloadToFile method (defined in urlmon.dll). Until IE8, it was possible to abort a download by returning E_ABORT in the progress callback.

On a system on which IE8 has been installed, doing so crashes the program, with an error report generated.

Do anyone know any alternative method to cancel a download launched via URLDownloadToFile ?

+1  A: 

If you're using MFC, have you considered using CInternetSession and CHttpFile?

I'm using these without any problems (IE8 installed)

A quick snippet of code I'm using:

CInternetSession internetSession;

CHttpFile *pHttpFile = 
    reinterpret_cast< CHttpFile* >( internetSession.OpenURL( L"www.example.com" ) );

DWORD dwStatusCode;
if ( pHttpFile->QueryInfoStatusCode( dwStatusCode ) && 
    dwStatusCode == HTTP_STATUS_OK )
{
    char szBuffer[ BUFFER_SIZE ];
    DWORD dwBufferIndex = 0;

    DWORD dwBytesRead = (DWORD)pHttpFile->Read( &szBuffer[ dwBufferIndex ], CHUNK_SIZE );
    while ( dwBytesRead > 0 && dwBufferIndex < BUFFER_SIZE )
    {
        dwBufferIndex += dwBytesRead;

        // break out if cancelled 
        if ( m_bCancel )
            break;

        dwBytesRead = (DWORD)pHttpFile->Read( &szBuffer[ dwBufferIndex ], CHUNK_SIZE );
    }

    szBuffer[ dwBufferIndex ] = '\0';

    // szBuffer now contains the file contents
}

pHttpFile->Close();
delete pHttpFile;

This is what I'm currently using - I need a buffer that contains the contents, however modifying this code to write to a file instead shouldn't be too much of a problem.

Alan
Does these have progress report with stats? It's a must have for my app.
Vinzz
I've modified my answer to give you an idea of how I do it
Alan
+1  A: 

Joel Had an issue like this. He gave up on it and cheated (cheating meaning cancelling a transfer caused the transfer to no longer be visible to the user, but still keep going). WinInet had a bug...

Brian
Well, that's naughty! But a good idea anyway, I'll consider that. thanks.
Vinzz