by Mehmet Kirazoglu (1 Submission)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 3rd October 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Deleting Temporary Internet Files...
API Declarations
Public Const ERROR_CACHE_FIND_FAIL As Long = 0
Public Const ERROR_CACHE_FIND_SUCCESS As Long = 1
Public Const ERROR_FILE_NOT_FOUND As Long = 2
Public Const ERROR_ACCESS_DENIED As Long = 5
Public Const ERROR_INSUFFICIENT_BUFFER As Long = 122
Public Const MAX_PATH As Long = 260
Public Const MAX_CACHE_ENTRY_INFO_SIZE As Long = 4096
Public Const LMEM_FIXED As Long = &H0
Public Const LMEM_ZEROINIT As Long = &H40
Public Const LPTR As Long = (LMEM_FIXED Or LMEM_ZEROINIT)
Public Const NORMAL_CACHE_ENTRY As Long = &H1
Public Const EDITED_CACHE_ENTRY As Long = &H8
Public Const TRACK_OFFLINE_CACHE_ENTRY As Long = &H10
Public Const TRACK_ONLINE_CACHE_ENTRY As Long = &H20
Public Const STICKY_CACHE_ENTRY As Long = &H40
Public Const SPARSE_CACHE_ENTRY As Long = &H10000
Public Const COOKIE_CACHE_ENTRY As Long = &H100000
Public Const URLHISTORY_CACHE_ENTRY As Long = &H200000
Public 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
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public 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
Public Declare Function FindFirstUrlCacheEntry Lib "wininet" _
Alias "FindFirstUrlCacheEntryA" _
(ByVal lpszUrlSearchPattern As String, _
lpFirstCacheEntryInfo As Any, _
lpdwFirstCacheEntryInfoBufferSize As Long) As Long
Public Declare Function FindNextUrlCacheEntry Lib "wininet" _
Alias "FindNextUrlCacheEntryA" _
(ByVal hEnumHandle As Long, _
lpNextCacheEntryInfo As Any, _
lpdwNextCacheEntryInfoBufferSize As Long) As Long
Public Declare Function FindCloseUrlCache Lib "wininet" _
(ByVal hEnumHandle As Long) As Long
Public Declare Function DeleteUrlCacheEntry Lib "wininet" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Public Declare Function lstrcpyA Lib "kernel32" _
(ByVal RetVal As String, ByVal Ptr As Long) As Long
Public Declare Function lstrlenA Lib "kernel32" _
(ByVal Ptr As Any) As Long
Public Declare Function LocalAlloc Lib "kernel32" _
(ByVal uFlags As Long, _
ByVal uBytes As Long) As Long
Public Declare Function LocalFree Lib "kernel32" _
(ByVal hMem As Long) As Long
To a form, add a listbox (List1), three command buttons (Command1/2/3) and a label (Label1). Add the following code:
--------------------------------------------------------------------------------
Option Explicit
Private Sub Command1_Click()
'load the cached file list
GetCacheURLList
Label1.Caption = List1.ListCount & " files listed."
End Sub
Private Sub Command2_Click()
Dim cachefile As String
'delete the selected file
cachefile = List1.List(List1.ListIndex)
Call DeleteUrlCacheEntry(cachefile)
'reload the list
GetCacheURLList
End Sub
Private Sub Command3_Click()
Dim cachefile As String
Dim i As Long
'delete all files except..
For i = 0 To List1.ListCount - 1
cachefile = List1.List(i)
'..if the file is a cookie, don't screw
'up saved passwords, so skip it
If InStr(cachefile, "Cookie") = 0 Then
Call DeleteUrlCacheEntry(cachefile)
End If
Next
'reload the list
GetCacheURLList
End Sub
Private Sub List1_Click()
Command2.Enabled = InStr(List1.List(List1.ListIndex), "Cookie") = 0
End Sub
Public Sub GetCacheURLList()
Dim ICEI As INTERNET_CACHE_ENTRY_INFO
Dim hFile As Long
Dim cachefile As String
Dim posUrl As Long
Dim posEnd As Long
Dim dwBuffer As Long
Dim pntrICE As Long
List1.Clear
'Like other APIs, calling FindFirstUrlCacheEntry or
'FindNextUrlCacheEntry with an insufficient buffer will
'cause the API to fail, and the buffer pointing to the
'correct size required for a successful call.
dwBuffer = 0
'Call to determine the required buffer size
hFile = FindFirstUrlCacheEntry(0&, ByVal 0, dwBuffer)
'both conditions hould be met by the first call
If (hFile = ERROR_CACHE_FIND_FAIL) And _
(Err.LastDllError = ERROR_INSUFFICIENT_BUFFER) Then
'The INTERNET_CACHE_ENTRY_INFO data type is a
'variable-length type. It is neccessary to allocate
'memnory for the result of the call and pass the
'pointer to this memory location to the API.
pntrICE = LocalAlloc(LMEM_FIXED, dwBuffer)
'allocation successful
If pntrICE Then
'set a Long pointer to the memory location
CopyMemory ByVal pntrICE, dwBuffer, 4
'and call the first find API again passing the
'pointer to the allocated memory
hFile = FindFirstUrlCacheEntry(vbNullString, ByVal pntrICE, dwBuffer)
'hfile should = 1 (success)
If hFile <> ERROR_CACHE_FIND_FAIL Then
'loop through the cache
Do
'the pointer has ben filled, so move the
'data back into a ICEI structure
CopyMemory ICEI, ByVal pntrICE, Len(ICEI)
'CacheEntryType is a long representing
'the type of entry returned
If (ICEI.CacheEntryType And _
NORMAL_CACHE_ENTRY) = NORMAL_CACHE_ENTRY Then
'extract the string from the memory location
'pointed to by the lpszSourceUrlName member
'and add to a list
cachefile = GetStrFromPtrA(ICEI.lpszSourceUrlName)
List1.AddItem cachefile
End If
'free the pointer and memory associated
'with the last-retrieved file
Call LocalFree(pntrICE)
'and again repeat the procedure, this time calling
'FindNextUrlCacheEntry with a buffer size set to 0.
'This will cause the call to once again fail,
'returning the required size as dwBuffer
dwBuffer = 0
Call FindNextUrlCacheEntry(hFile, ByVal 0, dwBuffer)
'allocate and assign the memory to the pointer
pntrICE = LocalAlloc(LMEM_FIXED, dwBuffer)
CopyMemory ByVal pntrICE, dwBuffer, 4
'and call again with the valid parameters.
'If the call fails (no more data), the loop exits.
'If the call is successful, the Do portion of the
'loop is executed again, extracting the data from
'the returned type
Loop While FindNextUrlCacheEntry(hFile, ByVal pntrICE, dwBuffer)
End If 'hFile
End If 'pntrICE
End If 'hFile
'clean up by closing the find handle, as
'well as calling LocalFree again to be safe
Call LocalFree(pntrICE)
Call FindCloseUrlCache(hFile)
End Sub
Public Function GetStrFromPtrA(ByVal lpszA As Long) As String
GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function