« IE7/IE8のWinINet関数のANSI版(~A)では、シフトJISではなく、UTF-8を使う。 | トップページ | PowerShell 2.0 PS1ファイルの関連付け »

2009年10月15日 (木)

GetCacheEntryInfoW()を使ってURLからキャッシュのパスを調べる。

GetCacheEntryInfoW()を使ってURLからキャッシュのパスを調べるVBAのサンプルです。

Option Explicit

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
  dwReserved As Long
  Buffer(0 To 1023) As Byte
End Type

Private Declare Function GetUrlCacheEntryInfo Lib "wininet.dll" Alias "GetUrlCacheEntryInfoW" (ByVal sUrlName As Long, lpCacheEntryInfo As Any, lpdwCacheEntryInfoBufferSize As Long) As Long

Public Function GetCacheFileName(ByVal lpszUrl As String) As String
Dim dwEntrySize As Long
Dim lpCacheEntry As INTERNET_CACHE_ENTRY_INFO
dwEntrySize = Len(lpCacheEntry)
If GetUrlCacheEntryInfo(StrPtr(lpszUrl), lpCacheEntry, dwEntrySize) Then
  GetCacheFileName = MidBW(lpCacheEntry.Buffer, lpCacheEntry.lpszLocalFileName - VarPtr(lpCacheEntry) - 80)
End If
End Function

Function MidBW(Bytes() As Byte, lb As Long) As String
If lb < 0 Then Exit Function
Dim s As String
s = Bytes
s = MidB(s, lb + 1)
MidBW = Left(s, InStr(s, vbNullChar) - 1)
End Function

Sub aaa()
Debug.Print GetCacheFileName(URL)
End Sub

« IE7/IE8のWinINet関数のANSI版(~A)では、シフトJISではなく、UTF-8を使う。 | トップページ | PowerShell 2.0 PS1ファイルの関連付け »