编程读取其它进程中ListView的内容
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
void __fastcall TMainForm::GetListViewContent()
{
//TODO: Add your source code here
//*
int nColumnCount;
int nItemCount;
int i,j;
char chBuffer[1024];
DWORD dwProcessID;
HANDLE hProcess;
void * Pointer;
SIZE_T nNumberOfBytesRead;
AnsiString s;
TLVItem Item;
void * lpItem;
nColumnCount = Header_GetItemCount(ListView_GetHeader(hWindow));
//if(nColumnCount <= 0) return;
//这一行不能加,不然在第一次读取非REPORT风格的内容时,会返回
nItemCount = ListView_GetItemCount(hWindow);
GetWindowThreadProcessId(hWindow, &dwProcessID);
hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
false,
dwProcessID);
Pointer = VirtualAllocEx(hProcess,
NULL,
1024,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
lpItem = VirtualAllocEx(hProcess,
NULL,
sizeof(TLVItem),
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
try
{
for(i= 0 ; i < nItemCount ; ++i)
{
s = "";
for(j=0; j <=nColumnCount; ++j)
{
ZeroMemory(chBuffer, sizeof(chBuffer)*sizeof(char));
WriteProcessMemory(hProcess,
Pointer,
chBuffer,
sizeof(chBuffer)*sizeof(char),
&nNumberOfBytesRead);
Item.mask = LVIF_TEXT;
Item.iItem = i;
Item.iSubItem = j;
Item.cchTextMax = sizeof(chBuffer);
Item.pszText = (LPSTR)Pointer;
nNumberOfBytesRead = 0;
WriteProcessMemory(hProcess,
lpItem,
(LPVOID)&Item,
sizeof(TLVItem),
(LPDWORD)&nNumberOfBytesRead);
SendMessage(hWindow, LVM_GETITEMTEXT, i, (LPARAM)lpItem);
ReadProcessMemory(hProcess,
(void *)Pointer,
chBuffer,
sizeof(chBuffer),
&nNumberOfBytesRead);
s += chBuffer;
s +="\t";
}
ContentMemo->Lines->Add(s);
}//for(i...
}//try
__finally
{
VirtualFreeEx(hProcess,
Pointer,
0,
MEM_RELEASE);
VirtualFreeEx(hProcess,
lpItem,
0,
MEM_RELEASE);
CloseHandle(hProcess);
}
}