Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


In this article I will show you the use of FindFirstUrlCacheEntry, FindNextUrlCacheEntry, FindCloseUrlCache and DeleteUrlCacheEntry APIs. First three APIs are used to retrive cache entries and DeleteUrlCacheEntry is used to delete a specific entry from the cache.

Here is the example

Step-By-Step Example

- Create a standard exe project
- Add two command buttons, one combo box, one listbox and one textbox control on the form1
- Add the following code in form1

Click here to copy the following block
Option Explicit

Const MAX_ENTRIES = 5000

Private Const ERROR_CACHE_FIND_FAIL As Long = 0
Private Const ERROR_CACHE_FIND_SUCCESS As Long = 1
Private Const ERROR_FILE_NOT_FOUND As Long = 2
Private Const ERROR_ACCESS_DENIED As Long = 5
Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122
Private Const MAX_PATH  As Long = 260
Private Const MAX_CACHE_ENTRY_INFO_SIZE As Long = 4096

Private Const LMEM_FIXED As Long = &H0
Private Const LMEM_ZEROINIT As Long = &H40
Private Const LPTR As Long = (LMEM_FIXED Or LMEM_ZEROINIT)

Private Const NORMAL_CACHE_ENTRY As Long = &H1
Private Const EDITED_CACHE_ENTRY As Long = &H8
Private Const TRACK_OFFLINE_CACHE_ENTRY As Long = &H10
Private Const TRACK_ONLINE_CACHE_ENTRY As Long = &H20
Private Const STICKY_CACHE_ENTRY As Long = &H40
Private Const SPARSE_CACHE_ENTRY As Long = &H10000
Private Const COOKIE_CACHE_ENTRY As Long = &H100000
Private Const URLHISTORY_CACHE_ENTRY As Long = &H200000
Private Const URLCACHE_FIND_DEFAULT_FILTER As Long = NORMAL_CACHE_ENTRY Or _
    COOKIE_CACHE_ENTRY Or _
    URLHISTORY_CACHE_ENTRY Or _
    TRACK_OFFLINE_CACHE_ENTRY Or _
    TRACK_ONLINE_CACHE_ENTRY Or _
    STICKY_CACHE_ENTRY
Private Type FILETIME
  dwLowDateTime As Long
  dwHighDateTime As Long
End Type

Private Type INTERNET_CACHE_ENTRY_INFO
  dwStructSize As Long
  lpszSourceUrlName As Long
  lpszLocalFileName As Long
  CacheEntryType As Long
  dwUseCount As Long
  dwHitRate As Long
  dwSizeLow As Long
  dwSizeHigh As Long
  LastModifiedTime As FILETIME
  ExpireTime As FILETIME
  LastAccessTime As FILETIME
  LastSyncTime As FILETIME
  lpHeaderInfo As Long
  dwHeaderInfoSize As Long
  lpszFileExtension As Long
  dwExemptDelta As Long
End Type

Private Declare Function FindFirstUrlCacheEntry Lib "Wininet.dll" _
    Alias "FindFirstUrlCacheEntryA" _
    (ByVal lpszUrlSearchPattern As String, _
    lpFirstCacheEntryInfo As Any, _
    lpdwFirstCacheEntryInfoBufferSize As Long) As Long

Private Declare Function FindNextUrlCacheEntry Lib "Wininet.dll" _
    Alias "FindNextUrlCacheEntryA" _
    (ByVal hEnumHandle As Long, _
    lpNextCacheEntryInfo As Any, _
    lpdwNextCacheEntryInfoBufferSize As Long) As Long

Private Declare Function FindCloseUrlCache Lib "Wininet.dll" _
    (ByVal hEnumHandle As Long) As Long

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
  Alias "DeleteUrlCacheEntryA" _
 (ByVal lpszUrlName As String) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" _
    (pDest As Any, _
    pSource As Any, _
    ByVal dwLength As Long)

Private Declare Function lstrcpyA Lib "kernel32" _
    (ByVal RetVal As String, ByVal Ptr As Long) As Long

Private Declare Function lstrlenA Lib "kernel32" _
    (ByVal Ptr As Any) As Long

Private Declare Function LocalAlloc Lib "kernel32" _
    (ByVal uFlags As Long, _
    ByVal uBytes As Long) As Long

Private Declare Function LocalFree Lib "kernel32" _
    (ByVal hMem As Long) As Long


Private Sub Command2_Click()
  Dim cachefile As String
  Dim i As Long, ret As Long
  
 'delete all files loaded in the listbox
  For i = 0 To List1.ListCount - 1
   cachefile = List1.List(i)
   ret = DeleteUrlCacheEntry(cachefile)
  Next
  
  'Reload the list
  Command1_Click
