首页  编辑  

获取其他进程的ListView/TreeView内容

Tags: /超级猛料/Friends.网友专栏/zswang/   Date Created:

获取其他进程的ListView/TreeView内容

(*//

标题:获取其他进程中ListView的文本

说明:Window2000+Delphi6调试通过

设计:Zswang

支持:wjhu111@21cn.com

日期:2004-03-25

//*)

uses CommCtrl;

function ListViewColumnCount(mHandle: THandle): Integer;

begin

 result := Header_GetItemCount(ListView_GetHeader(mHandle));

end; { ListViewColumnCount }

function GetListViewText(mHandle: THandle; mStrings: TStrings): Boolean;

var

 vColumnCount: Integer;

 vItemCount: Integer;

 I, J: Integer;

 vBuffer: array[0..255] of Char;

 vProcessId: DWORD;

 vProcess: THandle;

 vPointer: Pointer;

 vNumberOfBytesRead: Cardinal;

 S: string;

 vItem: TLVItem;

begin

 result := False;

 if not Assigned(mStrings) then Exit;

 vColumnCount := ListViewColumnCount(mHandle);

 if vColumnCount <= 0 then Exit;

 vItemCount := ListView_GetItemCount(mHandle);

 GetWindowThreadProcessId(mHandle, @vProcessId);

 vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or

   PROCESS_VM_WRITE, False, vProcessId);

 vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT,

   PAGE_READWRITE);

 mStrings.BeginUpdate;

 try

   mStrings.Clear;

   for I := 0 to vItemCount - 1 do begin

     S := '';

     for J := 0 to vColumnCount - 1 do begin

       with vItem do begin

         mask := LVIF_TEXT;

         iItem := I;

         iSubItem := J;

         cchTextMax := SizeOf(vBuffer);

         pszText := Pointer(Cardinal(vPointer) + SizeOf(TLVItem));

       end;

       WriteProcessMemory(vProcess, vPointer, @vItem,

         SizeOf(TLVItem), vNumberOfBytesRead);

       SendMessage(mHandle, LVM_GETITEM, I, lparam(vPointer));

       ReadProcessMemory(vProcess, Pointer(Cardinal(vPointer) + SizeOf(TLVItem)),

         @vBuffer[0], SizeOf(vBuffer), vNumberOfBytesRead);

       S := S + #9 + vBuffer;

     end;

     Delete(S, 1, 1);

     mStrings.Add(S);

   end;

 finally

   VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);

   CloseHandle(vProcess);

   mStrings.EndUpdate;

 end;

 result := True;

end; { GetListViewText }

//Example

procedure TForm1.FormCreate(Sender: TObject);

begin

 RegisterHotKey(Handle, 1, MOD_WIN, VK_F2);

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

 UnRegisterHotKey(Handle, 1);

end;

procedure TForm1.WMHOTKEY(var Msg: TWMHOTKEY);

begin

 case Msg.HotKey of

   1:

     GetListViewText(

       WindowFromPoint(Point(Mouse.CursorPos.X, Mouse.CursorPos.Y)),

       MemoText.Lines);

 end;

end;

(*//

标题:获取其他进程中TreeView的文本

说明:Window2000+Delphi6调试通过

设计:Zswang

支持:wjhu111@21cn.com

日期:2004-03-26

//*)

function TreeNodeGetNext(mHandle: THandle; mTreeItem: HTreeItem): HTreeItem;

var

 vParentID: HTreeItem;

begin

 result := nil;

 if (mHandle <> 0) and (mTreeItem <> nil) then begin

   result := TreeView_GetChild(mHandle, mTreeItem);

   if result = nil then

     result := TreeView_GetNextSibling(mHandle, mTreeItem);

   vParentID := mTreeItem;

   while (result = nil) and (vParentID <> nil) do begin

     vParentID := TreeView_GetParent(mHandle, vParentID);

     result := TreeView_GetNextSibling(mHandle, vParentID);

   end;

 end;

end;  { TreeNodeGetNext }

function TreeNodeGetLevel(mHandle: THandle; mTreeItem: HTreeItem): Integer;

var

 vParentID: HTreeItem;

begin

 result := -1;

 if (mHandle <> 0) and (mTreeItem <> nil) then begin

   vParentID := mTreeItem;

   repeat

     Inc(result);

     vParentID := TreeView_GetParent(mHandle, vParentID);

   until vParentID = nil;

 end;

end; { TreeNodeGetLevel }

function GetTreeViewText(mHandle: THandle; mStrings: TStrings): Boolean;

var

 vItemCount: Integer;

 vBuffer: array[0..255] of Char;

 vProcessId: DWORD;

 vProcess: THandle;

 vPointer: Pointer;

 vNumberOfBytesRead: Cardinal;

 I: Integer;

 vItem: TTVItem;

 vTreeItem: HTreeItem;

begin

 result := False;

 if not Assigned(mStrings) then Exit;

 GetWindowThreadProcessId(mHandle, @vProcessId);

 vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or

   PROCESS_VM_WRITE, False, vProcessId);

 vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT,

   PAGE_READWRITE);

 mStrings.BeginUpdate;

 try

   mStrings.Clear;

   vItemCount := TreeView_GetCount(mHandle);

   vTreeItem := TreeView_GetRoot(mHandle);

   for I := 0 to vItemCount - 1 do begin

     with vItem do begin

       mask := TVIF_TEXT;

       cchTextMax := SizeOf(vBuffer);

       pszText := Pointer(Cardinal(vPointer) + SizeOf(vItem));

       hItem := vTreeItem;

     end;

     WriteProcessMemory(vProcess, vPointer, @vItem,

       SizeOf(vItem), vNumberOfBytesRead);

     SendMessage(mHandle, TVM_GETITEM, 0, lparam(vPointer));

     ReadProcessMemory(vProcess, Pointer(Cardinal(vPointer) + SizeOf(TLVItem)),

       @vBuffer[0], SizeOf(vBuffer), vNumberOfBytesRead);

     mStrings.Add(StringOfChar(#9, TreeNodeGetLevel(mHandle, vTreeItem)) + vBuffer);

     vTreeItem := TreeNodeGetNext(mHandle, vTreeItem);

   end;

 finally

   VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);

   CloseHandle(vProcess);

   mStrings.EndUpdate;

 end;

 result := True;

end; { GetTreeViewText }