Visual Basic Code Example: Locating a Queue

 

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 an application-defined function that searches for all public queues with the label "Test Queue." This example uses the following Message Queuing COM components.

  • MSMQQuery: Its MSMQQuery.LookupQueue method creates, initializes, and returns an MSMQQueueInfos object that defines a query for locating a collection of public queues based on any of the following queue properties: queue identifier, service type, label, creation time, modification time, and multicast address. In this example, the query is based only on the label.

  • MSMQQueueInfos: Represents a query and exposes methods that query the directory service and return an MSMQQueueInfo object for each queue found in the query.

  • MSMQQueueInfo: Represents a public queue that matches the search criteria of the query.

Note

When using COM components to perform a query, the query results contain all the properties of the queues found that are stored in an MSMQQueueInfo object.

To run a query based on the label of the queue

  1. Create an MSMQQuery object and declare MSMQQueueInfos and MSMQQueueInfo objects.

  2. Call MSMQQuery.LookupQueue to define the query and obtain an MSMQQueueInfos object. In this case, the search criteria are specified by setting Label to "Test Queue."

  3. Call MSMQQueueInfos.Next to start the query and return the first queue in the collection of public queues satisfying the search criteria of the query.

  4. In a loop structure, display the path name of the queue returned in the previous call to Next and then call MSMQQueueInfos.Next again to continue returning the remaining queues and displaying their path names.

Code Example

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

Dim query As New MSMQQuery  
Dim qinfos As MSMQQueueInfos  
Dim qinfoCurrent As MSMQQueueInfo  
Set qinfos = query.LookupQueue ( _  
    Label:="Test Queue" )  
Set qinfoCurrent = qinfos.Next  
  
' Display the path names of all queues  
' with the label "Test Queue".  
While Not qinfoCurrent Is Nothing  
    MsgBox qinfoCurrent.PathName  
    Set qinfoCurrent = qinfos.Next  
Wend