================================================================================
IE Extension: VBIEDownloadManager
===============================================================================
/////////////////////////////////////////////////////////////////////////////
Summary:
The sample demonstrates how to implement a custom download manager for IE.
When IE starts to download a file, the VBWebDownloader.exe will be launched to
download the file.
NOTE: Some third party download extensions for IE may conflict with this code sample,
so it is better to disable them temporarily before you try out the sample.
/////////////////////////////////////////////////////////////////////////////
Setup and Removal:
A. Setup
For 32bit IE on x86 or x64 OS, install VBIEDownloadManagerSetup(x86).msi, the output
of the VBIEDownloadManagerSetup(x86) setup project.
For 64bit IE on x64 OS, install VBIEDownloadManagerSetup(x64).msi outputted by the
VBIEDownloadManagerSetup(x64) setup project.
B. Removal
For 32bit IE on x86 or x64 OS, uninstall VBIEDownloadManagerSetup(x86).msi, the
output of the VBIEDownloadManagerSetup(x86) setup project.
For 64bit IE on x64 OS, uninstall VBIEDownloadManagerSetup(x64).msi, the output of
the VBIEDownloadManagerSetup(x64) setup project.
////////////////////////////////////////////////////////////////////////////////
Demo:
Step1. Open this project in VS2010 and set the platform of the solution to x86. Make
sure that the projects VBIEDownloadManagerSetup, VBWebDownloader and
VBIEDownloadManagerSetup(x86) are selected to build in Configuration Manager.
NOTE: If you want to run this sample in 64bit IE, set the platform to x64 and
select VBIEDownloadManagerSetup, VBWebDownloader and
VBIEDownloadManagerSetup(x64) to build.
Step2. Build the solution.
Step3. Right click the project VBIEDownloadManagerSetup(x86) in Solution Explorer,
and choose "Install".
Step4. Open 32bit IE and visit the the download link of Microsoft .NET Framework 4
http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=0a391abd-25c1-4fc0-919f-b21f31ab88b7.
Click the "Download" button on this page, and then VBWebDownloader.exe will
be launched. In VBWebDownloader.exe, you will find that the url is
http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe
and the local path is D:\dotNetFx40_Full_x86_x64.exe
Step5. Click the button "Download" in VBWebDownloader.exe, it will start to download
the file, and after a few minutes, you will find a file D:\dotNetFx40_Full_x86_x64.exe
in Windows Explorer.
/////////////////////////////////////////////////////////////////////////////
Code Logic:
A. Create the VBWebDownloader project with following features:
1. Set the buffer and cache size.
2. Download a specified block data of the whole file.
3. Start, Pause, Resume and Cancel a download.
4. Supply the file size, download speed and used time.
5. Expose the events StatusChanged, DownloadProgressChanged and DownloadCompleted.
6. Can be launched with an argument (the file to download).
For more detailed information about the VBWebDownloader.exe, see
http://code.msdn.microsoft.com/VBWebDownloader-5fdcfb74
B. Create the VBIEDownloadManager project.
In Visual Studio 2010, create a Visual Basic / Windows / Class Library project
named "VBIEDownloadManager".
The ability to implement a custom download manager was introduced in Microsoft
Internet Explorer 5.5. This feature enables you to extend the functionality of
Windows Internet Explorer and WebBrowser applications by implementing a Component
Object Model (COM) object to handle the file download process.
A download manager is implemented as a COM object that exposes the IUnknown and
IDownloadManager interface. IDownloadManager has only one method,
IDownloadManager::Download, which is called by Internet Explorer or a WebBrowser
application to download a file.
For Internet Explorer 6 and later, if the WebBrowser application does not implement
the IServiceProvider::QueryService method, or when using Internet Explorer itself
for which IServiceProvider::QueryService cannot be implemented, the application
checks for the presence of a registry key containing the class identifier (CLSID)
of the download manager COM object. The CLSID can be provided in either of the
following registry values.
HKEY_LOCAL_MACHINE
Software
Microsoft
Internet Explorer
DownloadUI
HKEY_CURRENT_USER
Software
Microsoft
Internet Explorer
DownloadUI
DownloadUI is not a key, it is a REG_SZ value under Software\Microsoft\Internet Explorer.
If the application cannot locate a custom download manager the default download user
interface is used.
The IEDownloadManager class implements the IDownloadManager interface. When IE starts to
download a file, the Download method of this class will be called, and then the
VBWebDownloader.exe will be launched to download the file.
Public Function Download(ByVal pmk As IMoniker, ByVal pbc As IBindCtx,
ByVal dwBindVerb As UInteger, ByVal grfBINDF As Integer,
ByVal pBindInfo As IntPtr, ByVal pszHeaders As String,
ByVal pszRedir As String, ByVal uiCP As UInteger) As Integer Implements NativeMethods.IDownloadManager.Download
' Get the display name of the pointer to an IMoniker interface that specifies
' the object to be downloaded.
Dim name As String = String.Empty
pmk.GetDisplayName(pbc, Nothing, name)
If Not String.IsNullOrEmpty(name) Then
Dim url As Uri = Nothing
Dim result As Boolean = Uri.TryCreate(name, UriKind.Absolute, url)
If result Then
' Launch VBWebDownloader.exe to download the file.
Dim assemblyFile As New FileInfo(
System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim start As ProcessStartInfo = New ProcessStartInfo With _
{.Arguments = name,
.FileName = String.Format("{0}\VBWebDownloader.exe", assemblyFile.DirectoryName)}
Process.Start(start)
Return 0
End If
End If
Return 1
End Function
This class will also set the registry values when this assembly is registered to COM.
''' <summary>
''' Called when derived class is registered as a COM server.
''' </summary>
<ComRegisterFunction()>
Public Shared Sub Register(ByVal t As Type)
Dim ieKeyPath As String = "SOFTWARE\Microsoft\Internet Explorer\"
Using ieKey As RegistryKey = Registry.LocalMachine.CreateSubKey(ieKeyPath)
ieKey.SetValue("DownloadUI", t.GUID.ToString("B"))
End Using
End Sub
''' <summary>
''' Called when derived class is unregistered as a COM server.
''' </summary>
<ComUnregisterFunction()>
Public Shared Sub Unregister(ByVal t As Type)
Dim ieKeyPath As String = "SOFTWARE\Microsoft\Internet Explorer\"
Using ieKey As RegistryKey = Registry.LocalMachine.OpenSubKey(ieKeyPath, True)
ieKey.DeleteValue("DownloadUI")
End Us