您现在的位置:首页 >> API >> API >> 内容

如何利用Win32API取得另一支程式中的ListView內的所有值

时间:2011/9/3 14:53:28 点击:

  核心提示:uses CommCtrl;function ListViewColumnCount(mHandle: THandle): Integer;begin Result := Header_GetItem...
uses CommCtrl;

function ListViewColumnCount(mHandle: THandle): Integer;
begin
  Result := Header_GetItemCount(ListView_GetHeader(mHandle));
end; { ListViewColumnCount }

function GetListViewSelectText(mHandle: THandle;var mStrings: string): 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;
  I := ListView_GetNextItem(mHandle, -1, LVNI_SELECTED);
  if I = -1 then
    Exit;
//  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;
    mStrings := '';
    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);
      mStrings := mStrings + #9 + vBuffer;
    end;
  finally
    VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
    CloseHandle(vProcess);
//    mStrings.EndUpdate;
  end;
  Result := True;
end; { GetListViewText }

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;

作者:Zswang 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2022 版权所有 All Rights Reserved.
  • 沪ICP备05001939号