vb读取其他程序的listview控件内容,itemcount可以获得,但是内容总是返回为空

如题:

Public Function GetListViewTextArray(ByVal hWindow As Long, ByVal ProcessID As Long) As String()
    Dim result              As Long
    Dim myItem()              As LV_ITEMA
    Dim pHandle             As Long
    Dim pStrBufferMemory    As Long
    Dim pMyItemMemory       As Long
    Dim strBuffer()         As Byte
    Dim index               As Long
    Dim tmpString           As String
    Dim strLength           As Long
    Dim i As Integer, sum As Integer, j As Integer, hCount As Long
    Dim strArr() As String, itemString As String
    hCount = SendMessage(hWindow, LVM_GETHEADER, 0, 0)
    If hCount > 0 Then
        hCount = SendMessage(hCount, HDM_GETITEMCOUNT, 0, 0)
    Else
        hCount = 0
    End If
    ReDim strBuffer(MAX_LVMSTRING)
    pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
    ReDim myItem(hCount)
    For j = 0 To SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0) - 1
        For i = 0 To hCount
            pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
            myItem(i).mask = LVIF_TEXT
            myItem(i).iSubItem = i
            myItem(i).pszText = pStrBufferMemory
            myItem(i).cchTextMax = MAX_LVMSTRING
            pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem(i)), MEM_COMMIT, PAGE_READWRITE)
            result = WriteProcessMemory(pHandle, pMyItemMemory, myItem(i), Len(myItem(i)), 0)
            result = SendMessage(hWindow, LVM_GETITEMTEXT, j, ByVal pMyItemMemory)
            If result = 0 Then
                result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
                result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
                Exit For
            End If
            result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
            result = ReadProcessMemory(pHandle, pMyItemMemory, myItem(i), Len(myItem(i)), 0)
            tmpString = StrConv(strBuffer, vbUnicode)
            tmpString = Left(tmpString, InStr(tmpString, vbNullChar) - 1)
            itemString = itemString & tmpString & ","
            result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
            result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
        Next
        ReDim Preserve strArr(0 To sum)
        strArr(j) = Left(itemString, Len(itemString) - 1)
        sum = sum + 1
        itemString = ""
    Next
    result = CloseHandle(pHandle)
    GetListViewTextArray = strArr
End Function

1个回答

这种直接读取内存的方式,必须考虑windows的版本兼容性。在代码编写者电脑上可以运行,只要换了一个版本的windows,内存分布不同,就失效了。

caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复Jack智峰: FindWindowEx
10 个月之前 回复
weixin_44958290
Jack智峰 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 可是我是用EnumChildWindows一层一层遍历的,找到窗口的句柄之后,接下去用EnumChildWindows就找不到了
10 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 这种类似外挂的程序,你要开发,首先要搞清楚原理,其次,要对所有版本的操作系统分别处理
10 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复Jack智峰: 那肯定是你层次关系没有搞对,嵌套在子窗体里,要一层一层往下获取。
10 个月之前 回复
weixin_44958290
Jack智峰 还有一个问题就是:有时候,我遍历其他程序窗体控件的时候,有时候会找不到listview控件,但是用SPY++又可以看到
10 个月之前 回复
weixin_44958290
Jack智峰 那要怎么办?还有什么办法可以直接读取listview控件的内容?有时候,我遍历其他程序窗体控件
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