Microsoft KB Archive/262110

= How To Clear Cache When Your Application Hosts a WebBrowser Control =

Article ID: 262110

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q262110



SUMMARY
When your application hosts a WebBrowser control, there may be a need to clear the cache programmatically. This feature is not available through the interfaces of the WebBrowser control. This article explains how to use WinInet API functions to clear the cache directly.



MORE INFORMATION
Use WinInet API FindFirstURLCacheEntry to find the first cache entry and use FindNextUrlCacheEntry to enumerate through the cache. Use DeleteUrlCacheEntry to delete each entry.

Note that FindFirstUrlCacheGroup, FindNextUrlCacheGroup, and DeleteUrlCacheGroup are used in the following example. These API functions have only became available with Internet Explorer 5, so proper checking as shown is needed to prevent errors.

The following steps show you how to use the WinInet API in Visual Basic to clear all the files in the cache.

 Create a new Visual Basic standard EXE project. Create a command button under form1.  Paste the following code in the module of form1: Option Explicit

Private Declare Function FindFirstUrlCacheGroup Lib &quot;wininet.dll&quot; ( _   ByVal dwFlags As Long, _    ByVal dwFilter As Long, _    ByRef lpSearchCondition As Long, _    ByVal dwSearchCondition As Long, _    ByRef lpGroupId As Date, _    ByRef lpReserved As Long) As Long

Private Declare Function FindNextUrlCacheGroup Lib &quot;wininet.dll&quot; ( _   ByVal hFind As Long, _    ByRef lpGroupId As Date, _    ByRef lpReserved As Long) As Long Private Declare Function DeleteUrlCacheGroup Lib &quot;wininet.dll&quot; ( _   ByVal sGroupID As Date, _    ByVal dwFlags As Long, _    ByRef lpReserved As Long) As Long Private Declare Function FindFirstUrlCacheEntry Lib &quot;wininet.dll&quot; Alias &quot;FindFirstUrlCacheEntryA&quot; ( _   ByVal lpszUrlSearchPattern As String, _    ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _    ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long Private Type INTERNET_CACHE_ENTRY_INFO dwStructSize As Long szRestOfData(1024) As Long End Type

Private Declare Function DeleteUrlCacheEntry Lib &quot;wininet.dll&quot; Alias &quot;DeleteUrlCacheEntryA&quot; ( _   ByVal lpszUrlName As Long) As Long

Private Declare Function FindNextUrlCacheEntry Lib &quot;wininet.dll&quot; Alias &quot;FindNextUrlCacheEntryA&quot; ( _   ByVal hEnumHandle As Long, _    ByRef lpNextCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _    ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long

Private Const CACHGROUP_SEARCH_ALL = &H0 Private Const ERROR_NO_MORE_FILES = 18 Private Const ERROR_NO_MORE_ITEMS = 259 Private Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE = &H2 Private Const BUFFERSIZE = 2048

Private Sub Command1_Click Dim sGroupID As Date Dim hGroup As Long Dim hFile As Long Dim sEntryInfo As INTERNET_CACHE_ENTRY_INFO Dim iSize As Long On Error Resume Next ' Delete the groups hGroup = FindFirstUrlCacheGroup(0, 0, 0, 0, sGroupID, 0) ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented If Err.Number <> 453 Then If (hGroup = 0) And (Err.LastDllError <> 2) Then MsgBox &quot;An error occurred enumerating the cache groups&quot; & Err.LastDllError Exit Sub End If   Else Err.Clear End If   If (hGroup <> 0) Then 'we succeeded in finding the first cache group.. enumerate and 'delete Do           If (0 = DeleteUrlCacheGroup(sGroupID, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0)) Then ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented If Err.Number <> 453 Then MsgBox &quot;Error deleting cache group &quot; & Err.LastDllError Exit Sub Else Err.Clear End If           End If            iSize = BUFFERSIZE If (0 = FindNextUrlCacheGroup(hGroup, sGroupID, iSize)) And (Err.LastDllError <> 2) Then MsgBox &quot;Error finding next url cache group! - &quot; & Err.LastDllError End If       Loop Until Err.LastDllError = 2 End If ' Delete the files sEntryInfo.dwStructSize = 80 iSize = BUFFERSIZE hFile = FindFirstUrlCacheEntry(0, sEntryInfo, iSize) If (hFile = 0) Then If (Err.LastDllError = ERROR_NO_MORE_ITEMS) Then GoTo done End If       MsgBox &quot;ERROR: FindFirstUrlCacheEntry - &quot; & Err.LastDllError Exit Sub End If   Do        If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _ And (Err.LastDllError <> 2) Then Err.Clear End If       iSize = BUFFERSIZE If (0 = FindNextUrlCacheEntry(hFile, sEntryInfo, iSize)) And (Err.LastDllError <> ERROR_NO_MORE_ITEMS) Then MsgBox &quot;Error: Unable to find the next cache entry - &quot; & Err.LastDllError Exit Sub End If   Loop Until Err.LastDllError = ERROR_NO_MORE_ITEMS done: MsgBox &quot;cache cleared&quot; Command1.Enabled = True End Sub  Run your project and click Command1. The cache in your computer will be cleared.

