Я использую приведенный ниже код, чтобы получить текст, содержащийся в SysListView32, но он всегда возвращает пустую строку в retval.
Мне нужно захватить все содержимое всех строк и столбцов.
В чем ошибка?
Я работаю в VB. NET Проект e, и я использую Windows 10 - 64 бита.
Извините за мой плохой английский sh.
Public Shared Function ReadListViewItem(ByVal hWnd As IntPtr, ByVal item As Integer, ByVal subItem As Integer) As String
Const dwBufferSize As Integer = 1024
Dim dwProcessID As Integer
Dim lvItem As LV_ITEM
Dim retval As String
Dim bSuccess As Boolean
Dim hProcess As IntPtr = IntPtr.Zero
Dim lpRemoteBuffer As IntPtr = IntPtr.Zero
Dim lpLocalBuffer As IntPtr = IntPtr.Zero
Dim threadId As IntPtr = IntPtr.Zero
Dim myIntPtr As IntPtr
Try
lvItem = New LV_ITEM
lpLocalBuffer = Marshal.AllocHGlobal(dwBufferSize)
' Get the process id owning the window
threadId = GetWindowThreadProcessId(hWnd, dwProcessID)
If (threadId = IntPtr.Zero) OrElse (dwProcessID = 0) Then
Throw (New ArgumentException("hWnd"))
End If
' Open the process with all access
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID)
If hProcess = IntPtr.Zero Then
Throw (New ApplicationException("Falha ao acessar o processo."))
End If
' Allocate a buffer in the remote process
lpRemoteBuffer = VirtualAllocEx(hProcess, IntPtr.Zero, dwBufferSize, MEM_COMMIT, PAGE_READWRITE)
If lpRemoteBuffer = IntPtr.Zero Then
Throw (New SystemException("Falha a alocar memória no processo remoto."))
End If
' Fill in the LVITEM struct, this is in your own process
' Set the pszText member to somewhere in the remote buffer,
' For the example I used the address imediately following the LVITEM stuct
lvItem.mask = LVIF_TEXT Or LVIF_IMAGE
lvItem.iItem = item
myIntPtr = New IntPtr((lpRemoteBuffer.ToInt32() + Marshal.SizeOf(GetType(LV_ITEM))))
lvItem.pszText = myIntPtr
lvItem.cchTextMax = 50
lvItem.iSubItem = subItem
lvItem.iImage = 1
'lvItem.cchTextMax = 1
' Copy the local LVITEM to the remote buffer
bSuccess = WriteProcessMemory(hProcess, lpRemoteBuffer, lvItem, Marshal.SizeOf(GetType(LV_ITEM)), IntPtr.Zero)
If Not bSuccess Then
Throw (New SystemException("Falha ao escrever processo na memória."))
End If
' Send the message to the remote window with the address of the remote buffer
SendMessage(hWnd, LVM_GETITEM, 0, lpRemoteBuffer)
' Read the struct back from the remote process into local buffer
bSuccess = ReadProcessMemory(hProcess, lpRemoteBuffer, lpLocalBuffer, dwBufferSize, IntPtr.Zero)
If Not bSuccess Then
Throw (New SystemException("Falha ao ler processo da memória."))
End If
' At this point the lpLocalBuffer contains the returned LV_ITEM structure
' the next line extracts the text from the buffer into a managed string
myIntPtr = New IntPtr((lpLocalBuffer.ToInt32() + Marshal.SizeOf(GetType(LV_ITEM))))
retval = Marshal.PtrToStringAnsi(myIntPtr)
Catch ex As ArgumentOutOfRangeException
Console.Write(ex.Message)
retval = ""
Finally
If lpLocalBuffer <> IntPtr.Zero Then
Marshal.FreeHGlobal(lpLocalBuffer)
End If
If lpRemoteBuffer <> IntPtr.Zero Then
VirtualFreeEx(hProcess, lpRemoteBuffer, 0, MEM_RELEASE)
End If
If hProcess <> IntPtr.Zero Then
CloseHandle(hProcess)
End If
End Try
Return (retval)
End Function