Getting FindFirstUrlCacheEntry to work?

  • Thread starter Thread starter Joe HM
  • Start date Start date
J

Joe HM

Hello -

I am desperately trying to get the FindFirstUrlCacheEntry() function to
work under VB.NET. Unfortunately all I can find when I do a Google are
examples for older VB versions. The "As Any" does not work any more
and there are lots of other problems.

Could anybody tell me how to implement FindFirstUrlCacheEntry in
VB.NET?

Here is what I tried ...

Public Structure FILETIME
Dim dwLowDateTime As Long
Dim dwHighDateTime As Long
End Structure

Public Structure INTERNET_CACHE_ENTRY_INFO
Dim dwStructSize As Long
Dim lpszSourceUrlName As Long
Dim lpszLocalFileName As Long
Dim CacheEntryType As Long 'String
Dim dwUseCount As Long
Dim dwHitRate As Long
Dim dwSizeLow As Long
Dim dwSizeHigh As Long
Dim LastModifiedTime As FILETIME
Dim ExpireTIme As FILETIME
Dim LastAccessTime As FILETIME
Dim LastSyncTime As FILETIME
Dim lpHeaderInfo As Long
Dim dwHeaderInfoSize As Long
Dim lpszFileExtension As Long 'String
Dim dwReserved As Long
End Structure

Public Declare Function FindFirstUrlCacheEntry Lib "wininet" _
Alias "FindFirstUrlCacheEntryA" _
(ByVal lpszUrlSearchPattern As String, _
ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long

....

Dim hFile As Long
Dim lData As INTERNET_CACHE_ENTRY_INFO

hFile = FindFirstUrlCacheEntry("http://msdn.microsoft.com/", lData ,
lBuffer)

This at least does not crash but I do not get any useful data out of
it. The problem must be that some of member variables (e.g.
lpszSourceUrlName) are actually pointers to strings.

How can I convert the Long to a String ... or rather how can I copy the
memory the Long points to into a String?

Thanks for any hints!

Joe
 
Joe,

The first and the main problem of your code is misuse of Long type.
Remember that "Classic" VB's Long corresponds to .NET System.Int32 or VB
..NET Integer. Thus, you have to replace Longs with Integers.

Second, FILETIME is already declared in System.Runtime.InteropServices
namespace, so you must not declare it yourself.

Third, FindFirstUrlCacheEntry() requires to be called specially. Look at the
example:

~
Imports System.Runtime.InteropServices

....

Private Declare Ansi Function FindFirstUrlCacheEntryA Lib "wininet.dll" ( _
<MarshalAs(UnmanagedType.LPStr), [In]()> ByVal lpszUrlSearchPattern As
String, _
ByVal lpFirstCacheEntryInfo As IntPtr, _
ByRef lpdwFirstCacheEntryInfoBufferSize As Int32 _
) As IntPtr

<StructLayout(LayoutKind.Sequential)> Private Structure
INTERNET_CACHE_ENTRY_INFOA
Public dwStructSize As Integer
<MarshalAs(UnmanagedType.LPStr)> Public lpszSourceUrlName As String
<MarshalAs(UnmanagedType.LPStr)> Public lpszLocalFileName As String
Public CacheEntryType As Integer
Public dwUseCount As Integer
Public dwHitRate As Integer
Public dwSizeLow As Integer
Public dwSizeHigh As Integer
Public LastModifiedTime As FILETIME
Public ExpireTime As FILETIME
Public LastAccessTime As FILETIME
Public LastSyncTime As FILETIME
Public lpHeaderInfo As IntPtr
Public dwHeaderInfoSize As Integer
<MarshalAs(UnmanagedType.LPStr)> Public lpszFileExtension As String
Public dwExemptDelta As Integer
End Structure

....

Dim fcei As INTERNET_CACHE_ENTRY_INFOA, fceibs As Integer
FindFirstUrlCacheEntryA(Nothing, IntPtr.Zero, fceibs) REM determine amount
of memory needed
Dim AllocatedSpace As IntPtr = Marshal.AllocHGlobal(fceibs) REM allocate
memory
Dim hFile As IntPtr = FindFirstUrlCacheEntryA("*.*", AllocatedSpace, fceibs)
fcei = CType(Marshal.PtrToStructure(AllocatedSpace, fcei.GetType),
INTERNET_CACHE_ENTRY_INFOA) REM get info
Marshal.FreeHGlobal(AllocatedSpace) REM free memory
~
How can I convert the Long to a String ... or rather how can I copy the
memory the Long points to into a String?

Again Integer, not Long...
Take a look at System.Runtime.InteropServices.Marshal.PtrToString* methods.

I hope it helps.

Roman
 
Back
Top