This function allows you to read messages in the queue. When reading messages, you can either peek at (not removing them) or retrieve the messages (removing them) in the queue.

Messages can be read either synchronously, asynchronously, or through a transaction.

DWORD dwTimeout,
DWORD dwAction,
MQMSGPROPS pMessageProps,
LPOVERLAPPED lpOverlapped,
HANDLE hCursor,
Transaction *pTransaction


  • hSource
    [in] Handle to the queue that contains the message. For transactions, specify a queue on a local computer.

  • dwTimeout
    [in] Time, in milliseconds, to wait for the message. Can be set to INFINITE.

  • dwAction
    [in] How the message is read in the queue. Specify one of the values listed in the following table.

    Value Description
    MQ_ACTION_RECEIVE Reads the message at the current cursor location and removes it from the queue.
    MQ_ACTION_PEEK_CURRENT Reads the message at the current cursor location but does not remove it from the queue. The cursor remains pointing at the current message.

    If a cursor was not created by MQCreateCursor (hCursor is NULL), the queue's cursor can only point to the first message in the queue.

    MQ_ACTION_PEEK_NEXT Reads the next message in the queue, skipping the message at the current cursor location, but does not remove it from the queue.

    You must call MQCreateCursor (hCursor is not NULL) before you can use MQ_ACTION_PEEK_NEXT.

  • pMessageProps
    [in/out] On input, a pointer to an MQMSGPROPS structure that specifies which message properties will be received. Can be set to NULL.

    On output, it contains the received message property values.

  • lpOverlapped
    [in/out] Pointer to an overlapped structure. Set to NULL for synchronous receive and transactions.

  • fnReceiveCallback
    [in] Pointer to the callback function. Set to NULL for synchronous receive and transactions.

  • hCursor
    [in] Handle to a cursor for looking at messages in the queue. Can be set to NULL. See the following Remarks section.

  • pTransaction
    [in] Not supported; set to NULL.

Return Values

  • MQ_OK
    Indicates success.
    The action specified in dwAction does not agree with the access rights the queue was opened with.
    The supplied buffer for the message body is too small. The part of the message body that fits into the buffer is copied, but the message is not removed from the queue.
    Windows CE does not support encryption. If specified, the follow properties return this error: PROPID_M_HASH_ALG, PROPID_M_ENCRYPTION_ALG**,** PROPID_M_PROV_TYPE**,** PROPID_M_SECURITY_CONTEXT. Other encryption properties return NULL data.
    The supplied sender identification buffer is too small to hold the sender identification.
    The supplied symmetric key buffer is too small to hold the symmetric key.
    The supplied sender certificate buffer is too small to hold the security certificate.
    The supplied signature buffer is too small to hold the message's digital signature.
    The supplied provider name buffer is too small to hold the cryptographic service provider's name.
    The supplied message label buffer is too small to hold the message's label.
    The supplied format name buffer is too small to hold the format name of the queue.
    MSMQ was unable to connect to the Microsoft Distributed Transaction Coordinator (DTC).
    One of the following message properties was specified (in pMessageProps) without its associated length property: PROPID_M_ADMIN_QUEUE, PROPID_M_DEST_QUEUE, PROPID_M_LABEL, PROPID_M_RESP_QUEUE.
    The queue handle specified in hSource is not valid.
    No message was received within the time-out period specified by dwTimeout.
    A message that is currently pointed at by the cursor has been removed from the queue. It can be removed by another process or by another call to MQReceiveMessage using a different cursor, or the message time-to-be-received timer has expired.
    The operation was canceled before it could be completed. For example, the queue handle was closed by another thread while waiting for a message.
    One or more message properties specified in pMessageProps resulted in an error.
    The queue was deleted before the message could be read. The specified queue handle is no longer valid and the queue handle must be closed.
    MQ_ACTION_PEEK_NEXT cannot be used with the current cursor position.
    Cannot connect to the Queue Manager.
    The specified queue handle was obtained in a previous session of the Queue Manager service. Close the queue and open it again to obtain a fresh handle.
    Transaction error. An attempt was made to open a remote queue for read access from within a transaction, or an attempt was made to read a message from a nontransactional queue from within a transaction.
    One or more of the properties specified in pMessageProps resulted in a warning code even though the function is completed.


