(五)C# QQ讨论组广告群发工具——操作其他进程ListView(附源码)

由于上篇C#操作QQ的TreeView控件以及详细讲解过如何操作其他进程的控件的流程,所以关于如何操作我就不在啰嗦了。传送门:Rolends 主要实现流程如下:
1), 获取列数
获取列数需先获取列的索引指针:

 C# Code By wuleba.com

1 columnIndex = WinAPIHelper.SendMessage (lvHwnd, (int) WinAPIHelper.LVM.GETHEADER, 0, 0);

利用列索引指针去获取列数量:

 C# Code By wuleba.com

1 columnCount = WinAPIHelper.SendMessage (new IntPtr (columnIndex), (int) WinAPIHelper.HDM.HDM_GETITEMCOUNT, 0, 0);

2), 获取行数

 C# Code By wuleba.com

1 rowCount = WinAPIHelper.SendMessage (lvHwnd, (int) WinAPIHelper.LVM.GETITEMCOUNT, 0, 0);

3), 遍历所有cell,使用LVITEM获取需要的数据

 C# Code By wuleba.com

1 iRet = WinAPIHelper.SendMessage (lvHwnd, (int) WinAPIHelper.LVM.GETITEMW, j, pItemMemory);

像这些没有技术难点的东西我会讲的少一点,大家可以查MSDN,需要我的APIHelper类,请留言

 C# Code By wuleba.com

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
public class WinListViewRow
{
public string[] Cells { get; set; }
}
public class WinListView
{
public WinListView()
{
Rows =
new List<WinListViewRow>();
ColumnHeaders =
new List<string>();
}
public int RowCount { get; set; }
public int ColumnCount { get; set; }
public List<string> ColumnHeaders { get; set; }
public List<WinListViewRow> Rows { get; set; }
}
public class ListViewHelper
{
const int MAX_LVMSTRING = 512;
public static int GetRowCount (IntPtr lvHwnd)
{
int iRet = WinAPIHelper.SendMessage (lvHwnd, (int) WinAPIHelper.LVM.GETITEMCOUNT, 0, 0);
return iRet;
}
public static string GetItemText (IntPtr lvHwnd, int pRow, int pColumn)
{
byte[] strBuffer = new byte[MAX_LVMSTRING + 1];
IntPtr hPro = WinAPIHelper.OpenProcess (WinAPIHelper.PROCESS_ALL_ACCESS,
false, WndHelper.GetProcessId (lvHwnd) );
IntPtr pStrBufferMemory = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, MAX_LVMSTRING, WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ReadWrite);
WinAPIHelper.LVITEM item =
new WinAPIHelper.LVITEM();
item.iSubItem = pColumn;
item.cchTextMax = MAX_LVMSTRING;
item.pszText = pStrBufferMemory;
item.mask = WinAPIHelper.LVIF_TEXT;
IntPtr pItemMemory = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, Marshal.SizeOf (
typeof (WinAPIHelper.LV_ITEM) ), WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ReadWrite);
bool result = WinAPIHelper.WriteProcessMemory (hPro, pItemMemory, ref item, Marshal.SizeOf (item), IntPtr.Zero);
IntPtr pStrLocaAddress = Marshal.AllocHGlobal (MAX_LVMSTRING);
int iRet = WinAPIHelper.SendMessage (lvHwnd, (int) WinAPIHelper.LVM.GETITEMTEXTW, pRow, pItemMemory);
int readLen = 0;
result = WinAPIHelper.ReadProcessMemory (hPro, pStrBufferMemory, pStrLocaAddress, MAX_LVMSTRING,
out readLen);
result = WinAPIHelper.ReadProcessMemory (hPro, pItemMemory,
ref item, Marshal.SizeOf (item), IntPtr.Zero);
string tmpString2 = Marshal.PtrToStringAuto (pStrLocaAddress);
if (pStrLocaAddress != IntPtr.Zero)
{
try
{
Marshal.FreeHGlobal (pStrLocaAddress);
}
catch { }
}
if (pItemMemory != IntPtr.Zero)
{
try
{
WinAPIHelper.VirtualFreeEx (hPro, pItemMemory, 0, WinAPIHelper.MEM_RELEASE);
}
catch { }
}
if (pStrBufferMemory != IntPtr.Zero)
{
try
{
WinAPIHelper.VirtualFreeEx (hPro, pStrBufferMemory, 0, WinAPIHelper.MEM_RELEASE);
}
catch { }
}
if (hPro != IntPtr.Zero)
{
try
{
WinAPIHelper.CloseHandle (hPro);
}
catch { }
}
return tmpString2;
}
public static WinListView GetListView (IntPtr lvHwnd)
{
WinListView lv =
new WinListView();
byte[] strBuffer = new byte[MAX_LVMSTRING + 1];
IntPtr hPro = WinAPIHelper.OpenProcess (WinAPIHelper.PROCESS_ALL_ACCESS,
false, WndHelper.GetProcessId (lvHwnd) );
IntPtr pStrBufferMemory = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, MAX_LVMSTRING, WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ReadWrite);
IntPtr pItemMemory = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, Marshal.SizeOf (
typeof (WinAPIHelper.LV_ITEM) ), WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ReadWrite);
IntPtr pStrLocaAddress = Marshal.AllocHGlobal (MAX_LVMSTRING);
WinAPIHelper.LVITEM item =
new WinAPIHelper.LVITEM();
item.cchTextMax = MAX_LVMSTRING;
item.pszText = pStrBufferMemory;
item.mask = WinAPIHelper.LVIF_TEXT;
int columnIndex = 0;
int tryCount = 0;
GETCOLUMNINDEX:
if (columnIndex == 0)
{
tryCount++;
if (tryCount > SysConfig.ListViewItemFindTryCount)
{
return lv;
}
columnIndex = WinAPIHelper.SendMessage (lvHwnd, (
int) WinAPIHelper.LVM.GETHEADER, 0, 0); //列的index指针
Console.WriteLine (“列指针为” + columnIndex);
Thread.Sleep (
1000);
goto GETCOLUMNINDEX;
}
int columnCount = 0;
tryCount =
0;
//获取总列数
GETCOLUMNCOUNT:
if (columnCount == 0)
{
tryCount++;
if (tryCount > SysConfig.ListViewItemFindTryCount)
{
return lv;
}
columnCount = WinAPIHelper.SendMessage (
new IntPtr (columnIndex), (int) WinAPIHelper.HDM.HDM_GETITEMCOUNT, 0, 0);
Console.WriteLine (
“列数量为” + columnCount);
Thread.Sleep (
1000);
goto GETCOLUMNCOUNT;
}
int rowCount = 0;
tryCount =
0;
//获取总行数
GETROWCOUNT:
if (rowCount == 0)
{
tryCount++;
if (tryCount > SysConfig.ListViewItemFindTryCount)
{
return lv;
}
rowCount = WinAPIHelper.SendMessage (lvHwnd, (
int) WinAPIHelper.LVM.GETITEMCOUNT, 0, 0);
Console.WriteLine (
“行数量为” + rowCount);
Thread.Sleep (
1000);
goto GETROWCOUNT;
}
lv.ColumnCount = columnCount;
lv.RowCount = rowCount;
for (int j = 0; j < rowCount; j++)
{
var row =
new WinListViewRow();
row.Cells =
new string[columnCount];
for (int i = 0; i < columnCount; i++)
{
item.iSubItem = i;
bool result = WinAPIHelper.WriteProcessMemory (hPro, pItemMemory, ref item, Marshal.SizeOf (item), IntPtr.Zero);
int iRet = WinAPIHelper.SendMessage (lvHwnd, (int) WinAPIHelper.LVM.GETITEMTEXTW, j, pItemMemory);
iRet = WinAPIHelper.SendMessage (lvHwnd, (
int) WinAPIHelper.LVM.GETITEMW, j, pItemMemory);
int readLen = 0;
result = WinAPIHelper.ReadProcessMemory (hPro, pStrBufferMemory, pStrLocaAddress, MAX_LVMSTRING,
out readLen);
result = WinAPIHelper.ReadProcessMemory (hPro, pItemMemory,
ref item, Marshal.SizeOf (item), IntPtr.Zero);
string tmpString2 = Marshal.PtrToStringAuto (pStrLocaAddress);
row.Cells[i] = tmpString2;
}
if (row.Cells.Length > 0)
{
lv.Rows.Add (row);
}
}
if (pStrLocaAddress != IntPtr.Zero)
{
try
{
Marshal.FreeHGlobal (pStrLocaAddress);
}
catch { }
}
if (pItemMemory != IntPtr.Zero)
{
try
{
WinAPIHelper.VirtualFreeEx (hPro, pItemMemory, 0, WinAPIHelper.MEM_RELEASE);
}
catch { }
}
if (pStrBufferMemory != IntPtr.Zero)
{
try
{
WinAPIHelper.VirtualFreeEx (hPro, pStrBufferMemory, 0, WinAPIHelper.MEM_RELEASE);
}
catch { }
}
if (hPro != IntPtr.Zero)
{
try
{
WinAPIHelper.CloseHandle (hPro);
}
catch { }
}
return lv;
}
}

这篇代码实现比较滥,大家可以自己拿去改。
相关文章:(四)C# QQ讨论组广告群发工具——操作QQ的TreeView控件(附源码)

版权声明:

小编:吾乐吧软件站

链接:https://www.wuleba.com/4258.html

来源:吾乐吧软件站

本资源仅供个人学习,禁止用于商业用途。下载后请于24小时内删除,并支持正版或原作者。因使用本资源导致的任何法律纠纷或损失,由使用者自行承担。

THE END
上一篇 Windows Server 2012候选版本将于6月发布
下一篇 迅雷会员尊享版 7.3.0.30 体验计划(零插件、零广告)
相关内容
转发
1
点赞
分享
全部转发 0 只看作者
按时间倒序 按时间正序

全部评论 1 只看作者
按时间倒序 按时间正序
Crystal
支持开源
2012年4月25日 22:08 0 回复
赞赏支持
微信收款码
微信扫一扫
支付宝收款码
支付宝扫一扫
投诉内容
0/200
上传图片(最多3张,单张不超过2MB)
生成分享图片
正在生成预览...