编程读取其它进程中TreeView的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
void __fastcall TMainForm::GetTreeViewContent()
{
//TODO: Add your source code here
int nItemCount;
int i;
char chBuffer[256];
DWORD dwProcessID;
HANDLE hProcess;
void * Pointer;
SIZE_T nNumberOfBytesRead;
TTVItem Item;
HTREEITEM hTreeItem;
GetWindowThreadProcessId(hWindow, &dwProcessID);
hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
false,
dwProcessID);
Pointer = VirtualAllocEx(hProcess,
NULL,
4096,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
try{
nItemCount = TreeView_GetCount(hWindow);
hTreeItem = TreeView_GetRoot(hWindow);
for(i= 0 ; i < nItemCount ; ++i)
{
TreeView_Expand(hWindow, hTreeItem, TVM_EXPAND);
Item.mask = TVIF_TEXT;
Item.cchTextMax = sizeof (chBuffer);
Item.pszText = (LPSTR)((UINT *)Pointer + sizeof(TTVItem));
Item.hItem = hTreeItem;
WriteProcessMemory(hProcess,
Pointer,
&Item,
sizeof(TTVItem),
&nNumberOfBytesRead);
SendMessage(hWindow, TVM_GETITEM, 0, (LPARAM)Pointer);
ReadProcessMemory(hProcess,
(void *)((UINT *)Pointer + sizeof(TTVItem)),
chBuffer,
sizeof(chBuffer),
&nNumberOfBytesRead);
ContentMemo->Lines->Add("┣"+GetLineString(TreeNodeGetLevel(hTreeItem))
+" "+AnsiString(chBuffer));
hTreeItem = TreeNodeGetNext(hTreeItem);
}//for(i...
}//try
__finally
{
VirtualFreeEx(hProcess,
Pointer,
0,
MEM_RELEASE);
CloseHandle(hProcess);
}
}
//---------------------------------------------------------------------------
HTREEITEM __fastcall TMainForm::TreeNodeGetNext(HTREEITEM hTreeItem)
{
//TODO: Add your source code here
HTREEITEM hParentID;
HTREEITEM hResult;
hResult = NULL;
if(hWindow != NULL && hTreeItem != NULL)
{
hResult = TreeView_GetChild(hWindow, hTreeItem);
if(hResult == NULL)
{
hResult = TreeView_GetNextSibling(hWindow, hTreeItem);
}
hParentID = hTreeItem;
while(hResult == NULL && hParentID != NULL)
{
hParentID = TreeView_GetParent(hWindow, hParentID);
hResult = TreeView_GetNextSibling(hWindow, hParentID);
}
}
return hResult;
}
//---------------------------------------------------------------------------
int __fastcall TMainForm::TreeNodeGetLevel(HTREEITEM hTreeItem)
{
//TODO: Add your source code here
HTREEITEM hParentID;
int iRet;
iRet = -1;
if(hWindow != NULL && hTreeItem != NULL)
{
hParentID = hTreeItem;
do
{
iRet ++;
hParentID = TreeView_GetParent(hWindow, hParentID);
}while(hParentID != NULL);
}
return iRet;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMainForm::GetLineString(int iLength)
{
//TODO: Add your source code here
AnsiString sRet;
for(int i = 0; i < iLength; ++i)
{
sRet += "━";
}
return sRet;
}