Finding hWnd for any window or subsection of a window

This topic contains 0 replies, has 1 voice, and was last updated by  autopilot 12 years, 6 months ago.

  • Author
    1 Star2 Stars3 Stars4 Stars5 Stars Rate it First!
  • #13519
    Posts: 432

    For use withVB 2005
    The attached module is what I use to find the different parts of the Paltalk window. It is based on code that I found here a long time ago but can not find any more. I do not know who created it originally, but I have modified it to make it more versitile. If used with my automatic paltalk detection program, you should have all the pieces to use this.

    Below are 2 examples of how to use this module:

    Private Sub SendPalText()
    RTB.Text = "Text to send"
    'set rtb font & color if wanted here
    iHnd = FindWindowHnd(WindowClass, MyRoomName, _
    RoomOutboundTextBoxClass, SendTxtIndex)
    SendMessageByString(iHnd, WM_SETTEXT, 0, RTB.Rtf)
    SendMessageByString(iHnd, WM_KEYDOWN, 13, 0)
    End Sub[/vb]
    [vb]Function FindNic() As String
    iHnd = FindWindowHnd(WindowClass, MyRoomName, SysListCtrlClass, _
    FindNic = GetListviewItem(iHnd)
    End Function[/vb]
    To use the FindNic sample, you will need to also have the GetListviewItem function (code from BattleStar-Galactica).
    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 GetListviewItem(ByVal lstviewhwnd As Integer) 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 itemIndex As Integer
    Dim tmpString As String = String.Empty
    Dim ProcessID As Integer
    Dim usernum As Integer
    Dim i As Short
    UserNic = String.Empty
    itemIndex = SendMessage(lstviewhwnd, LVM_GETNEXTITEM, _
    usernum = SendMessage(lstviewhwnd, LVM_GETITEMCOUNT, _
    0, 0)
    'init the string buffer
    ReDim strBuffer(MAX_LVMSTRING)
    'open a handle to the process and allocate the string buffer
    Call GetWindowThreadProcessId(lstviewhwnd, ProcessID)
    pHandle = OpenProcess(PROCESS_VM_OPERATION Or _
    pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, _
    '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 = MAX_LVMSTRING
    'write the structure into the remote process's memory space
    pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), _
    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, _
    result = ReadProcessMemory(pHandle, pStrBufferMemory, _
    strBuffer(0), MAX_LVMSTRING, 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)
    Return tmpString
    End Function


    Edit: we have a vb tag 😉
    Edit: fixed variable names to match autodetecte project

You must be logged in to reply to this topic.