Introduced in MSMQ 3.0, the MQReceiveMessageByLookupId function reads a specific message in the queue or subqueue. Messages can be referenced by a lookup identifier or by their position at the front or end of the queue.
Messages can be read asynchronously, synchronously, or through a transaction.
HRESULT APIENTRY MQReceiveMessageByLookupId( QUEUEHANDLE hSource, ULONGLONG ullLookupId, DWORD dwLookupAction, MQMSGPROPS * pMessageProps, LPOVERLAPPED lpOverlapped, PMQRECEIVECALLBACK fnReceiveCallback, ITransaction * pTransaction );
[in] A handle to the queue that contains the message. For transactions, specify a queue on a local computer.
[in] The lookup identifier of a message or 0 ("0" is used when accessing the first or last message in the queue).
[in] How the message is read in the queue. Specify one of the following:
Peeks at the first message in the queue but does not remove it from the queue. The ullLookupId parameter must be set to 0.
Peeks at the message preceding the message specified by ullLookupId but does not remove it from the queue.
Peeks at the message specified by ullLookupId but does not remove it from the queue.
Peeks at the message following the message specified by ullLookupId but does not remove it from the queue.
Peeks at the last message in the queue but does not remove it from the queue. The ullLookupId parameter must be set to 0.
Retrieves the first message in the queue and removes it from the queue. The ullLookupId parameter must be set to 0.
Retrieves the message preceding the message specified by ullLookupId and removes it from the queue.
Retrieves the message specified by ullLookupId and removes it from the queue.
Retrieves the message following the message specified by ullLookupId and removes it from the queue.
Retrieves the last message in the queue and removes it from the queue. The ullLookupId parameter must be set to 0.
[in, out] On input, a pointer to an MQMSGPROPS structure that specifies which message properties will be retrieved. Can be set to NULL.
On output, pMessageProps contains the retrieved message property values.
[in, out] A pointer to an OVERLAPPED structure that can be used in asynchronous receive operations based on a completion port or an event mechanism. Setting this parameter to a valid pointer makes the receive operation asynchronous. Set to NULL for synchronous receive operations and transactions as well as for asynchronous receive operations that do not employ an OVERLAPPED structure.
[in] A pointer to the callback function. Set to NULL for synchronous receive operations and transactions as well as for asynchronous receive operations that do not employ a callback function.
[in] A pointer to a transaction object, a constant, or NULL.
Transaction objects can be obtained internally from Message Queuing (by calling MQBeginTransaction), or externally from Microsoft® Distributed Transaction Coordinator (MS DTC).
Specifies that the call is not part of a transaction.
Instructs Message Queuing to verify that a COM+ object is running and that the current COM+ object is participating in a transaction. If Message Queuing finds that the application is running in the context of a COM+ (Component Services) transaction, the message is retrieved within the current COM+ transaction. Otherwise, the message is retrieved outside of a transaction. For more information, see COM+ Transactions.
Requests a single-message transaction. Note that a request to retrieve a message in a single-message transaction using this constant is equivalent to a request to retrieve a single message from a transactional queue outside of a transaction using MQ_NO_TRANSACTION.
Specifies that the call is part of an externally coordinated, XA-compliant transaction.
NULL indicates the message is not retrieved as part of a transaction.
The access rights of the queue for performing the action specified in dwLookupAction are not allowed for the user that opened the queue.
The buffer supplied for PROPID_M_BODY, PROPID_M_COMPOUND_MESSAGE, PROPID_M_EXTENSION, or PROPID_M_SOAP_ENVELOPE is too small. Details can be retrieved from the aStatus array. In each case, the portion of the property that fits is copied to the buffer, but the message is not removed from the queue.
Message Queuing was unable to connect to the MS DTC.
The supplied format name buffer is too small to hold the format name of the queue.
The supplied message label buffer is too small to hold the message's label.
A property was specified in pMessageProps without its associated length property.
The queue handle specified in hSource is not valid.
The operation was canceled before it could be completed. For example, the queue handle was closed by another thread while waiting for a message.
Receiving or peeking at a message using a lookup identifier from a remote queue residing on a computer running MSMQ 1.0 or MSMQ 2.0 is not supported.
One or more message properties specified in pMessageProps resulted in an error.
The supplied provider name buffer is too small to hold the cryptographic service provider's name.
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.
The message referenced by the lookup identifier does not exist. Either the lookup identifier is incorrect or the message has been removed from the queue.
The supplied sender certificate buffer is too small to hold the user certificate.
The sender identifier buffer supplied is too small.
The Message Queuing service is not available.
The signature buffer supplied is too small to hold the message's digital signature.
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.
The supplied symmetric key buffer is too small to hold the symmetric key.
One of the following actions was attempted within the context of a transaction.
An attempt was made to read a message from a remote queue.
An attempt was made to peek a message inside a transaction.
An attempt was made to read a message from a nontransactional queue.
An attempt was made to read a message using a callback or overlap function.
One or more of the properties specified in pMessageProps resulted in a warning code even though the function is completed.
The MQReceiveMessageByLookupId function is used by the receiving application to peek at or retrieve the messages in the queue. This function can be used to go directly to any message in the queue, and then navigate to the preceding or following messages.
The lookup identifier of a message (PROPID_M_LOOKUPID) is generated by Message Queuing when the message is placed in the queue (this is not the message identifier generated when the message is sent). Lookup identifiers are assigned to all messages that are put in application-generated destination, administration, and report queues, as well as system-generated journal, dead-letter, connector, and outgoing queues. The lookup identifier is unique to the queue and has no meaning outside the queue.
When retrieving messages using MQReceiveMessageByLookupId, there is no receive time-out functionality as provided in MQReceiveMessage. The message must exist when MQReceiveMessageByLookupId is called. If the message does not exist, an MQ_ERROR_MESSAGE_NOT_FOUND error is returned and the call fails.
To peek at or retrieve messages from a local outgoing queue, the corresponding remote destination queue must be opened with administrative access in addition to peek or receive access. Specifically, the handle to the queue (hSource) must be obtained by calling MQOpenQueue with the dwAccess parameter set to MQ_PEEK_ACCESS | MQ_ADMIN_ACCESS or MQ_RECEIVE_ACCESS | MQ_ADMIN_ACCESS. Local administrative permissions are also needed to retrieve messages from an outgoing queue.
Messages cannot be read from a queue that resides on a remote computer without a direct connection. Opening a queue with receive access or peek access requires a direct connection to the computer where the queue resides, and an RPC session is established during calls to receive or peek at messages. For more information, see Opening Remote Queues with Peek or Receive Access.
Equivalent COM Method
When using COM components, you can receive messages referenced by a lookup identifier or by their position at the front or end of the queue by calling the MSMQQueue.ReceiveByLookupId, MSMQQueue.ReceiveFirstByLookupId, MSMQQueue.ReceiveLastByLookupId, MSMQQueue.ReceiveNextByLookupId, and MSMQQueue.ReceivePreviousByLookupId methods of the MSMQQueue object. You can peek at messages referenced by a lookup identifier or by their position at the front or end of the queue by calling the MSMQQueue.PeekByLookupId, MSMQQueue.PeekFirstByLookupId, MSMQQueue.PeekLastByLookupId, MSMQQueue.PeekNextByLookupId, and MSMQQueue.PeekPreviousByLookupId methods of the MSMQQueue object.
The following code examples are included in Using Message Queuing.
|For an example of||See|
Peeking at a message with a given lookup identifier
Navigating through a queue using the lookup identifiers of the messages
The following code shows how to receive or peek at a message from a subqueue using a lookup ID.
PCWSTR wszRejectedOrdersQueue = ”Direct=OS:mymachine\private$\orders;rejectedorders”; hr = MQReceiveMessageByLookupID(hQueue, LookupID, MQ_LOOKUP_PEEK_CURRENT, &msgprops, NULL, NULL, MQ_NO_TRANSACTION);
Windows NT/2000/XP: Included in Windows XP and Windows Server 2003.
Windows 95/98/Me: Unsupported.
Header: Declared in Mq.h.
Library: Use Mqrt.lib.