Windows Messaging - SendMessage() doesn't work on a window already found

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Hi. I am trying to SendMessage to an app called 'Signal threads' - a test app which sends out a message called TV1_Threads, and listens for a reply. I have shown the values of each variable in the commented areas.
VB.NET:
        Dim windowHandle As Integer
        Dim controlHandle As Integer

        Dim WM_SEE_VB_MESSAGE_1 As Integer
        Dim ret As Integer

        windowHandle = FindWindow(vbNullString, "Signal threads")   '2623292
        controlHandle = FindWindowEx(windowHandle, 0, vbNullString, vbNullString)   '2426820

        WM_SEE_VB_MESSAGE_1 = RegisterWindowMessage("TV1_Threads")   '50187

        ret = SendMessage(controlHandle, WM_SEE_VB_MESSAGE_1, 0, 0)   '0

ret = 0 and the receiving app (Signal threads) does not show that the message was received.

If I use the BROADCAST way of doing it,
VB.NET:
        Dim HWND_BROADCAST As Integer = &HFFFF&
        Dim WM_SEE_VB_MESSAGE_1 As Integer
        Dim ret As Integer

        WM_SEE_VB_MESSAGE_1 = RegisterWindowMessage("TV1_Threads")
        ret = SendMessage(HWND_BROADCAST, WM_SEE_VB_MESSAGE_1, 0, 0)
this way works great and the Signal threads app responds every time.

What's wrong with the first code sample? Thanks in advance.
 
You are sending to the first control window in that app, do you have a WndProc override in that class? If you do it will work. If you override WndProc in the main form of "Signal threads" app you just send to the windowHandle. That HWND_BROADCAST works for you indicates that you are receiving in main window and not in a control window class.

By the way documentation for RegisterWindowMessage recommends usage only for broadcasting to multiple windows, if not it recommends using a custom WM_User message directly to window class, or custom WM_APP message to application window.
 
The only class is Public Class Form1, there's no WndProc override. The entire code is as below. Button3 is the broadcast one, which works fine. Button4 doesn't. The app Signal threads is a test app written in Delphi by someone else just to test this code. I am trying to teach myself about WndProc, any pointers will be appreciated, thank you.

VB.NET:
Imports System.Windows.Forms

Public Class Form1

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
    Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageW" (ByVal lpString As String) As Integer
    Private Const WM_CHAR = &H102

    'Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
    'End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Application.Exit()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim HWND_BROADCAST As Integer = &HFFFF&
        Dim WM_SEE_VB_MESSAGE_1 As Integer
        Dim ret As Integer

        Try

            WM_SEE_VB_MESSAGE_1 = RegisterWindowMessage("TV1_Threads")
            ret = SendMessage(HWND_BROADCAST, WM_SEE_VB_MESSAGE_1, 0, 0)
            'MsgBox(ret)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim windowHandle As Integer
        Dim controlHandle As Integer

        Dim WM_SEE_VB_MESSAGE_1 As Integer
        Dim ret As Integer

        Try

            windowHandle = FindWindow(vbNullString, "Signal threads")
            controlHandle = FindWindowEx(windowHandle, 0, vbNullString, vbNullString)

            WM_SEE_VB_MESSAGE_1 = RegisterWindowMessage("TV1_Threads")

            ret = SendMessage(controlHandle, WM_SEE_VB_MESSAGE_1, 0, 0)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class
 
As I said, if your broadcast is fine, then sending your message directly to the window (windowHandle) will also work, because that is what a broadcast does.
 
Back
Top