Visual Basic Network Services
NetMessageBufferSend: Broadcasting System Messages
     
Posted:   Monday January 24, 2000
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   Windows NT4, Windows 2000 or Windows XP as the message originator
Author:   VBnet - Randy Birch
     

Related:  

WriteFile: Broadcasting System Messages using Mailslots
     
 Prerequisites
Operating system listed above.

Sending a message from one machine to another, with the message source originating on a NT system, can be easily done using the NetMessageBufferSend API.  This is essentially the same as using the DOS command NET SEND.  The receiving system will display the message appropriately; when sent to another NT system, the Net Message dialog will pop on screen. When sending to a Windows9x system, the message will be delivered through the WinPopup utility (which must be running).

By varying the SendTo, SendFrom and Server members of the call, the alert can be broadcast to specific users or to your own system.

 BAS Module Code
None.

 Form Code
Start a new project with four text boxes (Text1-Text4 (Text4 can be multiline)), a command button (Command1) and a label (Label1) to display the returned message. Other ID labels for the form are optional. Add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'from LMMSG.H
Private Const ERROR_ACCESS_DENIED As Long = 5
Private Const ERROR_BAD_NETPATH As Long = 53
Private Const ERROR_INVALID_PARAMETER As Long = 87
Private Const ERROR_NOT_SUPPORTED As Long = 50
Private Const ERROR_INVALID_NAME As Long = 123
Private Const NERR_BASE As Long = 2100
Private Const NERR_SUCCESS As Long = 0                     
Private Const NERR_NetworkError As Long = (NERR_BASE + 36) 
Private Const NERR_NameNotFound As Long = (NERR_BASE + 173)
Private Const NERR_UseNotFound As Long = (NERR_BASE + 150) 

Private Const MAX_COMPUTERNAME As Long = 15
Private Const VER_PLATFORM_WIN32s As Long = 0
Private Const VER_PLATFORM_WIN32_WINDOWS As Long = 1
Private Const VER_PLATFORM_WIN32_NT As Long = 2

Private Type OSVERSIONINFO
  OSVSize         As Long
  dwVerMajor      As Long
  dwVerMinor      As Long
  dwBuildNumber   As Long
  PlatformID      As Long
  szCSDVersion    As String * 128 
End Type

'User-defined type for passing
'the data to the Send function
Private Type NetMessageData
   sServerName As String
   sSendTo As String
   sSendFrom As String
   sMessage As String
End Type

'NetMessageBufferSend parameters:
'servername:  Unicode string specifying the name of the
'             remote server on which the function is to
'             execute. If this parameter is vbNullString,
'             the local computer is used. 
'
'msgname:     Unicode string specifying the message alias to
'             which the message buffer should be sent. 
'
'fromname:    Unicode string specifying who the message is from. 
'             This parameter is required to send interrupting messages
'             from the computer name. If this parameter is NULL, the
'             message is sent from the logged-on user.
'
'msgbuf:      Unicode string containing the message to send.
'
'msgbuflen:   value that contains the length, in bytes, of
'             the message text pointed to by the msgbuf parameter.
Private Declare Function NetMessageBufferSend Lib "netapi32" _
  (ByVal servername As String, _
   ByVal msgname As String, _
   ByVal fromname As String, _
   ByVal msgbuf As String, _
   ByRef msgbuflen As Long) As Long

Private Declare Function GetComputerName Lib "kernel32" _
   Alias "GetComputerNameA" _
  (ByVal lpBuffer As String, _
   nSize As Long) As Long

Private Declare Function GetVersionEx Lib "kernel32" _
   Alias "GetVersionExA" _
  (lpVersionInformation As OSVERSIONINFO) As Long


Private Sub Form_Load()

   Dim tmp As String
   
  'pre-load the text boxes with
  'the local computer name for testing
   tmp = Space$(MAX_COMPUTERNAME + 1)
   Call GetComputerName(tmp, Len(tmp))
   
   Text1.Text = TrimNull(tmp)
   Text2.Text = TrimNull(tmp)
   Text3.Text = TrimNull(tmp)
   
End Sub


Private Sub Command1_Click()

   Dim MsgData As NetMessageData
   Dim sSuccess As String
   
   With MsgData
      .sServerName = Text1.Text
      .sSendTo = Text2.Text
      .sSendFrom = Text3.Text
      .sMessage = Text4.Text
   End With
   
    sSuccess = NetSendMessage(MsgData)
    
    Label1.Caption = sSuccess
    
End Sub


Private Function IsWinNT() As Boolean

  'returns True if running WinNT/Win2000/WinXP
   #If Win32 Then
  
      Dim OSV As OSVERSIONINFO
   
      OSV.OSVSize = Len(OSV)
   
      If GetVersionEx(OSV) = 1 Then
   
        'PlatformId contains a value representing the OS.
         IsWinNT = (OSV.PlatformID = VER_PLATFORM_WIN32_NT)
         
      End If

   #End If

End Function


Private Function NetSendMessage(msgData As NetMessageData) As String

   Dim success As Long
   
  'assure that the OS is NT ..
  'NetMessageBufferSend  can not
  'be called on Win9x
   If IsWinNT() Then
      
      With msgData
      
        'if To name omitted return error and exit
         If .sSendTo = "" Then
            
            NetSendMessage = GetNetSendMessageStatus(ERROR_INVALID_PARAMETER)
            Exit Function
            
         Else
       
           'if there is a message
            If Len(.sMessage) Then
   
              'convert the strings to unicode
               .sSendTo = StrConv(.sSendTo, vbUnicode)
               .sMessage = StrConv(.sMessage, vbUnicode)
            
              'Note that the API could be called passing
              'vbNullString as the SendFrom and sServerName
              'strings. This would generate the message on
              'the sending machine.
               If Len(.sServerName) > 0 Then
                  .sServerName = StrConv(.sServerName, vbUnicode)
               Else
                  .sServerName = vbNullString
               End If
                        
               If Len(.sSendFrom) > 0 Then
                  .sSendFrom = StrConv(.sSendFrom, vbUnicode)
               Else
                  .sSendFrom = vbNullString
               End If
            
              'change the cursor and show. Control won't return
              'until the call has completed.
               Screen.MousePointer = vbHourglass
           
               success = NetMessageBufferSend(.sServerName, _
                                              .sSendTo, _
                                              .sSendFrom, _
                                              .sMessage, _
                                              ByVal Len(.sMessage))
           
               Screen.MousePointer = vbNormal
           
               NetSendMessage = GetNetSendMessageStatus(success)
   
            End If 'If Len(.sMessage)
         End If  'If .sSendTo
      End With  'With msgData
   End If  'If IsWinNT
   
End Function


Private Function GetNetSendMessageStatus(nError As Long) As String
    
   Dim msg As String
   
   Select Case nError
   
     Case NERR_SUCCESS:            msg = "The message was successfully sent"
     Case NERR_NameNotFound:       msg = "Send To not found"
     Case NERR_NetworkError:       msg = "General network error occurred"
     Case NERR_UseNotFound:        msg = "Network connection not found"
     Case ERROR_ACCESS_DENIED:     msg = "Access to computer denied"
     Case ERROR_BAD_NETPATH:       msg = "Sent From server name not found."
     Case ERROR_INVALID_PARAMETER: msg = "Invalid parameter(s) specified."
     Case ERROR_NOT_SUPPORTED:     msg = "Network request not supported."
     Case ERROR_INVALID_NAME:      msg = "Illegal character or malformed name."
     Case Else:                    msg = "Unknown error executing command."
     
   End Select
   
   GetNetSendMessageStatus = msg
   
End Function
         

Private Function TrimNull(item As String)

  'return string before the terminating null
   Dim pos As Integer
   
   pos = InStr(item, Chr$(0))
   
   If pos Then
      TrimNull = Left$(item, pos - 1)
   Else
      TrimNull = item
   End If
   
End Function
 Comments
The code will populate Text1, 2 and 3 with the current machine name. Type a message into Text4 and hit Send. The alert will appear on-screen. By specifying another networked machine as the Server and SendTo names, the alert will appear at that computer. Remember that if you are specifying a Win9x as the target system, WinPopup must be already running on that system to receive the alert -- this API will not launch it.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter