Jack智峰 2019-08-02 15:05 采纳率: 0%
浏览 438

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条回答 默认 最新

  • threenewbee 2019-08-02 15:16
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)