Open GoogleCodeExporter opened 8 years ago
Hi MH,
Here is a new proposal to correct the bug in FindUrlCacheEntries :
public static ArrayList FindUrlCacheEntries(string urlPattern)
{
ArrayList results = new ArrayList();
IntPtr buffer = IntPtr.Zero;
UInt32 structSize;
//This call will fail but returns the size required in structSize
//to allocate necessary buffer
IntPtr hEnum = FindFirstUrlCacheEntry(urlPattern, buffer, out structSize);
try
{
int lastErrorCacheEntry = 0;
if (hEnum == IntPtr.Zero)
{
lastErrorCacheEntry = Marshal.GetLastWin32Error();
if (lastErrorCacheEntry == Hresults.ERROR_INSUFFICIENT_BUFFER)
{
//Allocate buffer
buffer = Marshal.AllocHGlobal((int)structSize);
//Call again, this time it should succeed
hEnum = FindFirstUrlCacheEntry(urlPattern, buffer, out
structSize);
// get last error so we can detect there are no more items
lastErrorCacheEntry = Marshal.GetLastWin32Error();
}
if (lastErrorCacheEntry == Hresults.ERROR_NO_MORE_ITEMS)
{
return results;
}
}
// To avoid crash
if (buffer == IntPtr.Zero)
{
throw new ApplicationException("Unable to find first url cache
entry, error=" + lastErrorCacheEntry);
}
INTERNET_CACHE_ENTRY_INFO result =
(INTERNET_CACHE_ENTRY_INFO)Marshal.PtrToStructure(buffer,
typeof(INTERNET_CACHE_ENTRY_INFO));
try
{
if (Regex.IsMatch(result.lpszSourceUrlName, urlPattern,
RegexOptions.IgnoreCase))
{
results.Add(result);
}
}
catch (ArgumentException ae)
{
throw new ApplicationException("Invalid regular expression,
details=" + ae.Message);
}
if (buffer != IntPtr.Zero)
{
try { Marshal.FreeHGlobal(buffer); }
catch { }
buffer = IntPtr.Zero;
structSize = 0;
}
//Loop through all entries, attempt to find matches
while (true)
{
long nextResult = FindNextUrlCacheEntry(hEnum, buffer, out
structSize);
if (nextResult != 1) //TRUE
{
int lastError = Marshal.GetLastWin32Error();
if (lastError == Hresults.ERROR_INSUFFICIENT_BUFFER)
{
buffer = Marshal.AllocHGlobal((int)structSize);
nextResult = FindNextUrlCacheEntry(hEnum, buffer, out
structSize);
}
else if (lastError == Hresults.ERROR_NO_MORE_ITEMS)
{
break;
}
}
result =
(INTERNET_CACHE_ENTRY_INFO)Marshal.PtrToStructure(buffer,
typeof(INTERNET_CACHE_ENTRY_INFO));
if (Regex.IsMatch(result.lpszSourceUrlName, urlPattern,
RegexOptions.IgnoreCase))
{
results.Add(result);
}
if (buffer != IntPtr.Zero)
{
try { Marshal.FreeHGlobal(buffer); }
catch { }
buffer = IntPtr.Zero;
structSize = 0;
}
}
}
finally
{
if (hEnum != IntPtr.Zero)
{
FindCloseUrlCache(hEnum);
}
if (buffer != IntPtr.Zero)
{
try { Marshal.FreeHGlobal(buffer); }
catch { }
}
}
return results;
}
Regards,
Claire
Original comment by claire.a...@gmail.com
on 11 Feb 2009 at 11:06
Claire, lets make a baby!!! :D
This problem was happening on random windows 7 machines. I was just starting to
get desperate trying to figure out why.
You just saved my life.... tks so much!!
Nuno Araújo
Original comment by nemesis....@gmail.com
on 5 Sep 2013 at 6:11
Original issue reported on code.google.com by
claire.a...@gmail.com
on 10 Feb 2009 at 5:43