Pop up message to remote computer

Abdelmalek Aitouche 176 Reputation points
2021-10-19T13:52:04.677+00:00

Hello,
I am trying to send a pop up notification (message) to a remote computer using some proposed methods using the command prompt always unsuccessfully. Could you please help with a Visual Basic program to do this.

Thanks a lot.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,726 Reputation points
    2021-10-19T15:11:20.857+00:00

    Maybe you can test with WTS APIs
    I'm not on a network, so I cannot test with a remote computer
    Change the server name or use WTS_CURRENT_SERVER_HANDLE :

            Dim pSessions As IntPtr = IntPtr.Zero
            Dim nSessions As Integer
            Dim pServer = WTSOpenServer("Server_Name")
            '  If WTSEnumerateSessions(CType(WTS_CURRENT_SERVER_HANDLE, IntPtr), 0, 1, pSessions, nSessions) Then
            If WTSEnumerateSessions(pServer, 0, 1, pSessions, nSessions) Then
                Dim nDataSize As Integer = Marshal.SizeOf(GetType(WTS_SESSION_INFO))
                Dim pCurrentSession As IntPtr = pSessions
                For Index As Integer = 0 To nSessions - 1
                    Dim si As WTS_SESSION_INFO = CType(Marshal.PtrToStructure(pCurrentSession, GetType(WTS_SESSION_INFO)), WTS_SESSION_INFO)
                    Dim nResponse As Integer = 0
                    Dim sMessageTitle As String = "Title"
                    Dim sMessageText As String = "Message"
                    Dim nTitleLength As Integer = sMessageTitle.Length * 4
                    Dim nMessageLength As Integer = sMessageText.Length * 4
                    If si.State = WTS_CONNECTSTATE_CLASS.WTSActive Then
                        'Dim bMessageReturn As Boolean = WTSSendMessage(CType(WTS_CURRENT_SERVER_HANDLE, IntPtr), si.SessionId, sMessageTitle, nTitleLength, sMessageText, nMessageLength, CInt((CInt(MessageBoxButtons.OK) Or CInt(MessageBoxIcon.Exclamation))), 0, nResponse, False)
                        Dim bMessageReturn As Boolean = WTSSendMessage(pServer, si.SessionId, sMessageTitle, nTitleLength, sMessageText, nMessageLength, CInt((CInt(MessageBoxButtons.OK) Or CInt(MessageBoxIcon.Exclamation))), 0, nResponse, False)
                        If Not bMessageReturn Then
                            Dim nError As Integer = Marshal.GetLastWin32Error()
                            System.Windows.Forms.MessageBox.Show("Error : " & nError.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                        End If
                    End If
                    pCurrentSession += nDataSize
                Next
                WTSFreeMemory(pSessions)
                WTSCloseServer(pServer)
            End If
    

    Declarations :

        Public Const INVALID_HANDLE_VALUE As Integer = -1
        Public Const WTS_CURRENT_SERVER_HANDLE As Integer = 0
        Public Const WTS_CURRENT_SESSION As Integer = -1
    
        <DllImport("WTSApi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function WTSSendMessage(hServer As IntPtr, SessionId As Integer, pTitle As String, TitleLength As Integer, pMessage As String, MessageLength As Integer, Style As Integer, Timeout As Integer, <Out> ByRef pResponse As Integer, bWait As Boolean) As Boolean
        End Function
    
        <DllImport("WTSApi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function WTSEnumerateSessions(hServer As IntPtr, Reserved As Integer, Version As Integer, <Out> ByRef ppSessionInfo As IntPtr, <Out> ByRef pCount As Integer) As Boolean
        End Function
    
        <DllImport("WTSApi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Sub WTSFreeMemory(pMemory As IntPtr)
        End Sub
    
        <DllImport("WTSApi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function WTSOpenServer(pServerName As String) As IntPtr
        End Function
    
        <DllImport("WTSApi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Sub WTSCloseServer(hServer As IntPtr)
        End Sub
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
        Public Structure WTS_SESSION_INFO
            Public SessionId As Integer
            Public pWinStationName As String
            Public State As WTS_CONNECTSTATE_CLASS
        End Structure
    
        Public Enum WTS_CONNECTSTATE_CLASS
            WTSActive
            WTSConnected
            WTSConnectQuery
            WTSShadow
            WTSDisconnected
            WTSIdle
            WTSListen
            WTSReset
            WTSDown
            WTSInit
        End Enum
    
    0 comments No comments

  2. Viorel 112.1K Reputation points
    2021-10-20T16:18:31.987+00:00

    What command did you try? The next example seems to work:

    Process.Start("msg", "* /server:Computer123 Hello!")
    

    Replace "Computer123" with the right name of the target computer or server.

    0 comments No comments