我們知道在使用IE瀏覽網頁時,IE會把遠端主機的內容保存在本機以供以后脫機訪問。下面將介紹的是如何通過Delphi編程實現遍歷Cache中所有保存的內容。 如果大家對Windows API編程比較熟悉的話,應該知道對于遍歷訪問一般有兩種辦法,一是定義一個回調函數,然后將回調函數地址傳遞給遍歷函數,當遍歷到一個內容時就會調用回調函數一次,例如EnumWindows函數。另外一種是先利用一個API函數建立一個局柄,然后以這個局柄作為遍歷函數的參數,我們可以通過反復調用遍歷函數知道返回False,例如FindFirstFile以及FindNextFile函數。對IE Cache的遍歷使用的是第二種方法,即首先調用FindFirstUrlCacheEntryEx,如果成功返回一個局柄,然后通過重復調用FindNextUrlCacheEntryEx知道函數返回False,這樣就可以實現對Cache中所有文件的遍歷。 下面來看程序,建立一個新工程,然后在Form1中分別加入兩個TButton組件以及兩個TListBox組件,Form1的完整代碼如下:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Wininet, StdCtrls;
type TForm1 = class(TForm) Button1: TButton; ListBox1: TListBox; ListBox2: TListBox; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private function FindNextEntrys(Handle:Integer):Boolean; { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.DFM} function TForm1.FindNextEntrys(Handle:Integer):Boolean; var T: PInternetCacheEntryInfo; D: DWORD; begin D := 0; FindnextUrlCacheEntryEx(Handle, nil, @D, nil, nil, nil); GetMem(T, D); try if FindNextUrlCacheEntryEx(Handle, T, @D, nil, nil, nil) then begin ListBox1.Items.Add(T.lpszSourceUrlName); ListBox2.Items.Add(T.lpszLocalFileName); Result := True; end else Result := False; finally FreeMem(T, D) end; end;
procedure TForm1.Button1Click(Sender: TObject); var H:Integer; T: PInternetCacheEntryInfo; D: DWORD; begin D := 0; FindFirstUrlCacheEntryEx(nil, 0, NORMAL_CACHE_ENTRY, 0,nil,@D, nil, nil, nil); GetMem(T, D); try H := FindFirstUrlCacheEntryEx(nil,0, NORMAL_CACHE_ENTRY, 0, T, @D, nil, nil, nil); if (H = 0) then else begin repeat until not FindNextEntrys(H); FindCloseUrlCache(H); end finally FreeMem(T, D) end; end;
procedure TForm1.Button2Click(Sender: TObject); var URL:String; begin If ListBox1.ItemIndex >=0 then begin URL:=ListBox1.Items.Strings[ListBox1.ItemIndex]; Self.Caption := URL; if DeleteUrlCacheEntry(PChar(URL))then ListBox1.Items.Delete(ListBox1.ItemIndex); end; end;
end.
運行程序,點擊Button1,就可以分別在ListBox1中列出所有在Cache中的文件所對應的URL以及在ListBox2中列出相應的文件名。在ListBox1中選擇一個列表,然后點擊 Button2 就可以將該項從Cache中刪除。 下面看程序,FindFirstUrlCacheEntryEx函數在Delphi中定義如下:
function FindFirstUrlCacheEntryExA(lpszUrlSearchPattern: PAnsiChar; dwFlags: DWORD; dwFilter: DWORD; GroupId: GROUPID; lpFirstCacheEntryInfo: PInternetCacheEntryInfo; lpdwFirstCacheEntryInfoBufferSize: LPDWORD; lpGroupAttributes: Pointer; { 必須為 nil } pcbGroupAttributes: LPDWORD; {必須為 nil } lpReserved: Pointer { 必須為 nil } ): THandle; stdcall;
其中,dwFilter定義查找類型,在這里定義為NORMAL_CACHE_ENTRY以查找普通的Cache文件,GroupId定義查找分組,在這里定義為0以查找所有分組。lpFirstCacheEntryInfo定義Cache文件數據結構。該結構在Wininet.pas中有定義,這里就不列出了,其中成員lpszSourceUrlName以及lpszLocalFileName分別定義文件URL以及本地文件名。 在上面的程序中我們可以看到,不論調用FindFirstUrlCacheEntryEx還是FindNextUrlCacheEntryEx,都需要調用兩次,第一次獲得一個指向PInternetCacheEntryInfo結構的指針,將這個指針通過GetMem函數賦予一個PInternetCacheEntryInfo結構數據。然后第二次調用才可以獲得結果。遍歷訪問完畢后需要調用FindCloseUrlCache方法關閉打開的局柄。 上面介紹的是Cache操作中的遍歷Cache文件以及刪除Cache文件的操作。Cache操作函數還包括:分組函數,可以將特定的文件分在一個組內并執行組操作,例如:CreateUrlCacheGroup、SetUrlCacheEntryGroup;數據流(Stream)操作函數,可以將Cache中的內容輸入到數據流中。等等。大家可以參考MSDN中的幫助,或者到我的主頁 http://www.applevb.com 同我討論以及獲得源程序。 以上程序在Win2000、Delphi 5下編寫,Win2000、Win98下運行通過。
|