Skip to content

Re: Re: An easy VB 2010 Send and Gettext Sample

#186843
Admin
Administrator

Hey Auto, I know you gonna work on the selecting nick thing next, but I got a quick question this code in your quickkeys program the module
mdlSysListView.vb

Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, ByVal dwSize As Integer, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As IntPtr
Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, ByVal dwSize As Integer, ByVal dwFreeType As Integer) As Integer
Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As LV_ITEMA, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As LV_ITEMA, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Byte, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As StringBuilder, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcId As Integer) As IntPtr
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer

Const StringBufferLength As Integer = 255
Const LVM_FIRST As Integer = &H1000
Const LVM_GETITEMCOUNT As Integer = (LVM_FIRST + 4)
Const LVM_GETNEXTITEM As Integer = (LVM_FIRST + 12)
Const LVM_FINDITEM As Long = (LVM_FIRST + 13)
Const LVM_SETITEMPOSITION As Integer = (LVM_FIRST + 15)
Const LVM_GETITEMPOSITION As Integer = (LVM_FIRST + 16)
Const LVM_GETITEMTEXT As Integer = (LVM_FIRST + 45)
Const LVM_SORTITEMS As Integer = (LVM_FIRST + 48)
Const LVM_GETSELECTEDCOUNT As Integer = (LVM_FIRST + 50)
Const LVM_GETITEM As Integer = (LVM_FIRST + 75)
Const LVNI_SELECTED As Integer = &H2
Const LVFI_PARAM As Integer = &H1
Const LVIF_TEXT As Integer = &H1
Const LVM_FINDITEMW As Integer = (LVM_FIRST + 83)
Const LVIS_SELECTED As Integer = &H2
Const LVIF_STATE As Integer = &H8
Const LVM_SETITEMSTATE As Integer = (LVM_FIRST + 43)
Const PROCESS_VM_OPERATION As Integer = &H8S
Const PROCESS_VM_READ As Integer = &H10S
Const PROCESS_VM_WRITE As Integer = &H20S
Const MEM_COMMIT As Integer = &H1000S
Const PAGE_READWRITE As Integer = &H4S
Const MEM_RELEASE As Integer = &H8000S
Const MEM_RESERVE As Integer = &H2000S


Structure LV_ITEMA
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 Function SLVItemIndex(ByVal lstviewhwnd As IntPtr, ByVal Item As String, ByRef ItemIndex As Integer) As Boolean
Dim result As Integer
Dim myItem As LV_ITEMA
Dim pHandle As Integer
Dim pStrBufferMemory As Integer
Dim pMyItemMemory As Integer
Dim strBuffer() As Byte
Dim index As Integer
Dim tmpString As String = String.Empty
Dim ProcessID As Integer
Dim ItemCount As Integer
Dim i As Short

ItemCount = SendMessage(lstviewhwnd, LVM_GETITEMCOUNT, 0, 0)
'**********************
'init the string buffer
'**********************
ReDim strBuffer(StringBufferLength)
'***********************************************************
'open a handle to the process and allocate the string buffer
'***********************************************************
Call GetWindowThreadProcessId(lstviewhwnd, ProcessID)
pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
pStrBufferMemory = VirtualAllocEx(pHandle, 0, StringBufferLength, 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 = 2
myItem.pszText = pStrBufferMemory
myItem.cchTextMax = StringBufferLength
'**********************************************************
'write the structure into the remote process's memory space
'**********************************************************
pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

'*************************************************************
'*************************************************************
'*************************************************************

For i = 0 To ItemCount - 1
'*************************************************************
'send the get the item message and write back the memory space
'*************************************************************
result = SendMessage(lstviewhwnd, LVM_GETITEMTEXT, i, pMyItemMemory)
result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), StringBufferLength, 0)
result = ReadProcessMemory(pHandle, pMyItemMemory, 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)
If LCase(tmpString) = LCase(Item) Or LCase(tmpString) = "@" & LCase(Item) Then
ItemIndex = i
SLVItemIndex = True
Exit For
End If
tmpString = String.Empty
Next

