Visual Basic Code Example: Verifying Acknowledgment Requests

 

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 peeks at the MSMQMessage.Ack property of all the messages in a known queue, displaying the label of any message that requested acknowledgment messages.

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

To verify acknowledgments

  1. Declare the objects needed to read the messages in the queue. Note that when declaring the MSMQMessage object when reading messages, you cannot use the New keyword.

  2. Obtain an MSMQQueueInfo object. The following example obtains the MSMQQueueInfo object by setting MSMQQueueInfo.FormatName using the computer name and queue name provided by the caller.

  3. Call MSMQQueueInfo.Open to open the queue with receive access. When opening a queue with receive access the application can peek at or retrieve the messages in the queue.

  4. Call MSMQQueue.PeekCurrent to initialize the cursor. This call points the cursor to the first message in the queue.

  5. Using a loop, peek at the acknowledgment level of each message in the queue. This example displays the label of all messages that were sent with some type of acknowledgments requested.

  6. When there are no messages left, call MSMQQueue.Close to release resources used to open queue and exit the Sub procedure.

Code Example

The following code example can be run on all versions of Message Queuing.

Private Sub VerifyingAcknowledgment( _  
                                    strQueueName As String, _  
                                    strComputerName As String _  
                                    )  
  
  ' Declare variables.  
  Dim strFormatName As String  
  Dim msg As MSMQMessage  
  Dim q As MSMQQueue  
  Dim qinfo As New MSMQQueueInfo  
  
  ' Create a direct format name.  
  strFormatName = "DIRECT=OS:" & strComputerName & "\" & strQueueName  
  
  ' Set the format name of the MSMQQueueInfo object.  
  On Error GoTo ErrorHandler  
  qinfo.FormatName = strFormatName  
  
  ' Open the queue with receive access.  
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, ShareMode:=MQ_DENY_NONE)  
  
  ' Peek at all the messages in the queue using cursors.  
  Set msg = q.PeekCurrent(ReceiveTimeout:=1000)  
  Do While Not msg Is Nothing  
    ' Display the labels of messages that requested acknowledgment.  
    If Not msg.Ack = MQMSG_ACKNOWLEDGMENT_NONE Then  
      MsgBox "Message: " & msg.Label, , "requested acknowledgment"  
    End If  
    Set msg = q.PeekNext(ReceiveTimeout:=1000)  
  Loop  
  
  ' Close the queue.  
  q.Close  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub