Partilhar via


Visual Basic Code Example: Sending Messages to a Destination Queue Using a Destination Object

 

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 a single destination using direct messaging. The input parameters for this procedure include the computer name and queue name needed to generate the direct format name.

For information on sending messages, see Sending Messages.

To send a message to a destination queue

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

  2. Generate a direct format name using the computer name and queue name provided by the caller.

  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 MSMQDestinationMSMQDestination object, Message Queuing implicitly opens the destination queue with send access.

Code Example

The following code example requires MSMQ 3.0.

Sub SendMessage( _  
                strComputerName As String, _  
                strQueueName As String _  
                )  
  Dim strFormatName As String  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create a direct format name.  
  strFormatName = "DIRECT=OS:" & strComputerName & "\" & strQueueName  
  
  ' Set the format name of the MSMQDestination object.  
  On Error GoTo ErrorHandler  
  dest.FormatName = strFormatName  
  
  ' Set message properties.  
  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