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.
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;
DWORD dwShareMode = MQ_DENY_RECEIVE_SHARE;
hr = MQOpenQueue(
szwFormatNameBuffer, // Format Name of queue.
dwAccess, // Access mode of queue.
dwShareMode, // Exclusive mode of queue.
&hQueue // OUT: Handle to queue.
);
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.
);