新手求救:获取其他程序的TreeView项目内容,请求各位大侠的帮助!
新手求救:从网上收索各位大侠们的杰作,现勉强可以历遍TreeView各个项目,但苦于找不到获取项目的内容,第一次发帖请求各位大侠们的指点。
代码如下:
Private Type TvwLvwItem
mask As Long
iItem As Long
iSubItem As Long
state As Long
stateMask As Long
pszText As Long
cchTextMax As Long
iImage As Long
lParam As Long
iIndent As Long
End Type
Sub GetTreeView(SourceHwnd As Long, TargetHwnd As Long, Optional AppendMode As Boolean)
Dim ItemHwnd As Long
Dim ItemExpand As Long
Dim SourceID As Long
Dim SourceHandle As Long
Dim sItemTexts As String
Const ItemLens = 1000
MyItems.mask = LVIF_TEXT
MyItems.cchTextMax = ItemLens
If Not AppendMode Then
SendMessage TargetHwnd, LB_RESETCONTENT, 0, ByVal 0&
End If
GetWindowThreadProcessId SourceHwnd, SourceID
SourceHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, SourceID)
ItemHwnd = SendMessage(SourceHwnd, TVM_GETNEXTITEM, TVGN_ROOT, ByVal 0&)
Do While ItemHwnd <> 0
MyItems.iIndent = ItemHwnd
SendMessage SourceHwnd, TVM_SELECTITEM, TVGN_CARET, ByVal ItemHwnd
sItemTexts = GetTreeViewItem(SourceHwnd, SourceHandle)
SendMessage TargetHwnd, LB_ADDSTRING, 0&, ByVal sItemTexts
SendMessage SourceHwnd, TVM_EXPAND, TVE_EXPAND, ByVal ItemHwnd
ItemHwnd = SendMessage(SourceHwnd, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, ByVal ItemHwnd)
Loop
CloseHandle (SourceHandle)
End Sub
Private Function GetTreeViewItem(ByVal hWnd As Long, ByVal pHandle As Long) As String
Dim MyItemMemory As Long
Dim Result As Long
Dim Buffer As String
'Dim Buffer() As Byte
Dim tmpString As String
'ReDim Buffer(MyItems.cchTextMax)
MyItems.pszText = VirtualAllocEx(pHandle, 0, MyItems.cchTextMax, MEM_COMMIT, PAGE_READWRITE)
MyItemMemory = VirtualAllocEx(pHandle, 0, Len(MyItems), MEM_COMMIT, PAGE_READWRITE)
WriteProcessMemory pHandle, MyItemMemory, MyItems, Len(MyItems), 0
Result = SendMessage(hWnd, TVM_GETITEM, 0, ByVal MyItemMemory) ‘运行到此处当掉,发送失败,Result=0
If Result = 0 Then
VirtualFreeEx pHandle, MyItems.pszText, 0, MEM_RELEASE
VirtualFreeEx pHandle, MyItemMemory, 0, MEM_RELEASE
Exit Function
End If
ReadProcessMemory pHandle, MyItems.pszText, Buffer, MyItems.cchTextMax, 0
ReadProcessMemory pHandle, MyItemMemory, MyItems, Len(MyItems), 0
'tmpString = StrConv(Buffer, vbUnicode)
tmpString = Buffer
If InStr(tmpString, Chr$(0)) > 0 Then
tmpString = Left$(tmpString, InStr(tmpString, Chr$(0)) - 1)
End If
tmpString = Trim$(tmpString)
VirtualFreeEx pHandle, MyItems.pszText, 0, MEM_RELEASE
VirtualFreeEx pHandle, MyItemMemory, 0, MEM_RELEASE
If Len(tmpString) > 0 Then GetTreeViewItem = tmpString
End Function
不清楚SendMessage(hWnd, TVM_GETITEM, 0, ByVal MyItemMemory)最后一个参数要传什么进去,不知道是否有误,还是前面有问题,请高手们指点一下,还有ReadProcessMemory pHandle, MyItems.pszText, Buffer, MyItems.cchTextMax, 0 是否可以传入Buffer As String,还是一定要Buffer() As Byte?
[解决办法]
没想到这位仁兄和我有一样的遭遇呀,支持下
[解决办法]
http://topic.csdn.net/u/20110411/12/7aebff91-5fbf-4131-9b4a-eb80e45f814c.html?1664261547