Visual Basic Code Example: Opening a Queue
This example provides a routine that opens a queue for sending, peeking at, or receiving messages. The queue is opened using a direct format name created from the computer name and queue name supplied by the caller.
|When sending messages, use the MSMQDestination object (introduced in MSMQ 3.0) to send messages. The destination object does not require the application to explicitly open the queue. Your application only sends the message and the queue is opened by Message Queuing. For an example of sending messages using an MSMQDestination object, see Visual Basic Code Example: Sending Messages to a Destination Queue Using a Destination Object.|
The initial properties of the opened queue are based on the properties of the MSMQQueueInfo object used to open the queue. You can always obtain these initial settings, even if the open queue's properties are changed, by looking at the queue's MSMQQueue.QueueInfo property.
|Remote queues can be opened to send messages while offline. However, to provide a format name your application must obtain and cache the information needed for the format name while operating online.|
The following procedure lists the steps the function uses to open the queue.
To open a queue
Declare an MSMQQueueInfo object.
Obtain an MSMQQueueInfo object. This example sets the MSMQQueueInfo.FormatName property using the computer and queue name provided by the caller.
Call MSMQQueueInfo.Open using the access mode and share mode provided by the caller.
Add code for sending, peeking at, or receiving messages based on the access mode provided by the caller.
Close the queue.
The following code example contains no version-specific Message Queuing calls.
Sub OpenQueueInfo( _ strComputerName As String, _ strQueueName As String, _ lAccessMode As Long, _ lShareMode As Long _ ) Dim qinfo As New MSMQQueueInfo Dim q As MSMQQueue ' Set the format name in the MSMQQueueInfo object. On Error GoTo ErrorHandler qinfo.formatname = "DIRECT=OS:" & strComputerName & "\" & strQueueName ' Open the queue. Set q = qinfo.Open(Access:=lAccessMode, ShareMode:=lShareMode) MsgBox "The queue is open." ' 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