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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
|
public class WinTreeViewItem
{
public int Index { get; set; }
public int lpAddress { get; set; }
public string Text { get; set; }
public bool OwnChild { get; set; }
public IntPtr Handle { get; set; }
}
public class TreeViewHelper
{
const int buff_size = 1024;
const int MAX_TVMSTRING = 255;
static public List<WinTreeViewItem> GetAllItem (IntPtr hTv)
{
return GetItems (hTv, null, null, int.MaxValue);
}
static public WinTreeViewItem GetItemByText (IntPtr hTv, params string[] text)
{
List<string> containsList = new List<string>();
foreach (var item in text)
{
if (item != null && item != "")
containsList.Add (item);
}
List<WinTreeViewItem> items = GetItems (hTv, containsList, null, 1, false);
if (items.Count > 0)
return items[0];
else
return null;
}
static public bool SelectItem (IntPtr hTv, WinTreeViewItem item)
{
List<WinTreeViewItem> items = new List<WinTreeViewItem>();
items.Add (item);
return SelectItems (hTv, items);
}
static public List<WinTreeViewItem> GetItems (IntPtr hTv, List<string> containsList, List<string> withOutList, int takeCount)
{
return GetItems (hTv, containsList, withOutList, takeCount, false);
}
static public List<WinTreeViewItem> GetItems (IntPtr hTv, List<string> containsList, List<string> withOutList, int takeCount, bool isAddParent, int containsPreferenceCount = 0, int withOutPreferenceCount = 0)
{
if (withOutPreferenceCount < 1 && containsPreferenceCount < 1)
{
throw new Exception ("两个数量级不能同时为空");
}
int tryCount = 0;
IntPtr hPro = WinAPIHelper.OpenProcess (WinAPIHelper.PROCESS_ALL_ACCESS, false, WndHelper.GetProcessId (hTv) );
List<WinTreeViewItem> withOutResultItems = new List<WinTreeViewItem>();
List<WinTreeViewItem> containsResultItems = new List<WinTreeViewItem>();
int index = 0;
int retval = 0;
GETROOT:
if (retval == 0)
{
tryCount++;
if (tryCount > SysConfig.TreeViewItemFindTryCount)
{
return null;
}
retval = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_GETNEXTITEM, (int) WinAPIHelper.TVM_GETNEXTITEM.TVGN_ROOT, IntPtr.Zero);
Console.WriteLine ("根节点" + retval);
Thread.Sleep (500);
goto GETROOT;
}
tryCount = 0;
int itemCount = 0;
FINDCOUNT:
if (itemCount == 0)
{
tryCount++;
if (tryCount > SysConfig.TreeViewItemFindTryCount)
{
return null;
}
itemCount = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_GETCOUNT, 0, IntPtr.Zero);
Console.WriteLine ("可用创建数量" + itemCount);
Thread.Sleep (500);
goto FINDCOUNT;
}
IntPtr pStrBufferMemory = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, MAX_TVMSTRING, WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ReadWrite);
IntPtr remoteBuffer = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, Marshal.SizeOf (typeof (WinAPIHelper.TVITEM) ), WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ExecuteReadWrite);
if (remoteBuffer == IntPtr.Zero)
{
Console.WriteLine ("远程地址分配失败");
return null;
}
if (pStrBufferMemory == IntPtr.Zero)
{
Console.WriteLine ("远程地址分配失败");
return null;
}
WinAPIHelper.TVITEM tvItem = new WinAPIHelper.TVITEM();
// int size = Marshal.SizeOf(tvItem);
tvItem.mask = WinAPIHelper.TVIF_TEXT;
tvItem.pszText = pStrBufferMemory;
tvItem.cchTextMax = MAX_TVMSTRING;
IntPtr localBuffer = Marshal.AllocHGlobal (Marshal.SizeOf (tvItem) );
Marshal.StructureToPtr (tvItem, localBuffer, false);
int addCount = 0;
string tmpcontxt = "";
while (retval != 0)
{
if (index + 1 > itemCount) break;
tvItem.hItem = (IntPtr) retval;
var item = new WinTreeViewItem()
{
Index = index,
lpAddress = retval
};
int r1 = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_SELECTITEM, (int) WinAPIHelper.TVM_GETNEXTITEM.TVGN_CARET, new IntPtr (retval) );
if (r1 == 0)
{
Console.WriteLine ("选择项失败");
continue;
}
//Thread.Sleep(100);
int r2 = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_EXPAND, (int) WinAPIHelper.TVM_EXPAND.TVE_EXPAND, new IntPtr (retval) );
if (r2 == 1)
{
//Console.WriteLine("可以展开");
itemCount = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_GETCOUNT, 0, IntPtr.Zero);
item.OwnChild = true;
}
else
{
//Console.WriteLine("不可以展开");
}
bool bSuccess = WinAPIHelper.WriteProcessMemory (hPro, remoteBuffer, ref tvItem, Marshal.SizeOf (tvItem), IntPtr.Zero);
if (bSuccess == false)
{
Console.WriteLine ("写入远程地址失败");
continue;
}
int r3 = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_GETITEM, 0, remoteBuffer);
if (r3 == 0)
{
Console.WriteLine ("选择项操作失败");
continue;
}
bSuccess = WinAPIHelper.ReadProcessMemory (hPro, remoteBuffer, localBuffer, Marshal.SizeOf (tvItem), IntPtr.Zero);
if (bSuccess == false)
{
Console.WriteLine ("读取远程地址失败");
continue;
}
WinAPIHelper.TVITEM retItem = (WinAPIHelper.TVITEM) Marshal.PtrToStructure (localBuffer, (Type) typeof (WinAPIHelper.TVITEM) );
int readLen = 0;
IntPtr pStrLocaAddress = Marshal.AllocHGlobal (MAX_TVMSTRING);
bSuccess = WinAPIHelper.ReadProcessMemory (hPro, pStrBufferMemory, pStrLocaAddress, MAX_TVMSTRING, out readLen);
string pszItemText = Marshal.PtrToStringUni (pStrLocaAddress);
// Console.WriteLine(pszItemText);
item.Handle = tvItem.hItem;
item.Text = pszItemText;
bool withOutAdd = false;
bool containsAdd = false;
if (item.OwnChild)
{
goto NEXTITEM;
}
if (containsList != null)
{
foreach (var conItem in containsList)
{
if (pszItemText.Trim().Contains (conItem.Trim() ) )
{
containsAdd = true; goto ADDITEM;
}
}
}
if (withOutList != null)
{
bool isContains = false;
foreach (var outItem in withOutList)
{
if (pszItemText.Trim().Contains (outItem.Trim() ) )
{
isContains = true;
goto NEXTITEM;
}
}
if (!isContains)
{
withOutAdd = true;
goto ADDITEM;
}
}
ADDITEM:
if ( (withOutList == null && containsList == null) || (withOutAdd || containsAdd) )
{
if (withOutAdd)
{
if (withOutResultItems.Count < withOutPreferenceCount && takeCount - containsPreferenceCount > withOutResultItems.Count)
{
withOutResultItems.Add (item);
addCount++;
}
}
if (containsAdd)
{
if (containsResultItems.Count < containsPreferenceCount && takeCount - withOutPreferenceCount > containsResultItems.Count)
{
containsResultItems.Add (item);
addCount++;
}
}
containsAdd = false;
withOutAdd = false;
if (addCount == takeCount)
break;
}
NEXTITEM:
retval = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_GETNEXTITEM, (int) WinAPIHelper.TVM_GETNEXTITEM.TVGN_NEXTVISIBLE, new IntPtr (retval) );
index++;
}
if (localBuffer != IntPtr.Zero)
{
try
{
Marshal.FreeHGlobal (localBuffer);
}
catch { }
}
if (remoteBuffer != IntPtr.Zero)
{
try
{
WinAPIHelper.VirtualFreeEx (hPro, remoteBuffer, 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 { }
}
withOutResultItems.AddRange (containsResultItems);
return withOutResultItems;
}
static public bool SelectItems (IntPtr hTv, List<WinTreeViewItem> items)
{
if (items.Count < 1) return false;
int r1 = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_SELECTITEM, (int) WinAPIHelper.TVM_GETNEXTITEM.TVGN_CARET, items[0].Handle);
if (items.Count == 1) return r1 == 1 ? true : false;
IntPtr hPro = WinAPIHelper.OpenProcess (WinAPIHelper.PROCESS_ALL_ACCESS, false, WndHelper.GetProcessId (hTv) );
IntPtr remoteBuffer = WinAPIHelper.VirtualAllocEx (hPro, IntPtr.Zero, buff_size, WinAPIHelper.AllocationType.Commit, WinAPIHelper.MemoryProtection.ExecuteReadWrite);
for (int i = 1; i < items.Count; i++)
{
WinAPIHelper.TVITEM tvItem = new WinAPIHelper.TVITEM();
tvItem.hItem = items[i].Handle;
tvItem.mask = WinAPIHelper.TVIF_STATE;
tvItem.state = WinAPIHelper.TVIS_SELECTED;
tvItem.stateMask = WinAPIHelper.TVIS_SELECTED;
int size = Marshal.SizeOf (tvItem);
bool result = WinAPIHelper.WriteProcessMemory (hPro, remoteBuffer, ref tvItem, size, IntPtr.Zero);
int retval = WinAPIHelper.SendMessage (hTv, (int) WinAPIHelper.TV_Messages.TVM_SETITEM, 0, remoteBuffer);
Thread.Sleep (100);
}
if (remoteBuffer != IntPtr.Zero)
{
try
{
WinAPIHelper.VirtualFreeEx (hPro, remoteBuffer, 0, WinAPIHelper.MEM_RELEASE);
}
catch { }
}
if (hPro != IntPtr.Zero)
{
try
{
WinAPIHelper.CloseHandle (hPro);
}
catch { }
}
return true;
}
}
|
Shawn
期待下篇教程,强烈支持
吾乐吧软件站
本系列教程索引目录:
(一)C# QQ讨论组广告群发工具——QQ登陆功能的实现上篇(附源码)
(二)C# QQ讨论组广告群发工具——QQ登陆功能的实现下篇(附源码)
(三)C# QQ讨论组广告群发工具——ADSL 拨号实现(附源码)
(四)C# QQ讨论组广告群发工具——操作QQ的TreeView控件(附源码)
Muyangren
看完必顶~~
Blossom
很不错,支持
晓泽
我不想说。。 被这种东西骚扰得够多了 发完就退出关闭 连举报都不行 很恶心人
吾乐吧软件站@晓泽
作为广告是可耻的,但是作为技术来学习,他是优秀的
yisa
主要是WinAPIHelper这个类库没有哦[可怜]