为什么TVM_GETITEM取不到数据?

kusosa 2023-08-24 20:07:50

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Winapi.CommCtrl;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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: NativeUInt;
  I: Integer;
  vItem: TTVItemEx;
  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));//不论是哪个,memo都取不到

      //SendMessagea(mHandle, TVM_GETITEMa, 0, lparam(vPointer));//不论是哪个,memo都取不到
      //SendMessagew(mHandle, TVM_GETITEMw 0, lparam(vPointer));//不论是哪个,memo都取不到
      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 }


procedure TForm1.Button1Click(Sender: TObject);
begin
   GetTreeViewText(200246, Memo1.Lines);//数字为注册表systreeview32句柄
end;

end.

 

这问题出在哪里,烦请大神予以帮助,非常感谢。

环境为win10+delphi10 seattle

...全文
92 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

5,532

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