All message properties can be read. However, only those properties specified in the pMessageProps parameter are returned to the calling application; other properties are simply discarded when the message is read. For example, you may choose to retrieve the size of the message without retrieving the message body itself.

The hCursor parameter contains the handle to a cursor created by MQCreateCursor. Using a cursor is optional and is only necessary when you want to read messages that are not at the front of the queue. When using a cursor, you must peek at the first message in the queue by setting dwAction to MQ_ACTION_PEEK_CURRENT before you make calls with dwAction set to MQ_ACTION_PEEK_NEXT.

To retrieve the message body, you must specify PROPID_M_BODY in pMessageProps. The VT field of the corresponding element in the aPropVar array should be set to VT_UI1 | VT_VECTOR, allowing MSMQ to use the buffer specified in the CAUI1 structure to store the message. If the supplied buffer is too small to contain the entire message body, MQReceiveMessage fails and MQ_ERROR_BUFFER_OVERFLOW is returned. The buffer is filled to capacity, and the full message remains in the queue. When this happens, the other properties specified by pMessageProps are still read.

To retrieve the size of the message, specify PROPID_M_BODY_SIZE in pMessageProps. MSMQ sets PROPID_M_BODY_SIZE to the size of the message body, even if MQReceiveMessage fails because the message body exceeded the buffer allocated by PROPID_M_BODY. When retrieving the message body size, the CAUI1 structure associated with the PROPID_M_BODY property does not indicate the size. The cElems field of the CAUI1 structure merely indicates the maximum message body, which could be copied into the pElems buffer. The cElems field is never modified by MSMQ.

Not all properties require the application to specify the property type in the VT field of the aPropVar array. For these properties, the corresponding VT field in the aPropVar array can be set to VT_NULL.

When reading messages in a queue, the function's time-out timer (dwTimeout) can be set to 0, a specific amount of time, or INFINITE. A message can be retrieved if it is available at that period of time.

Messages cannot be read from a queue that resides on a computer without a direct connection. Opening a queue with receive or peek access requires a direct connection to the computer where the queue resides.

Synchronously Reading Messages

To synchronously read messages, set fnReceiveCallback and lpOverlapped to NULL. When this is done, the calling thread is blocked until a suitable message is available or a time-out occurs.

Asynchronously Reading Messages

When asynchronously reading messages, MQReceiveMessage returns a SUCCESS value if a suitable message is found. Otherwise, the function returns immediately with the return value MQ_INFORMATION_OPERATION_PENDING. This return value indicates that the operation is pending and will be completed as soon as a suitable message can be found. Asynchronous receive is based on standard Microsoft® Win32® mechanisms.

Applications can use a callback function, a Windows Event mechanism, or a completion port to read messages asynchronously. When using a callback function, you can register only 64 callbacks at the same time.

Completion ports cannot be used by dependent client applications. On MSMQ independent clients and servers, queue handles are implemented as file handles and so, can be associated with completion ports. However, on dependent clients, queue handles are not file handles and as a result, cannot be used with completion ports.

The output parameters to an asynchronous call to MQReceiveMessage should be kept intact until the operation is complete (that is, you cannot free them or reuse them). Use automatic variables with caution.

Messages in Administration Queues

When reading acknowledgment messages in an administration queue, you can see if the original message failed to reach its destination or was not retrieved from the queue by looking at the class property (PROPID_M_CLASS) of the acknowledgment message. The class property will contain a positive or negative acknowledgment, depending on the acknowledgment level specified by the original message.

If the class property is positive, the original message body is not included in the acknowledgment message. If the class property is negative, the message body is included as the message body of the acknowledgment message.

Responding to Messages

The receiving application can determine if the sending application expects a response by retrieving the PROPID_M_RESP_QUEUE and PROPID_M_RESP_QUEUE_LEN properties when reading a message. PROPID_M_RESP_QUEUE contains the format name of a response queue if a response is requested.

When opening a response queue to send several response messages, the receiving application can cache the queue handle returned by MQOpenQueue, thus eliminating the need to call MQOpenQueue several times for the same response queue.


Runs on Versions Defined in Include Link to
Windows CE OS 2.0 and later. Versions prior to 2.12 require the MSMQ add-on pack. Mq.h Mq.h Msmqrt.lib

Note   This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.

See Also


 Last updated on Tuesday, July 13, 2004

© 1992-2000 Microsoft Corporation. All rights reserved.