Visual Basic Code Example: Reading Messages in the Dead-Letter Queue

This example provides a Sub procedure that synchronously reads all the messages in the nontransactional dead-letter queue of a known computer.

This example receives the computer name and generates the machine format name needed to open the dead-letter queue for reading messages from the computer GUID obtained by calling the MSMQApplication.MachineIdOfMachineName method. This method cannot be used if there is no connection to the directory service. This restriction applies to dependent clients, independent clients that are working offline, and Message Queuing routing servers.

For information on when Message Queuing sends messages to the dead-letter queue, see Source Journaling.

In MSMQ 2.0 and later, this procedure can be modified to generate the direct format name of the dead-letter queue instead of retrieving its machine format name.

To read message in a dead-letter queue

  1. Declare the objects needed to retrieve the message.

ms706021.note(en-us,VS.85).gifNote
When declaring the MSMQMessage object when reading messages, the New keyword cannot be used.

  1. Obtain the identifier of the computer provided by the caller. This identifier is needed to create a format name for the dead-letter.

  2. Set the MSMQQueueInfo.FormatName property. The syntax for referencing dead-letter queue is as follows:

    MACHINE=MachineGUID;DEADLETTER
    
    In MSMQ 2.0 and later, the direct format name of the dead-letter queue can be generated instead of using the machine format name. This step is then replaced by the following code:

    MSMQQueueInfo.FormatName = "DIRECT=OS:" & wszComputerName & "\SYSTEM$;DEADLETTER"
    
  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. Using a loop, call MSMQQueue.Receive to read each messages in the dead-letter.

  5. 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.

Sub ReadingComputerJournal( _
                           strComputerName As String _
                           )
  Dim qinfo As New MSMQQueueInfo
  Dim q As MSMQQueue
  Dim msg As MSMQMessage
  Dim strMachineId As String

  ' Obtain the computer GUID for the format name.
  On Error GoTo ErrorHandler
  strMachineId = MachineIdOfMachineName(strComputerName)

  ' Set the format name of the dead-letter queue.
  qinfo.FormatName = "MACHINE=" & strMachineId & ";DEADLETTER"

  ' Open the dead-letter queue.
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _
                         ShareMode:=MQ_DENY_NONE)
                     
  ' Read the first message in the dead-letter queue.
  Set msg = q.Receive(ReceiveTimeout:=1000)
  
  ' Read the remaining messages in the dead-letter queue.
  Do While Not msg Is Nothing
    MsgBox "A message was removed from the dead-letter queue."
    Set msg = q.Receive(ReceiveTimeout:=1000)
  Loop
  
  MsgBox "There are no more messages. The dead-letter queue will be closed."
  q.Close
  Exit Sub

  ErrorHandler:
    MsgBox "Error " + Hex(Err.Number) + " was returned." _
           + Chr(13) + Err.Description
    If Not q Is Nothing And q.IsOpen2 Then
      q.Close
    EndIf
End Sub
Show:
© 2014 Microsoft