End Sub

Private Sub Form_Load()
  Command1.Caption = "Load Entries"
  Command2.Caption = "Delete All Entries"
  With Combo1
    .AddItem "Normal Entry": .ItemData(.NewIndex) = NORMAL_CACHE_ENTRY
    .AddItem "Edited Entry (IE5)": .ItemData(.NewIndex) = EDITED_CACHE_ENTRY
    .AddItem "Offline Entry": .ItemData(.NewIndex) = TRACK_OFFLINE_CACHE_ENTRY
    .AddItem "Online Entry": .ItemData(.NewIndex) = TRACK_ONLINE_CACHE_ENTRY
    .AddItem "Stick Entry": .ItemData(.NewIndex) = STICKY_CACHE_ENTRY
    .AddItem "Sparse Entry": .ItemData(.NewIndex) = SPARSE_CACHE_ENTRY
    .AddItem "Cookies": .ItemData(.NewIndex) = COOKIE_CACHE_ENTRY
    .AddItem "Visited History": .ItemData(.NewIndex) = URLHISTORY_CACHE_ENTRY
    .AddItem "Default Filter": .ItemData(.NewIndex) = URLCACHE_FIND_DEFAULT_FILTER
    .ListIndex = 0
  End With
End Sub

Private Sub Command1_Click()
  Dim TotalEntries As Long
  
  Me.Caption = "Loading Entries ..."
  List1.Clear

  TotalEntries = LoadCach(Combo1.ItemData(Combo1.ListIndex))
  Me.Caption = Format$(List1.ListCount, "###,###,###,##0") & " Entries found for " & Combo1.Text
  MsgBox Me.Caption
End Sub

Private Sub List1_Click()
  Text1.Text = List1.List(List1.ListIndex)
End Sub

Private Function LoadCach(CacheType As Long) As Long

  Dim CacheEntry As INTERNET_CACHE_ENTRY_INFO
  Dim hFile As Long
  Dim cachefile As String
  Dim nCount As Long
  Dim dwBuffer As Long
  Dim ptrCacheEntry As Long
  Dim ret As Long

  dwBuffer = 0

  '//to know exact size of the buffer first call FindFirstUrlCacheEntry without second argument
  hFile = FindFirstUrlCacheEntry(vbNullString, ByVal 0, dwBuffer)

  If (hFile = ERROR_CACHE_FIND_FAIL) And _
      (Err.LastDllError = ERROR_INSUFFICIENT_BUFFER) Then

    '//now allocate the buffer and store the buffer memory location in ptrCacheEntry
    ptrCacheEntry = LocalAlloc(LMEM_FIXED, dwBuffer)

    If ptrCacheEntry <> 0 Then

      'set a Long pointer to the memory location
      CopyMemory ByVal ptrCacheEntry, dwBuffer, 4

      'Call FindFirstUrlCacheEntry again and this time pass second argument
      hFile = FindFirstUrlCacheEntry(vbNullString, ByVal ptrCacheEntry, dwBuffer)

      If hFile <> ERROR_CACHE_FIND_FAIL Then

        'now just loop through the cache to find all available entries
        Do
          '//fill CacheEntry variable from memorylocation
          CopyMemory CacheEntry, ByVal ptrCacheEntry, Len(CacheEntry)

          If (CacheEntry.CacheEntryType And CacheType) Then
            cachefile = GetStrFromPtrA(CacheEntry.lpszSourceUrlName)
            List1.AddItem cachefile
            nCount = nCount + 1
          End If
          
          '//free the memory for last accessed entry
          Call LocalFree(ptrCacheEntry)
          
          If nCount > MAX_ENTRIES Then
            ret = MsgBox("Maximum limit is set to " & MAX_ENTRIES & ". Do you want to continue?", vbYesNo + vbDefaultButton2)
            If ret = vbNo Then Exit Do
          End If
          
          dwBuffer = 0
          Call FindNextUrlCacheEntry(hFile, ByVal 0, dwBuffer)

          'allocate and assign the memory to the pointer
          ptrCacheEntry = LocalAlloc(LMEM_FIXED, dwBuffer)
          CopyMemory ByVal ptrCacheEntry, dwBuffer, 4

          '//Loop until we reach the end for a specified cach type
        Loop While FindNextUrlCacheEntry(hFile, ByVal ptrCacheEntry, dwBuffer)

      End If
    End If
  End If

  '//cleanup
  Call LocalFree(ptrCacheEntry)
  Call FindCloseUrlCache(hFile)

  LoadCach = nCount
End Function

Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
  GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
  Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.