|
-
Jun 18th, 2008, 04:40 AM
#1
Thread Starter
Addicted Member
Read the text from another programs listview control?
I would like to read text from another programs listview control. I have searched over 3x forums for this and cant find any working examples and especially no working VB.NET examples.
I know that i have to use the command 'LVM_GETITEMTEXT' with the API Sendmessage but i have no idea how to issue the command and need a working example to go from. From what i have seen it seems more complicated then a normal sendmessage command as you have to allocate memory buffers etc which reallly confuses me .
I would be grateful if anyone could provide me with a working code example in VB.NET although i wont hold my breath as no one seems to know how to do this.
-
Jun 18th, 2008, 10:07 AM
#2
Thread Starter
Addicted Member
Re: Read the text from another programs listview control?
Ok here is the code im working on i have based it on the code found on the following forum topic:
http://www.vbforums.com/showthread.php?t=184384
I have tried to convert the code to VB.NET with little success as the program throws a Pininvoke error on the following line of code:
Code:
pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
Perhaps someone can explain what im doing wrong. I just want to read the names of items contained in another programs listview control.
Code:
Public Class Form1
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Boolean, ByVal lParam As IntPtr) As IntPtr
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Public Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Byte, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Structure LVITEM
Dim mask As Integer
Dim iItem As Integer
Dim iSubItem As Integer
Dim state As Integer
Dim stateMask As Integer
Dim pszText As Integer
Dim cchTextMax As Integer
Dim iImage As Integer
Dim lParam As Integer
Dim iIndent As Integer
End Structure
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_OPERATION = &H8
Public Const PROCESS_VM_READ = &H10
Public Const PROCESS_VM_WRITE = &H20
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const MAX_LVMSTRING As Long = 255
Public Const MEM_COMMIT = &H1000
Public Const PAGE_READWRITE = &H4
Public Const LVIF_TEXT As Long = &H1
Public Const MEM_RELEASE = &H8000&
Const LVM_FIRST = &H1000
Const LVM_GETITEMTEXT = (LVM_FIRST + 45)
Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)
Dim handle As IntPtr
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
handle = TextBox1.Text
' Aquire listview handle from the textbox
GetListviewItem(handle, 3794, 0, 0)
End Sub
Public Function GetListviewItem(ByVal hWindow As Long, ByVal ProcessID As Long, ByVal pColumn As Long, ByVal pRow As Long) As String
Dim result As Long
Dim myItem As LVITEM
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
'**********************
'init the string buffer
'**********************
ReDim strBuffer(MAX_LVMSTRING)
'***********************************************************
'open a handle to the process and allocate the string buffer
'***********************************************************
pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
'************************************************************************************
'initialize the local LV_ITEM structure
'The myItem.iSubItem member is set to the index of the column that is being retrieved
'************************************************************************************
myItem.mask = LVIF_TEXT
myItem.iSubItem = pColumn
myItem.pszText = pStrBufferMemory
myItem.cchTextMax = MAX_LVMSTRING
'**********************************************************
'write the structure into the remote process's memory space
'**********************************************************
pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
result = WriteProcessMemory(pHandle, pMyItemMemory, Val(myItem), Len(myItem), 0)
'*************************************************************
'send the get the item message and write back the memory space
'*************************************************************
result = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, pMyItemMemory)
result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
result = ReadProcessMemory(pHandle, pMyItemMemory, Val(myItem), Len(myItem), 0)
'**************************************************
'turn the byte array into a string and send it back
'**************************************************
For index = LBound(strBuffer) To UBound(strBuffer)
If Chr(strBuffer(index)) = vbNullChar Then Exit For
tmpString = tmpString & Chr(strBuffer(index))
Next index
tmpString = Trim(tmpString)
'**************************************************
'deallocate the memory and close the process handle
'**************************************************
result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
result = CloseHandle(pHandle)
If Len(tmpString) > 0 Then GetListviewItem = tmpString
End Function
End Class
-
Jun 20th, 2008, 04:33 AM
#3
Re: Read the text from another programs listview control?
for start you must not use long in .net, use int32 instead.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 20th, 2008, 05:29 AM
#4
Thread Starter
Addicted Member
Re: Read the text from another programs listview control?
Cheers for the tip . Any other suggestions why the code doesnt seem to be working? . I have changed my variable types on other occasions before but nothing i do seems to get the code running on a VB.NET setup.
Relly stuck with this issue! anyhelp appreciated
-
Jun 20th, 2008, 07:38 AM
#5
Re: Read the text from another programs listview control?
Well i'm not an API expert but to get some component values i would try to use AccessibleObjectFromWindow and child.
Also, how do you get the handle from the application you want to use?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|