Downloads bits from the Internet and saves them to a file.
Syntax
HRESULT URLDownloadToFile( LPUNKNOWN pCaller, LPCTSTR szURL, LPCTSTR szFileName, DWORD dwReserved, LPBINDSTATUSCALLBACK lpfnCB );
Parameters
- pCaller
- A pointer to the controlling IUnknown interface of the calling Microsoft ActiveX component, if the caller is an ActiveX component. If the calling application is not an ActiveX component, this value can be set to NULL. Otherwise, the caller is a Component Object Model (COM) object that is contained in another component, such as an ActiveX control in the context of an HTML page. This parameter represents the outermost IUnknown of the calling component. The function attempts the download in the context of the ActiveX client framework, and allows the caller container to receive callbacks on the progress of the download.
- szURL
- A pointer to a string value that contains the URL to download. Cannot be set to NULL. If the URL is invalid, INET_E_DOWNLOAD_FAILURE is returned.
- szFileName
- A pointer to a string value containing the name or full path of the file to create for the download. If szFileName includes a path, the target directory must already exist.
- dwReserved
- Reserved. Must be set to 0.
- lpfnCB
- A pointer to the IBindStatusCallback interface of the caller. By using OnProgress, a caller can receive download status. URLDownloadToFile calls the OnProgress and OnDataAvailable methods as data is received. The download operation can be canceled by returning E_ABORT from any callback. This parameter can be set to NULL if status is not required.
Return Value
Returns one of the following values.
S_OK The download started successfully. E_OUTOFMEMORY The buffer length is invalid, or there is insufficient memory to complete the operation. INET_E_DOWNLOAD_FAILURE The specified resource or callback interface was invalid.
Remarks
URLDownloadToFile binds to a host that supports IBindHost to perform the download. To do this, it first queries the controlling IUnknown passed as pCaller for IServiceProvider, then calls IServiceProvider::QueryService with SID_SBindHost. If pCaller does not support IServiceProvider, IOleObject or IObjectWithSite is used to query the object's host container. If no IBindHost interface is supported, or pCaller is NULL, URLDownloadToFile creates its own bind context to intercept download notifications.
URLDownloadToFile returns S_OK even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile. For best control over the download and its progress, an IBindStatusCallback interface is recommended.
Windows Internet Explorer 8. URLDownloadToFile does not support IBindStatusCallbackEx and cannot be used to download files over 4 gigabytes (GB) in size. Refer instead to IBindStatusCallbackEx::GetBindInfoEx for a code example.
Function Information
Stock Implementation urlmon.dll Custom Implementation No Header Urlmon.h Import library Urlmon.lib Minimum availability Internet Explorer 3.0 Minimum operating systems Windows NT 4.0, Windows 95 Unicode Implemented as ANSI and Unicode versions.
printf("%x",hr);
INET_E_INVALID_URL (800C000)
INET_E_NO_SESSION (800C0003)
INET_E_CANNOT_CONNECT (800C0004)
INET_E_RESOURCE_NOT_FOUND (800C0005)
INET_E_OBJECT_NOT_FOUND (800C0006)
INET_E_DATA_NOT_AVAILABLE (800C0007)
INET_E_DOWNLOAD_FAILURE (800C0008)
INET_E_AUTHENTICATION_REQUIRED (800C0009)
INET_E_NO_VALID_MEDIA (800C000A)
INET_E_CONNECTION_TIMEOUT (800C000B)
INET_E_INVALID_REQUEST (800C000C)
INET_E_UNKNOWN_PROTOCOL (800C000D)
INET_E_SECURITY_PROBLEM (800C000E)
INET_E_CANNOT_LOAD_DATA (800C000F)
INET_E_CANNOT_INSTANTIATE_OBJECT (800C0010)
INET_E_INVALID_CERTIFICATE (800C0019)
INET_E_REDIRECT_FAILED (800C0014)
INET_E_REDIRECT_TO_DIR (800C0015)
INET_E_CANNOT_LOCK_REQUEST (800C0016)
INET_E_USE_EXTEND_BINDING (800C0017)
INET_E_TERMINATED_BIND (800C0018)
INET_E_ERROR_FIRST (800C0002)
INET_E_CODE_DOWNLOAD_DECLINED (800C0100)
INET_E_RESULT_DISPATCHED (800C0200)
INET_E_CANNOT_REPLACE_SFP_FILE (800C0300)
INET_E_CODE_INSTALL_SUPPRESSED (800C0400)
INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY (800C0500)
HRESULT result = URLDownloadToFile(NULL, URL, File, 0, NULL);
switch (result)
{
case S_OK: downloaded = true; break;
case E_OUTOFMEMORY: writeToAllTextboxes("Out of memory error"); break;
case INET_E_DOWNLOAD_FAILURE: writeToAllTextboxes("Cannot access server data"); break;
default: writeToAllTextboxes("Unknown error"); break;
}
the default case seems to be called. Why is that? I'm using Win XP and I'm 100% sure that the site works and the file path and everything is all set.
And probably more importantly for me, how do I fix this?
As documented here, it's possible to ensure downloading the newest version of a file by passing the BINDS_GETNEWESTVERSION flag to the dwReserved parameter:
"Note that the function documentation states that the parameter named dwReserved is reserved and must be set to zero. However, BINDF_GETNEWESTVERSION is a flag value that can be passed into this function as a dwReserved parameter to provided the appropriate functionality."
Source: http://support.microsoft.com/kb/q196466/en-us
I download zip files from web servers inside an ActiveX that runs in a browser. When I log in to a website and try to download the zip file through my ActiveX in firefox, it fails. This worked previously. I have the necessary cookies created on my machine but somehow firefox does not seem to use them anymore. This still works prefectly in IE which downloads the file without any issue.
If the web server does not require a login, the file downloads without any issue.
This function gets stuck during "Sending Request" phase as indicated by the status code in the Progress callback.
I suspect the issue is with some security update in windows XP which has updated urlmon.dll and wininet.dll. I think IE handles the new update but firefox has stopped working because of that. I may be wrong though. If anyone has faced a similar issue, please help me out.
I've got a callback defined, and I thought that something would get reported through there, but no.
Anybody know what's going on?