Other Tips All Tips By Date By Subject
API (33) Bit Manipulation (3) Clipboard (3) Combo Box (5) Desktop (3) GDI (13) Graphics (13) Internet (2) Interprocess Comms (3) Keyboard (2) Mouse (1) Shell (1) Sprites (1) Subclassing (3) Text Box (2) Windows (11) Windows Controls (10)
Submit
|
If you are creating a shortcut to an Internet file, you will want to know
whether it has been visited or not so you can colour it appropriately. This tip
shows you how to determine whether a file is in the cache so you can do this.
Start a new project and add a module. Then add the following code to the module:
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const eeErrorBase = 26720
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type INTERNET_CACHE_ENTRY_INFO
dwStructSize As Long
lpszSourceUrlName As String
lpszLocalFileName As String
CacheEntryType As String
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 String
dwReserved As Long
End Type
Private Declare Function GetUrlCacheEntryInfo Lib "wininet.dll" Alias "GetUrlCacheEntryInfoA" _
(ByVal sUrlName As String, _
lpCacheEntryInfo As Any, _
lpdwCacheEntryInfoBufferSize As Long _
) As Long
' To Report API errors:
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long _
) As Long
Public Function WinAPIError(ByVal lLastDLLError As Long) As String
Dim sBuff As String
Dim lCount As Long
' Return the error message associated with LastDLLError:
sBuff = String$(256, 0)
lCount = FormatMessage( _
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, _
0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
If lCount Then
WinAPIError = Left$(sBuff, lCount)
End If
End Function
Public Function GetCacheEntryInfo(ByVal hWnd As Long, ByVal lpszUrl As String) As Boolean
Dim dwEntrySize As Long
Dim lpCacheEntry As INTERNET_CACHE_ENTRY_INFO
Dim dwTemp As Long
Dim lErr As Long
If (GetUrlCacheEntryInfo(lpszUrl, ByVal 0&, dwEntrySize)) = 0 Then
lErr = Err.LastDllError
If (lErr ERROR_INSUFFICIENT_BUFFER) Then
' Doesn't exist. Raise error containing reason:
Err.Raise eeErrorBase + 1, App.EXEName & ".mCacheEntry", WinAPIError(lErr)
GetCacheEntryInfo = False
Exit Function
Else
' It exists!
GetCacheEntryInfo = True
End If
End If
End Function
To try out a the function, add a Command Button and a Text Box to
your project's form.
Then add this code to the form:
Option Explicit
Private Sub Command1_Click()
On Error Goto ErrorHandler
' Check whether the URL in the text box exists in the cache:
If (GetCacheEntryInfo(Me.hWnd, Text1.Text)) Then
MsgBox "URL In Cache.", vbInformation
Else
MsgBox "URL Not In Cache.", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "URL Not in Cache [" & Err.Description & "]",vbInformation
End Sub
Start the project and type a URL into the text box (for example, http://www.vbaccelerator.com/index.html).
When you click the button, you will get a message saying if the URL is in the cache or not. If Windows
gives a reason for the URL not being found, this will be shown in the square brackets after the
message.
|