'**************************************************
'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)
End Function

Public Function HighLightSLVItem(ByVal hwnd As IntPtr, ByVal ItemIndex As Integer) As Boolean
'found at
Dim lProcID As Integer
Dim hProc As IntPtr
Dim lxprocLVITEM As Integer
Dim LVITEM As LV_ITEMA
Dim lItemPos As Integer

GetWindowThreadProcessId(hwnd, lProcID) ' Get the process ID in which the ListView is running
If lProcID 0 Then
hProc = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, lProcID) ' makwe sure we have read write permissions in the process space
If hProc 0 Then
lxprocLVITEM = VirtualAllocEx(hProc, 0, Len(LVITEM), MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE) ' Grab enough memory in the other procedure's space to hold our LV_ITEM

' Set up our local LV_ITEM to change the selected item
LVITEM.mask = LVIF_STATE
LVITEM.state = True
LVITEM.stateMask = LVIS_SELECTED

' Copy the local LV_ITEM into the space we reserved in the foreign process
WriteProcessMemory(hProc, lxprocLVITEM, LVITEM, Len(LVITEM), 0)

' Now send the message, but pass the address of the copy of our LV_ITEM that now exists in the foreign process instead of our local versiony
lItemPos = ItemIndex ' first item
SendMessage(hwnd, LVM_SETITEMSTATE, lItemPos, lxprocLVITEM)

' Clean up
VirtualFreeEx(hProc, lxprocLVITEM, Len(LVITEM), MEM_RELEASE)
CloseHandle(hProc)
HighLightSLVItem = True
End If
End If
End Function

Public Function GetSLVSelectedItem(ByVal lstviewhwnd As IntPtr) As String
Dim result As Integer
Dim myItem As LV_ITEMA
Dim pHandle As Integer
Dim pStrBufferMemory As Integer
Dim pMyItemMemory As Integer
Dim strBuffer() As Byte
Dim index As Integer
Dim tmpString As String = String.Empty
Dim ProcessID As Integer
Dim usernum, itemIndex As Integer
Dim i As Short
itemIndex = SendMessage(lstviewhwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED)
usernum = SendMessage(lstviewhwnd, LVM_GETITEMCOUNT, 0, 0)
'**********************
'init the string buffer
'**********************
ReDim strBuffer(StringBufferLength)
'***********************************************************
'open a handle to the process and allocate the string buffer
'***********************************************************
Call GetWindowThreadProcessId(lstviewhwnd, ProcessID)
pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
pStrBufferMemory = VirtualAllocEx(pHandle, 0, StringBufferLength, 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 = 2
myItem.pszText = pStrBufferMemory
myItem.cchTextMax = StringBufferLength
'**********************************************************
'write the structure into the remote process's memory space
'**********************************************************
pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

'*************************************************************
'*************************************************************
'*************************************************************

i = itemIndex
'*************************************************************
'send the get the item message and write back the memory space
'*************************************************************
result = SendMessage(lstviewhwnd, LVM_GETITEMTEXT, i, pMyItemMemory)
result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), StringBufferLength, 0)
result = ReadProcessMemory(pHandle, pMyItemMemory, 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 GetSLVSelectedItem = tmpString
Return tmpString
End Function

Are you suppose to call it with this function?

  Function FindNic() As String
Dim iHnd As IntPtr = mdlGetHnd.SubFormHndByClassNameWithMWClassWithPartialMWCaption(mdlPalInfo.ChatRoomClass, Me.CtrlRoomSelector1.RoomName, mdlPalInfo.NicListClass, mdlPalInfo.NicListIndex)
Return mdlSysListView.GetSLVSelectedItem(iHnd)
End Function

By using this command
FindNic(nick.text) To select the nick?