Results 1 to 3 of 3

Thread: Unable to send message using SendMessage

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    2

    Question Unable to send message using SendMessage

    Hello this is my first posting here. I have read numerous postings but this is my first contribution.

    I am trying to use Sendmessage to send a string of text from one application to another. I have created a sandbox to work out the details.

    I have used the “Notepad” example and I have gotten that to function.

    This is what I have that works.


    Code:
    Public Class Xnutalk
    
       
        Private Declare Function SendMessageByString Lib "user32.dll" Alias "SendMessageA"_
        (ByVal hwnd As IntPtr, ByVal uMsg As Int32, ByVal wParam As IntPtr, ByVal lParam As String) As Integer
    
        Private Const WM_SETTEXT As Int32 = &HC
       
        Private Const WM_GETTEXTLENGTH As Int32 = &HE
    
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"_ 
        (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA"_
        (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
    
    
        Public Function GetHandle(ByVal ClassName As String) As IntPtr
    
            Dim hwnd As IntPtr = FindWindow(ClassName, Nothing) 
    
            hwnd = FindWindowEx(hwnd, IntPtr.Zero, Nothing, "Xnu talk client")
    
            If (Not hwnd.Equals(IntPtr.Zero)) Then
                Return hwnd
            Else
                Return IntPtr.Zero
            End If
        End Function
    
    
        Sub SetNewText(ByVal hwnd As IntPtr, ByVal txt As String)
            If (Not hwnd.Equals(IntPtr.Zero)) Then
                
                Call SendMessageByString(hwnd, WM_SETTEXT, IntPtr.Zero, txt)
            End If
        End Sub
    
    
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim khwnd As IntPtr
     
            khwnd = GetHandle("xnutalk client")
    
            SetNewText(khwnd, "Hello")
    
     
        End Sub
    Now what I am trying to do is modify it so instead of changing the title bar, it adds the text, in the example “Hello”, to a TextBox inside the Window. I have not been able to figure that out.

    I have tried many permutations of this and I think I am staring at trees looking for a forest.

    Here is what I have tried:
    Code:
    Public Class Xnutalk
    
       
        Private Declare Function SendMessageByString Lib "user32.dll" Alias "SendMessageA"_
        (ByVal hwnd As IntPtr, ByVal uMsg As Int32, ByVal wParam As IntPtr, ByVal lParam As String) As Integer
    
        Private Const WM_SETTEXT As Int32 = &HC
       
        Private Const WM_GETTEXTLENGTH As Int32 = &HE
    
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"_ 
        (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA"_
        (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
    
    
        Public Function GetHandle(ByVal ClassName As String) As IntPtr
    
            Dim hwnd As IntPtr = FindWindow(ClassName, Nothing) 
    
            hwnd = FindWindowEx(hwnd, IntPtr.Zero, Nothing, "chatbox")
    
            If (Not hwnd.Equals(IntPtr.Zero)) Then
                Return hwnd
            Else
                Return IntPtr.Zero
            End If
        End Function
    
    
        Sub SetNewText(ByVal hwnd As IntPtr, ByVal txt As String)
            If (Not hwnd.Equals(IntPtr.Zero)) Then
                
                Call SendMessageByString(hwnd, WM_SETTEXT, IntPtr.Zero, txt)
            End If
        End Sub
    
    
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim khwnd As IntPtr
     
            khwnd = GetHandle("xnutalk client")
    
            SetNewText(khwnd, "Hello")
    
     
        End Sub
    Where chatbox is the name of the box in question.

    Does anyone have any ideas?

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    2

    Re: Unable to send message using SendMessage

    Man I hope someone can assist me with this, I have been beating my head against a wall for some time now. I just cannot seem to understand why FindWindowEX cannot seem to find any element within xnutalk client.

    I may just have so little understanding as I do not know what I should even be asking.

    I understand from examples that FindwindowEX should find an element within the Findwindow window that is found (assuming there is one). But this is not happening.

  3. #3
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Unable to send message using SendMessage

    FindWindowEx is calling GetWindowText internally and compares the result to the parameter you specified as window title.

    GetWindowText is looking at a predefined address in the process memory for the name of the window. If the name is not there, FindWindowEx is not receiving anything (at least not a match) and so the functionality is kind of broken. This is done on purpose so that your program will not hang if the other program is not responding.

    Not every third party control conforms to the convention to store it's title where it is supposed to for example java, flash, qwidget...
    VB 2005, Win Xp Pro sp2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width