how to use URLDownloadToFile on a IE8 box (no crash would be nice)?
-
06-07-2019 - |
Question
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
?
Solution
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...
OTHER TIPS
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.