dcsimg
Results 1 to 3 of 3

Thread: Searching for a specific URL in IE cache

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    22

    Searching for a specific URL in IE cache

    hello,

    I'm working on a program but i'm having a small problem. I need to search the IE cache for multiple URL's that contain a specific string.


    Basically i'm searching the cache for a URL that contains something like:
    "http://website.com/internet/"
    and I hope to find files like:
    "http://website.com/internet/page.html"
    "http://website.com/internet/page.jv"
    etc...


    I don't need to do anything with the cache, just read the URL and save it as a string. I've been looking into the FindFirstUrlCacheEntry api under WinInet library, but i can't find anything that involves searching the cache.

  2. #2
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: Searching for a specific URL in IE cache

    This code was built to delete the cache, I've commented out the lines that delete Items. Create a form add a listbox. I think this is complete.. Let me know
    Code:
    Imports System
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        ' For PInvoke: Contains information about an entry in the Internet cache
        <StructLayout(LayoutKind.Explicit, Size:=80)> _
        Public Structure INTERNET_CACHE_ENTRY_INFOA1
            <FieldOffset(0)> _
            Public dwStructSize As UInteger
            <FieldOffset(4)> _
            Public lpszSourceUrlName As IntPtr
            <FieldOffset(8)> _
            Public lpszLocalFileName As IntPtr
            <FieldOffset(12)> _
            Public CacheEntryType As UInteger
            <FieldOffset(16)> _
            Public dwUseCount As UInteger
            <FieldOffset(20)> _
            Public dwHitRate As UInteger
            <FieldOffset(24)> _
            Public dwSizeLow As UInteger
            <FieldOffset(28)> _
            Public dwSizeHigh As UInteger
            <FieldOffset(32)> _
            Public LastModifiedTime As ComTypes.FILETIME
            <FieldOffset(40)> _
            Public ExpireTime As ComTypes.FILETIME
            <FieldOffset(48)> _
            Public LastAccessTime As ComTypes.FILETIME
            <FieldOffset(56)> _
            Public LastSyncTime As ComTypes.FILETIME
            <FieldOffset(64)> _
            Public lpHeaderInfo As IntPtr
            <FieldOffset(68)> _
            Public dwHeaderInfoSize As UInteger
            <FieldOffset(72)> _
            Public lpszFileExtension As IntPtr
            <FieldOffset(76)> _
            Public dwReserved As UInteger
            <FieldOffset(76)> _
            Public dwExemptDelta As UInteger
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> Public Structure INTERNET_CACHE_ENTRY_INFO
            Public dwStructSize As UInt32
            Public lpszSourceUrlName As String
            Public lpszLocalFileName As String
            Public CacheEntryType As UInt32
            Public dwUseCount As UInt32
            Public dwHitRate As UInt32
            Public dwSizeLow As UInt32
            Public dwSizeHigh As UInt32
            Public LastModifiedTime As ComTypes.FILETIME
            Public ExpireTime As ComTypes.FILETIME
            Public LastAccessTime As ComTypes.FILETIME
            Public LastSyncTime As ComTypes.FILETIME
            Public lpHeaderInfo As IntPtr
            Public dwHeaderInfoSize As UInt32
            Public lpszFileExtension As String
            Public dwExemptDelta As UInt32
        End Structure
    
        ' For PInvoke: Initiates the enumeration of the cache groups in the Internet cache
        <DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindFirstUrlCacheGroup", CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function FindFirstUrlCacheGroup(ByVal dwFlags As Integer, ByVal dwFilter As Integer, ByVal lpSearchCondition As IntPtr, ByVal dwSearchCondition As Integer, ByRef lpGroupId As Long, ByVal lpReserved As IntPtr) As IntPtr
        End Function
    
        ' For PInvoke: Retrieves the next cache group in a cache group enumeration
        <DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindNextUrlCacheGroup", CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function FindNextUrlCacheGroup(ByVal hFind As IntPtr, ByRef lpGroupId As Long, ByVal lpReserved As IntPtr) As Boolean
        End Function
    
        ' For PInvoke: Releases the specified GROUPID and any associated state in the cache index file
        <DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="DeleteUrlCacheGroup", CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function DeleteUrlCacheGroup(ByVal GroupId As Long, ByVal dwFlags As Integer, ByVal lpReserved As IntPtr) As Boolean
        End Function
    
        ' For PInvoke: Begins the enumeration of the Internet cache
        <DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindFirstUrlCacheEntryA", CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function FindFirstUrlCacheEntry(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszUrlSearchPattern As String, ByVal lpFirstCacheEntryInfo As IntPtr, ByRef lpdwFirstCacheEntryInfoBufferSize As Integer) As IntPtr
        End Function
    
        ' For PInvoke: Retrieves the next entry in the Internet cache
        <DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindNextUrlCacheEntryA", CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function FindNextUrlCacheEntry(ByVal hFind As IntPtr, ByVal lpNextCacheEntryInfo As IntPtr, ByRef lpdwNextCacheEntryInfoBufferSize As Integer) As Boolean
        End Function
    
        ' For PInvoke: Removes the file that is associated with the source name from the cache, if the file exists
        <DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="DeleteUrlCacheEntryA", CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function DeleteUrlCacheEntry(ByVal lpszUrlName As IntPtr) As Boolean
        End Function
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Indicates that all of the cache groups in the user's system should be enumerated
            Const CACHEGROUP_SEARCH_ALL As Integer = &H0
            ' Indicates that all the cache entries that are associated with the cache group
            ' should be deleted, unless the entry belongs to another cache group.
            Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE As Integer = &H2
            ' File not found.
            Const ERROR_FILE_NOT_FOUND As Integer = &H2
            ' No more items have been found.
            Const ERROR_NO_MORE_ITEMS As Integer = 259
            ' Pointer to a GROUPID variable
            Dim groupId As Long = 0
    
            ' Local variables
            Dim cacheEntryInfoBufferSizeInitial As Integer = 0
            Dim cacheEntryInfoBufferSize As Integer = 0
            Dim cacheEntryInfoBuffer As IntPtr = IntPtr.Zero
            Dim internetCacheEntry As INTERNET_CACHE_ENTRY_INFO
            Dim enumHandle As IntPtr = IntPtr.Zero
            Dim returnValue As Boolean = False
    
            ' Delete the groups first.
            ' Groups may not always exist on the system.
            ' For more information, visit the following Microsoft Web site:
            ' http://msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp			
            ' By default, a URL does not belong to any group. Therefore, that cache may become
            ' empty even when the CacheGroup APIs are not used because the existing URL does not belong to any group.			
            enumHandle = FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, groupId, IntPtr.Zero)
            ' If there are no items in the Cache, you are finished.
            If enumHandle <> IntPtr.Zero AndAlso ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() Then
                Exit Sub
            End If
    
            ' Loop through Cache Group, and then delete entries.
            While True
                ' Delete a particular Cache Group.
                'returnValue = DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero)
                'If Not returnValue AndAlso ERROR_FILE_NOT_FOUND = Marshal.GetLastWin32Error() Then
                returnValue = FindNextUrlCacheGroup(enumHandle, groupId, IntPtr.Zero)
                'End If
    
                If Not returnValue AndAlso (ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() OrElse ERROR_FILE_NOT_FOUND = Marshal.GetLastWin32Error()) Then
                    Exit While
                End If
            End While
    
            ' Start to delete URLs that do not belong to any group.
            enumHandle = FindFirstUrlCacheEntry(Nothing, IntPtr.Zero, cacheEntryInfoBufferSizeInitial)
            If enumHandle = IntPtr.Zero AndAlso ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() Then
                Exit Sub
            End If
    
            cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
            cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize)
            enumHandle = FindFirstUrlCacheEntry(Nothing, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
    
            While True
                internetCacheEntry = DirectCast(Marshal.PtrToStructure(cacheEntryInfoBuffer, GetType(INTERNET_CACHE_ENTRY_INFO)), INTERNET_CACHE_ENTRY_INFO)
    
                cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize
                Dim cachefile As String
                cachefile = internetCacheEntry.lpszSourceUrlName
                ListBox1.Items.Add(cachefile)
                'returnValue = DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName)
                'If Not returnValue Then
                returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
                'End If
                If Not returnValue AndAlso ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() Then
                    Exit While
                End If
                If Not returnValue AndAlso cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize Then
                    cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
                    Dim ptr1 As IntPtr
                    ptr1 = New IntPtr(cacheEntryInfoBufferSize)
                    cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, ptr1) 'DirectCast(cacheEntryInfoBufferSize, IntPtr))
                    returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
                End If
            End While
            Marshal.FreeHGlobal(cacheEntryInfoBuffer)
        End Sub
    End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    22

    Re: Searching for a specific URL in IE cache

    It works Perfectly! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width