Reading Messages Synchronously

When reading messages synchronously, all calls are blocked until the next message is available or timeout occurs.

    To read a message synchronously
  1. Open the queue with receive or peek access.
    hr = MQOpenQueue(
        szwFormatNameBuffer,    // Format name of the queue.
        MQ_RECEIVE_ACCESS,      // Access rights to the Queue.
        0,                      // No receive Exclusive.
        &hQueue                 // OUT: handle to the opened Queue.
    );
     
  2. Specify the message properties to be retrieved. For example, if you only need to look at the body of the message, only specify PROPID_M_BODY.
    //Set the message body property.
    aPropId[PropIdCount] = PROPID_M_BODY;              //PropId
    aVariant[PropIdCount].vt = VT_VECTOR|VT_UI1;       //Type
    aVariant[PropIdCount].caub.cElems = MSG_BODY_LEN;  //Buffer size
    aVariant[PropIdCount].caub.pElems = ucMsgBody;     //Buffer
     
        PropIdCount++;
     
  3. Set the MQMSGPROPS structure.
    // Set the MQMSGPROPS structure.
    MsgProps.cProp = PropIdCount;       //Number of properties.
    MsgProps.aPropID = aPropId;         //Ids of properties.
    MsgProps.aPropVar = aVariant;       //Values of properties.
    MsgProps.aStatus  = NULL;           //No Error report.
     
  4. Retrieve the message from the queue.
    hr = MQReceiveMessage(
         hQueue,              // Handle to the Queue.
         5 * 60 * 1000,       // Timeout value (msec) to wait for
                             // messages (5*60*1000=5 min.).
         MQ_ACTION_RECEIVE,   // Action.
         &MsgProps,           // Properties to retrieve.
         NULL,                // Must be NULL for synchronous receive.
         NULL,                // Must be NULL for synchronous receive.
         NULL,                // No Cursor.
         MQ_NO_TRANSACTION    // No transaction.
                         );
     

Example

The following example opens a queue with receive access, specifies the body of the message as the only property to be retrieved, and then reads the first message in the queue as a non-transactional, synchronous operation.

HRESULT hr

//Open Queue
WCHAR * szwFormatNameBuffer;  // Format Name of the queue to be opened
QUEUEHANDLE hQueue;

// Obtain format name of queue.
 
hr = MQOpenQueue(
    szwFormatNameBuffer,    // Format Name of the queue to be opened.
    MQ_RECEIVE_ACCESS,      // Access rights to the Queue.
    0,                      // No receive Exclusive.
    &hQueue                 // OUT: handle to the opened Queue.
                );
 
if (FAILED(hr))
{
 // Error handler for MQOpenQueue.
}
 
 
MQMSGPROPS MsgProps;
MQPROPVARIANT aVariant[10];
MSGPROPID aPropId[10];
DWORD PropIdCount = 0;
 
//
// Prepare the message properties to be retrieved.
//
 
#define MSG_BODY_LEN 500
unsigned char ucMsgBody[MSG_BODY_LEN];
 
// Set the PROPID_M_BODY property.
aPropId[PropIdCount] = PROPID_M_BODY;              //PropId
aVariant[PropIdCount].vt = VT_VECTOR|VT_UI1;       //Type
aVariant[PropIdCount].caub.cElems = MSG_BODY_LEN;  //Buffer size.
aVariant[PropIdCount].caub.pElems = ucMsgBody;     //Buffer
 
PropIdCount++;
 
//
// Set other properties.
//
 
// Set the MQMSGPROPS structure
MsgProps.cProp = PropIdCount;       //Number of properties.
MsgProps.aPropID = aPropId;         //Ids of properties.
MsgProps.aPropVar = aVariant;       //Values of properties.
MsgProps.aStatus  = NULL;           //No Error report.
 
//
// Retrieve the message.
//
hr = MQReceiveMessage(
    hQueue,              // handle to the Queue.
  5 * 60 * 1000,      // Timeout value (msec) to wait for
                      // for messages (5*60*1000=5 min.).
    MQ_ACTION_RECEIVE,   // Action.
    &MsgProps,           // properties to retrieve.
    NULL,                // No overlapped structure.
    NULL,                // No callback function.
    NULL,                // No Cursor.
    MQ_NO_TRANSACTION    // No transaction
                     );
 
 
if (FAILED(hr))
    {
    //  Error handler for MQReceiveMessage.
    }