Sorry, I was away for a long time due to some other issues, however m still working on this and not succeeded so far. I have found a code example in
VB 6 somewhere from the net. It fetches all entries from the cache and displays them in a list box in a Form. It works just fine and the code is not much difficult to understand for a Win API geek. I have been trying to convert this code to c# using PInvoke, not succeeded so far. I am not so strong on the issues like data type compliance and pointer manipulation, so find it hard to diagnose where the problem exists. I have pasted the
VB code and its (so far) 'translated' c# code. The code in C# is not complete. Also added comments where the problem seems to be. In short, either i am not getting the actual address of the struct or the handle of the cache entry, or rtlmovememory() doesn't work. Remember, there's no compile/run-time error but i can't get the required result either. Hope a C# geek who has worked with PInvoke could help getting rid of this.
Thankyou
Here's the
VB 6 example.
'This project requries a form with a listbox (List1) on it
'and a class module (MemoryBlock)
'In the form:
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
dwExemptDelta As Long
'szRestOfData() As Byte
End Type
Private Declare Function FindFirstUrlCacheEntry Lib "wininet.dll" Alias "FindFirstUrlCacheEntryA" (ByVal lpszUrlSearchPattern As String, ByVal lpFirstCacheEntryInfo As Long, ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long
Private Declare Function FindNextUrlCacheEntry Lib "wininet.dll" Alias "FindNextUrlCacheEntryA" (ByVal hEnumHandle As Long, ByVal lpNextCacheEntryInfo As Long, ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long
Private Declare Sub FindCloseUrlCache Lib "wininet.dll" (ByVal hEnumHandle As Long)
Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
Private Sub Form_Load()
'KPD-Team 2001
'URL:
http://www.allapi.net/
'E-Mail:
[email protected]
Dim ICEI As INTERNET_CACHE_ENTRY_INFO, Ret As Long
Dim hEntry As Long, Msg As VbMsgBoxResult
Dim MemBlock As New MemoryBlock
'Start enumerating the visited URLs
FindFirstUrlCacheEntry vbNullString, ByVal 0&, Ret
'If Ret is larger than 0...
If Ret > 0 Then
'... allocate a buffer
MemBlock.Allocate Ret
'call FindFirstUrlCacheEntry
hEntry = FindFirstUrlCacheEntry(vbNullString, MemBlock.Handle, Ret)
'copy from the buffer to the INTERNET_CACHE_ENTRY_INFO structure
MemBlock.ReadFrom VarPtr(ICEI), LenB(ICEI)
'Add the lpszSourceUrlName string to the listbox
If ICEI.lpszSourceUrlName <> 0 Then List1.AddItem MemBlock.ExtractString(ICEI.lpszSourceUrlName, Ret)
End If
'Loop until there are no more items
Do While hEntry <> 0
'Initialize Ret
Ret = 0
'Find out the required size for the next item
FindNextUrlCacheEntry hEntry, ByVal 0&, Ret
'If we need to allocate a buffer...
If Ret > 0 Then
'... do it
MemBlock.Allocate Ret
'and retrieve the next item
FindNextUrlCacheEntry hEntry, MemBlock.Handle, Ret
'copy from the buffer to the INTERNET_CACHE_ENTRY_INFO structure
MemBlock.ReadFrom VarPtr(ICEI), LenB(ICEI)
'Add the lpszSourceUrlName string to the listbox
If ICEI.lpszSourceUrlName <> 0 Then List1.AddItem MemBlock.ExtractString(ICEI.lpszSourceUrlName, Ret)
'Else = no more items
Else
Exit Do
End If
Loop
'Close enumeration handle
FindCloseUrlCache hEntry
'Delete our memory block
Set MemBlock = Nothing
Msg = MsgBox("Do you wish to delete the Internet Explorer cache?", vbYesNo + vbDefaultButton2 + vbQuestion)
If Msg = vbYes Then
'loop trough the entries...
For Ret = 0 To List1.ListCount - 1
'...and delete them
DeleteUrlCacheEntry List1.List(Ret)
Next Ret
MsgBox "Cache deleted..."
End If
End Sub
'In the class module 'MemoryBlock':
Option Explicit
Private Const MEM_DECOMMIT = &H4000
Private Const MEM_RELEASE = &H8000
Private Const MEM_COMMIT = &H1000
Private Const MEM_RESERVE = &H2000
Private Const MEM_RESET = &H80000
Private Const MEM_TOP_DOWN = &H100000
Private Const PAGE_READONLY = &H2
Private Const PAGE_READWRITE = &H4
Private Const PAGE_EXECUTE = &H10
Private Const PAGE_EXECUTE_READ = &H20
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Const PAGE_GUARD = &H100
Private Const PAGE_NOACCESS = &H1
Private Const PAGE_NOCACHE = &H200
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSrc As Long, ByVal ByteLen As Long)
Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFree Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function VirtualLock Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long) As Long
Private Declare Function VirtualUnlock Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long) As Long
Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
Private Declare Function IsBadWritePtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
Private Declare Function IsBadStringPtr Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpStringDest As String, ByVal lpStringSrc As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private m_VirtualMem As Long, lLength As Long
'Returns the handle of the allocated memory
Public Property Get Handle() As Long
Handle = m_VirtualMem
End Property
'Allocates a specific amount of bytes in the Virtual Memory
Public Sub Allocate(lCount As Long)
ReleaseMemory
m_VirtualMem = VirtualAlloc(ByVal 0&, lCount, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
VirtualLock m_VirtualMem, lCount
End Sub
'Reads from the allocated memory and writes it to a specified pointer
Public Sub ReadFrom(hWritePointer As Long, lLength As Long)
If IsBadWritePtr(hWritePointer, lLength) = 0 And IsBadReadPtr(Handle, lLength) = 0 Then
CopyMemory hWritePointer, Handle, lLength
End If
End Sub
'Writes to the allocated memory and reads it from a specified pointer
Public Sub WriteTo(hReadPointer As Long, lLength As Long)
If IsBadReadPtr(hReadPointer, lLength) = 0 And IsBadWritePtr(Handle, lLength) = 0 Then
CopyMemory Handle, hReadPointer, lLength
End If
End Sub
'Extracts a string from the allocated memory
Public Function ExtractString(hStartPointer As Long, lMax As Long) As String
Dim Length As Long
If IsBadStringPtr(hStartPointer, lMax) = 0 Then
ExtractString = Space(lMax)
lstrcpy ExtractString, hStartPointer
Length = lstrlen(hStartPointer)
If Length >= 0 Then ExtractString = Left$(ExtractString, Length)
End If
End Function
'Release the allocated memory
Public Sub ReleaseMemory()
If m_VirtualMem <> 0 Then
VirtualUnlock m_VirtualMem, lLength
VirtualFree m_VirtualMem, lLength, MEM_DECOMMIT
VirtualFree m_VirtualMem, 0, MEM_RELEASE
m_VirtualMem = 0
End If
End Sub
Private Sub Class_Terminate()
ReleaseMemory
End Sub
and the C# version (not complete, eliminated MemoryBolck class to make it simple.)
public class Test
{
const int MEM_DECOMMIT = 0x4000;
const int MEM_COMMIT= 0x1000;
const int MEM_RELEASE = 0x8000;
const int MEM_RESERVE = 0x2000;
const int MEM_RESET = 0x80000;
const int MEM_TOP_DOWN = 0x100000;
const int PAGE_READONLY = 0x2;
const int PAGE_READWRITE = 0x4;
const int PAGE_EXECUTE = 0x10;
const int PAGE_EXECUTE_READ = 0x20;
const int PAGE_EXECUTE_READWRITE = 0x40;
const int PAGE_GUARD = 0x100;
const int PAGE_NOACCESS = 0x1;
const int PAGE_NOCACHE = 0x200;
public struct FILETIME
{
public int dwLowDateTime;
public int dwHighDateTime;
}
public struct INTERNET_CACHE_ENTRY_INFO
{
public int dwStructSize;
public int lpszSourceUrlName;
public int lpszLocalFileName;
public int CacheEntryType;
public int dwUseCount;
public int dwHitRate;
public int dwSizeLow;
public int dwSizeHigh;
public FILETIME LastModifiedTime;
public FILETIME ExpireTime;
public FILETIME LastAccessTime;
public FILETIME LastSyncTime;
public int lpHeaderInfo;
public int dwHeaderInfoSize;
public int lpszFileExtension;
public int dwReserved;
public int dwExemptDelta;
}
[DllImport("wininet")]
public static extern int FindFirstUrlCacheEntryA (string lpszUrlSearchPattern, int lpFirstCacheEntryInfo, ref int lpdwFirstCacheEntryInfoBufferSize);
[DllImport("wininet")]
public static extern int FindNextUrlCacheEntryA (int hEnumHandle, int lpNextCacheEntryInfo , ref int lpdwNextCacheEntryInfoBufferSize);
[DllImport("wininet")]
public static extern void FindCloseUrlCache (int hEnumHandle);
[DllImport("kernel32")]
public static extern void RtlMoveMemory (int pDest, int pSrc, int ByteLen);
[DllImport("kernel32")]
public static extern int VirtualAlloc (int lpAddress,int dwSize,int flAllocationType,int flProtect);
[DllImport("kernel32")]
public static extern int VirtualFree(int lpAddress,int dwSize,int dwFreeType);
[DllImport("kernel32")]
public static extern int VirtualLock(int lpAddress,int dwSize);
[DllImport("kernel32")]
public static extern int VirtualUnlock (int lpAddress,int dwSize);
[DllImport("kernel32")]
public static extern int IsBadReadPtr(int lp, int ucb);
[DllImport("kernel32")]
public static extern int IsBadWritePtr(int lp , int ucb);
[DllImport("kernel32")]
public static extern int IsBadStringPtrA(int lpsz,int ucchMax);
[DllImport("kernel32")]
public static extern int lstrcpyA(ref string lpStringDest ,int lpStringSrc);
[DllImport("kernel32")]
public static extern int lstrlenA(int lpString);
public static int m_VirtualMem, lLength;
public static int Ret=0;
static void Main()
{
GetFromBrowserCache();
return;
}
public static void GetFromBrowserCache()
{
INTERNET_CACHE_ENTRY_INFO ICEI = new INTERNET_CACHE_ENTRY_INFO();
int hEntry;
//Get the total bytes from the first cache entry
hEntry = FindFirstUrlCacheEntryA("", 0x0, ref Ret);
if (Ret > 0)
{
Allocate(Ret);
hEntry = FindFirstUrlCacheEntryA("", m_VirtualMem, ref Ret);
//Get the address of ICEI. In fact we need intptr, i don't know if it is the correct way of getting it.
System.Runtime.InteropServices.GCHandle GC = System.Runtime.InteropServices.GCHandle.Alloc(ICEI , System.Runtime.InteropServices.GCHandleType.Pinned );
int addr = (int)GC.AddrOfPinnedObject();
Console.WriteLine(" addr="+addr);
GC.Free();
//read cache entry into the struct. Struct size is 84.
ReadFrom(ref addr, 84);
}
}
public static void Allocate(int lCount)
{
ReleaseMemory();
m_VirtualMem = VirtualAlloc(0x0, lCount, MEM_COMMIT, PAGE_EXECUTE_READWRITE) ;
VirtualLock(m_VirtualMem, lCount);
}
public static void ReleaseMemory()
{
if(m_VirtualMem != 0)
{
VirtualUnlock(m_VirtualMem, lLength);
VirtualFree (m_VirtualMem, lLength, MEM_DECOMMIT);
VirtualFree (m_VirtualMem, 0, MEM_RELEASE);
m_VirtualMem = 0;
}
}
public static void ReadFrom(ref int hWritePointer , int lLength)
{
if(IsBadWritePtr(hWritePointer, lLength) == 0 && IsBadReadPtr(m_VirtualMem, lLength) == 0 )
{
//may be the following line is not functioning correctly because of invalid argument passed (not sure)
RtlMoveMemory(hWritePointer, m_VirtualMem, lLength);
//added following two lines only for debugging purpose.
string str=ExtractString(hWritePointer, Ret);
Console.WriteLine(str);
}
}
public static string ExtractString(int hStartPointer, int lMax)
{
int Length;
string strReturn="";
if (IsBadStringPtrA(hStartPointer, lMax) == 0)
{
for(int i=0; i<lMax;i++)
strReturn+=" ";
lstrcpyA(ref strReturn, hStartPointer);
Length = lstrlenA(hStartPointer);
if (Length >= 0)
return strReturn.Substring(0, Length);
//here i always find strReturn="P" and Length=1, whereas strReturn should have the url
}
return strReturn;
}
}
BaburMan