Export (0) Print
Expand All

Visual Basic Code Example: Reading Messages By Lookup Identifier

The following example provides a Sub procedure that retrieves a specific message from a known queue. This function could be implemented within a DLL that is invoked by a Message Queuing trigger associated with a queue.

For information about Message Queuing triggers, see Message Queuing Triggers.

Lookup identifiers can be used only for synchronous reading. Asynchronous reading requires the use of cursors.

For information about cursor movement, see Cursors and COM Components.

To retrieve a message by its lookup identifier

  1. Declare the objects needed to retrieve the message. This procedure declares the following Message Queuing objects (note that when declaring the MSMQMessage object when reading messages, the New keyword cannot be used).

    MSMQQueueInfo

    MSMQQueue

    MSMQMessage

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

    Because this procedure sets the MSMQQueueInfo.PathName property of the MSMQQueueInfo object, Message Queuing must obtain the format name of the queue before opening the queue. The format name of a public queue must be retrieved from the directory service, and the format name of a local private queue can be obtained from information stored on the local computer. However, a remote private queue cannot be opened unless the MSMQQueueInfo.FormatName property is set with a direct format name. This procedure can be modified to receive the format name from the caller or to generate a direct format name. The applicable format name can then be used to set the FormatName property. For more information, see Format Names.

  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.ReceiveByLookupId using the lookup identifier provided by the caller.

  5. Call MSMQQueue.Close to release resources used to open the queue and then exit the Sub procedure.

Code Example

The following code example requires MSMQ 3.0.

Sub ReadByLookupId( _
                   strComputerName As String, _
                   strQueueName As String, _
                   varMsgLookupId As Variant _
                   )
  Dim qinfo As New MSMQQueueInfo
  Dim q As MSMQQueue
  Dim msg As MSMQMessage

 ' Set the path name in the MSMQQueueInfo object.
  On Error GoTo ErrorHandler
  qinfo.PathName = strComputerName & "\" & strQueueName
  qinfo.Refresh

  ' Open the queue.
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _
                         ShareMode:=MQ_DENY_NONE)
                     
  ' Read message using lookup identifier.
  Set msg = q.ReceiveByLookupId(LookupId:=varMsgLookupId)
  MsgBox "The message was removed from the queue."

  ' Close the queue.
  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:
© 2015 Microsoft