다음을 통해 공유


Visual Basic Code Example: Sending Messages Using Multicast Addresses

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a private Sub procedure that sends a message to all destination queues associated with a multicast address provided by the caller.

For information on sending messages, see Sending Messages.

To send a message using a multicast address

  1. Declare the objects needed to send the message. This procedure declares the following objects:

  2. Generate a multicast address format name.

  3. Set the MSMQDestination.FormatName property.

  4. Set message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message".

  5. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name.

    Note

    When using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access.

Code Example

The following code example requires MSMQ 3.0.

Sub SendMessageMulticast( _  
                         strAddress As String _  
                         )  
  Dim strFormatName As String  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create a direct format name.  
  strFormatName = "MULTICAST=" & strAddress  
  
  ' Set the format name of the MSMQDestination object.  
  On Error GoTo ErrorHandler  
  dest.FormatName = strFormatName  
  
  ' Set the message label.  
  msg.Label = "Test Message"  
  
  ' Send the message and close the MSMQDestination object.  
  msg.Send DestinationQueue:=dest  
  dest.close  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub