Shoutbox

hWnd of IMWindowClass chat input box - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Forum: Plug-Ins (/forumdisplay.php?fid=28)
+------ Thread: hWnd of IMWindowClass chat input box (/showthread.php?tid=51617)

hWnd of IMWindowClass chat input box by eSouL on 10-10-2005 at 03:32 PM

Hi,

I'm using VB. How do I get the hwnd of the text box where user inputs his chat messages?


Second question:

What is the RegisterWindowMessage of MSN to send a chat message? And what is the hwnd to which the SendMessage function sends?
RE: hWnd of IMWindowClass chat input box by J-Thread on 10-10-2005 at 04:19 PM

2nd question: There is no RegisterWindowMessage for sending chat messages with msn. You have to send the chars to the input box and end with a Return, so the text is send. There has been a discussion about this lately, just search on the forums.


RE: hWnd of IMWindowClass chat input box by matty on 10-10-2005 at 04:28 PM

To find the hWnd of the textbox you will need to use ActiveAccessibility. Note that it isn't all that stable in VB Dempsey and I have found.

Matty's reply to Active Accessibility for incoming messages


RE: hWnd of IMWindowClass chat input box by eSouL on 10-10-2005 at 04:39 PM

Matty, is there a method to set the text of the textbox or perhaps sendkeys to it?


RE: hWnd of IMWindowClass chat input box by Stigmata on 10-10-2005 at 04:57 PM

quote:
Originally posted by eSouL
is there a method to set the text of the textbox

yep, using active accessibility :)
RE: hWnd of IMWindowClass chat input box by J-Thread on 10-10-2005 at 04:58 PM

Interesting code Matty, that may be really usefull!(Y)

eSould, take a look at this thread;)


RE: hWnd of IMWindowClass chat input box by matty on 10-10-2005 at 05:11 PM

quote:
Originally posted by Stigmata
quote:
Originally posted by eSouL
is there a method to set the text of the textbox

yep, using active accessibility :)
http://forums.msnfanatic.com/index.php?showtopic=11778



quote:
Originally posted by eSouL
Matty, is there a method to set the text of the textbox or perhaps sendkeys to it?

In a Module
code:
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
                                                                         ByVal hWnd2 As Long, _
                                                                         ByVal lpsz1 As String, _
                                                                         ByVal lpsz2 As String) _
                                                                         As Long
Public Declare Function
GetForegroundWindow Lib "user32" () As Long
Public Declare Function
PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, _
                                                                       ByVal wMsg As Long, _
                                                                       ByVal wParam As Long, _
                                                                       ByVal lParam As Long) _
                                                                       As Long
Public Declare Function
SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function
WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
                                                            ByVal dwMilliseconds As Long) _
                                                            As Long

Public Const WM_CHAR = &H102
Public Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const INFINITE = &HFFFF

Public lngPrevWnd As Long
Public
ConversationWnd_Obj As Object

Public Sub
SendText(hwnd As Long, _
                    sText As String, _
                    Optional bSend As Boolean = False, _
                    Optional bKeepFocus As Boolean = True)
Dim RetVal As Long

    RetVal = SetForegroundWindow(hwnd)
    WaitForSingleObject RetVal, INFINITE
   
    For i = 1 To Len(sText)
        Call PostMessage(hwnd, WM_CHAR, Asc(Mid(sText, i, 1)), 0)
    Next i
   
    If bSend Then
        Call
PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0&)
        Call PostMessage(hwnd, WM_KEYUP, VK_RETURN, 0&)
    End If
   
    If Not
bKeepFocus Then
        Call
SetForegroundWindow(lngPrevWnd)
    End If
End Sub




In your Class Module
code:
Public Function ParseCommand(ByVal sCommand As String, _
                             ByVal sCommandArg As String, _
                             ByVal oConversationWnd As Object, _
                             ByRef sResult As String) _
                             As Boolean

    Set
ConversationWnd_Obj = oConversationWnd
   
    Select Case LCase(sCommand)

        Case Is = "/xtest"
            lngPrevWnd = GetForegoundWindow()
            SendText FindWindowEx(ConversationWnd_Obj.hWnd, _
                                  0&, _
                                  "DirectUIHWND", _
                                  vbNullString), _
                     "this text will be sent", _
                     True, _
                     True

            sResult = ""
            ParseCommand = True
            Exit Function

    End Select


    ParseCommand = False

End Function



RE: hWnd of IMWindowClass chat input box by Mike on 10-10-2005 at 05:14 PM

So, using AA, you can get a hWnd to the send text box, and do stuff with it? (like type a message just like at your example)

I didn't know it! :O


RE: hWnd of IMWindowClass chat input box by matty on 10-10-2005 at 05:17 PM

quote:
Originally posted by Mike
So, using AA, you can get a hWnd to the send text box, and do stuff with it? (like type a message just like at your example)

I didn't know it! :O

Yes using AA  you can do that, but not with example I have. The axample I have just reads the text and displays the parent windows hWnd not the textbox one. Now I am not sure if the hWnd passed through is the textbox or the DirectUIHWND. But the code I posted will actually put text in the window not using AA.

The link to msnfanatic forums is from TheHuuf who posted sending text using AA.
RE: hWnd of IMWindowClass chat input box by Plik on 10-10-2005 at 05:36 PM

quote:
Originally posted by Matty
code:
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function
PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, _
                                                                       ByVal wMsg As Long, _
                                                                       ByVal wParam As Long, _
                                                                       ByVal lParam As Long) _
                                                                       As Long
Public Declare Function
SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function
WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
                                                            ByVal dwMilliseconds As Long) _
                                                            As Long

Public Const WM_CHAR = &H102
Public Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const INFINITE = &HFFFF

Public lngPrevWnd As Long
Public
ConversationWnd_Obj As Object

Public Sub
SendText(hwnd As Long, _
                    sText As String, _
                    Optional bSend As Boolean = False, _
                    Optional bKeepFocus As Boolean = True)
Dim RetVal As Long

    RetVal = SetForegroundWindow(hwnd)
    WaitForSingleObject RetVal, INFINITE
   
    For i = 1 To Len(sText)
        Call PostMessage(hwnd, WM_CHAR, Asc(Mid(sText, i, 1)), 0)
    Next i
   
    If bSend Then
        Call
PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0&)
        Call PostMessage(hwnd, WM_KEYUP, VK_RETURN, 0&)
    End If
   
    If Not
bKeepFocus Then
        Call
SetForegroundWindow(lngPrevWnd)
    End If
End Sub



IIRC, It isnt neccessary to set the windows focus to use post message to send keys.

RE: hWnd of IMWindowClass chat input box by eSouL on 10-10-2005 at 06:09 PM

Hi Matty, your code is fantastic! It helped me very much :D

Except for one thing, the message does not seem to get sent at the SendText function (the contact that is supposed to receive the message does not receive anything). I have verified that both the parameters hwnd and sText are correctly passed in.

Any ideas?


RE: hWnd of IMWindowClass chat input box by matty on 10-10-2005 at 06:50 PM

code:
SendText ConversationWnd_Obj.hWnd, "this text will be sent", True, True

Are you passing True to both parts of the function? The first one is to send the text and the second is to return the original windows focus.
RE: hWnd of IMWindowClass chat input box by eSouL on 10-10-2005 at 07:03 PM

Yup I passed both as true.

I got around the problem by using sendkeys instead.


RE: hWnd of IMWindowClass chat input box by matty on 10-10-2005 at 08:23 PM

Sorry I made a mistake in the code

You also need to include this

Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

And instead of passing ConversationWnd_Obj.hWnd you need to pass

SendText FindWindowEX(ConversationWnd_Obj.hWnd, 0&, "DirectUIHWND", vbNullString), "the text", true, true


RE: hWnd of IMWindowClass chat input box by eSouL on 10-11-2005 at 03:47 AM

Strangely, posting to DirectUIHWND still doesn't do anything. :O


RE: hWnd of IMWindowClass chat input box by matty on 10-11-2005 at 03:55 AM

quote:
Originally posted by eSouL
Strangely, posting to DirectUIHWND still doesn't do anything. :O
Thats odd cause it works with no problem here in my plugin I am developing.

Here is the actual code I have.

code:
        Dim lngNewWindow As Long
        Dim strTextToSend As String
        If Contacts_lv.SelectedItem.Tag <> "" Then
            lngPrevWnd = GetForegroundWindow()
            MessengerAPI_Obj.InstantMessage Contacts_lv.SelectedItem.Tag
            lngNewWindow = GetForegroundWindow()
            lngNewWindow = FindWindowEx(lngNewWindow, 0, "DirectUIHWND", vbNullString)
            SendText lngNewWindow, "/dropfile " & Me.Tag, True, False
            Unload Me
        End If

RE: hWnd of IMWindowClass chat input box by eSouL on 10-12-2005 at 05:13 AM

I shifted the code out and tested it on a separate project, but it still doesn't work. Anything is wrong?

code:
Dim hwnd_msn As Long
Dim hwnd_msn_edit As Long
hwnd_msn = FindWindow("IMWindowClass", vbNullString)
hwnd_msn_edit = FindWindowEx(hwnd_msn, 0&, "DirectUIHWND", vbNullString)

Dim sText As String
sText = Text1.text
Text1.text = ""
   
For i = 1 To Len(sText)
  Call PostMessage(hwnd_msn_edit, WM_CHAR, Asc(Mid(sText, i, 1)), 0)
Next i
   
Call PostMessage(hwnd_msn_edit, WM_KEYDOWN, VK_RETURN, 0&)
Call PostMessage(hwnd_msn_edit, WM_KEYUP, VK_RETURN, 0&)



EDIT:
I realized that the above code will only work if I bring the window to the front first using this code:
code:
   
    Dim RetVal As Long
    RetVal = SetForegroundWindow(hwnd_msn_edit)
    WaitForSingleObject RetVal, INFINITE


It wasn't very reliable though. Some messages would not get sent. Some concatenated and got sent as one line. (Tested it by repeatedly sending string of messsages through a text box)

Funny thing is, posting messages to Notepad actually works without having to bring the window to the front. I wonder why the difference. *-)