Opening a Queue

Queues can be opened for sending messages to the queue, retrieving messages from the queue, or peeking at the messages in the queue without removing them. All queues, both public and private, are opened by calling MQOpenQueue.

MQOpenQueue returns a queue handle that is used to:

When opening a queue, the application specifies the access mode and share mode of the queue. The queue's access mode indicates if the application is going to send messages to the queue, peek at the messages in the queue, or retrieve messages from the queue. The queue's share mode indicates who else can use the queue while the application is using the queue.

Before opening a queue, MSMQ verifies that the access mode requested by the application is not restricted by the access rights of the queue. For example, a queue may restrict those who can send messages to it. For a discussion of queue access rights, see Access Control.

To open a queue
  1. Obtain the format name of the queue. If the format name of the queue is not known, you can obtain a format name by using one of the following format name translation functions:

    MQInstanceToFormatName,

    MQPathNameToFormatName.

  2. Set the queue's access mode. Are messages going to be sent to the queue (dwAccess = MQ_SEND_ACCESS), retrieved from the queue(dwAccess = MQ_RECEIVE_ACCESS), or peeked at without removing them from the queue (dwAccess = MQ_PEEK_ACCESS)?

    When a queue is opened with receive access, the application can also peek at the queue's messages. However, the reverse is not true. When a queue is opened with peek access, the application cannot retrieve a message from the queue.

    DWORD dwAccess = MQ_RECEIVE_ACCESS;
     
  3. Set the queue's share mode. If messages are going to be retrieved from the queue (dwAccess = MQ_RECEIVE_ACCESS), determine if the application should stop others from retrieving messages at the same time it is retrieving messages (dwShareMode = MQ_DENY_RECEIVE_SHARE). Using this setting does not stop other applications from peeking at the messages in the queue, it only prevents them from retrieving messages at the same time the calling application is retrieving messages.
    DWORD dwShareMode = MQ_DENY_RECEIVE_SHARE;
        
  4. Call MQOpenQueue.
    hr = MQOpenQueue(
         szwFormatNameBuffer,     // Format Name of queue.
         dwAccess,                // Access mode of queue.
         dwShareMode,             // Exclusive mode of queue.
         &hQueue                  // OUT: Handle to queue.
         );
     

Example

This example opens a queue for reading messages.

////////////////////////
// Set the access mode.
////////////////////////
DWORD dwAccess = MQ_RECEIVE_ACCESS;
 
////////////////////////
// Set share mode.
///////////////////////
DWORD dwShareMode = MQ_DENY_RECEIVE_SHARE;
 
//////////////////////
// Call MQOpenQueue.
/////////////////////
QUEUEHANDLE hQueue;
 
hr = MQOpenQueue(
     szwFormatNameBuffer,     // Format Name of queue.
     dwAccess,                // Access mode of queue.
     dwShareMode,             // Exclusive receive mode.
     &hQueue                  // OUT: Handle to queue.
     );
 

© 1997 by Microsoft Corporation. All rights reserved.