Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

URLDownloadToFile (urlmon)
 
.
Summary
Download a file from a URL without user prompt

C# Signature:

/// <summary>
/// The URLMON library contains this function, URLDownloadToFile, which is a way
/// to download files without user prompts.  The ExecWB( _SAVEAS ) function always
/// prompts the user, even if _DONTPROMPTUSER parameter is specified, for "internet
/// security reasons".  This function gets around those reasons.
/// </summary>
/// <param name="pCaller">Pointer to caller object (AX).</param>
/// <param name="szURL">String of the URL.</param>
/// <param name="szFileName">String of the destination filename/path.</param>
/// <param name="dwReserved">[reserved].</param>
/// <param name="lpfnCB">A callback function to monitor progress or abort.</param>
/// <returns>0 for okay.</returns>
[DllImport("urlmon.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern Int32 URLDownloadToFile (
    [MarshalAs(UnmanagedType.IUnknown)] object pCaller,
    [MarshalAs(UnmanagedType.LPWStr)] string szURL,
    [MarshalAs(UnmanagedType.LPWStr)] string szFileName,
    Int32 dwReserved,
    IntPtr lpfnCB);

VB Signature:

Declare Function URLDownloadToFile  Lib "urlmon.dll" (TODO) As TODO

User-Defined Types:

None.

Notes:

This can be used in place of IWebBrowser2.ExecWB( Save-As, Dont-Prompt ) which will always prompt the user anyway. The function does not return until the transfer finishes or fails.

Tips & Tricks:

Please add some!

Sample Code:

    int response = URLDownloadToFile( null, urlStr, fileToStoreStr, 0, IntPtr.Zero );

// Here's a sample class to use a background thread to download a file, then call an optional callback

Alternative Managed API:

Do you know one? Please contribute it!

    /// <summary>
    /// spins a background thread to download the file for us
    /// </summary>
    public class DownloadFileThread
    {
    private DownloadCompleteCallback callback;
    private string sDownloadURL;
    private string sDownloadFile;
    private Form frmCaller;

    [DllImport("URLMON.DLL", EntryPoint = "URLDownloadToFileW", SetLastError = true,
         CharSet = CharSet.Unicode, ExactSpelling = true,
         CallingConvention = CallingConvention.StdCall)]
    public static extern int URLDownloadToFile(int pCaller, string srcURL,
        string dstFile, int Reserved, int CallBack);

    public DownloadFileThread(string sURL, string sFile, DownloadCompleteCallback callbackDelegate, Form frm)
    {
        callback = callbackDelegate;
        sDownloadURL = sURL;
        sDownloadFile = sFile;
        frmCaller = frm;
    }

    public void ThreadProc()
    {
        URLDownloadToFile(0, sDownloadURL, sDownloadFile, 0, 0);
        if (callback != null)
        {
        callback(sDownloadFile, frmCaller);
        }
    }
    }

// call this class with code like:

                DownloadFileThread dft = new DownloadFileThread(
                sURLToDownload,
                sWhereToSaveOnDisk,
                new DownloadCompleteCallback(DownloadCompleteCallback),
                this);
                Thread t = new Thread(new ThreadStart(dft.ThreadProc));
                t.Start();

// and the callback

    private static void DownloadCompleteCallback(string sFile, Form frm)
    {
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
URLDownloadToFile @msdn on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions