Compartir a través de


Visual Basic Code Example: Requesting Acknowledgment Messages

 

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 Sub procedure that requests acknowledgment messages based on a known acknowledgment level and an existing administration queue. This procedure sets the MSMQMessage.Ack and MSMQMessage.AdminQueueInfo properties of the message using the acknowledgment level and administration queue provided by the caller.

For information on how acknowledgment messages are returned, see Acknowledgment Messages.

To request acknowledgments

  1. Declare the objects needed to send a message that requests acknowledgment messages. The procedure declares the following objects:

  2. Generate a direct format name for the destination queue and the path name for the administration queue using the computer and queue names supplied by the caller.

  3. Set the MSMQDestination.FormatName property for the destination queue object, set the MSMQQueueInfo.PathName property of the administration queue object, and refresh its other properties.

  4. Set required message properties. This procedure sets the MSMQMessage.AdminQueueInfo and MSMQMessage.Ack properties of the message. Note that if the message is being sent over HTTP/HTTPS transport, you must set the AdminQueueInfo property to an MSMQQueueInfo object initialized by setting its MSMQQueueInfo.FormatName property to an HTTP direct format name.

  5. Optional. Set additional message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message: Ack".

  6. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the destination queue format name. Note that when using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access and then closes the destination queue when the object is released.

Code Example

The following code example requires MSMQ 3.0.

Sub RequestAcknowledgment( _  
                          strComputerName As String, _  
                          strDestQueueName As String, _  
                          strAdminQueueName As String, _  
                          lAckLevel As Long _  
                          )  
  
  ' Declare Message Queuing objects and strings.  
  Dim dest As New MSMQDestination  
  Dim qinfoAdmin As New MSMQQueueInfo  
  Dim msg As New MSMQMessage  
  Dim strDestFormatName, strAdminPathName As String  
  
  ' Create a direct format name of the destination queue and   
  ' the path name of the administration queue.  
  strDestFormatName = "DIRECT=OS:" & strComputerName & "\" & strDestQueueName  
  strAdminPathName = strComputerName & "\" & strAdminQueueName  
  
  ' Set the format name of the MSMQDestination object for the   
  ' destination queue and the path name of the MQMQQueueInfo object  
  ' for the administration queue.  
  On Error GoTo ErrorHandler  
  dest.FormatName = strDestFormatName  
  qinfoAdmin.PathName = strAdminPathName  
  qinfoAdmin.Refresh  
  
  ' Set the administration queue for the message.  
  msg.Ack = lAckLevel  
  Set msg.AdminQueueInfo = qinfoAdmin  
  
  ' Set other message properties.  
  msg.Label = "Test Message: Ack"  
  
  ' Send the message.  
  msg.Send DestinationQueue:=dest  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub