Share via


C-C++ Code Example: Reading Messages By Lookup Identifier

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

The following example provides a private function that locates and reads a specific message based on the lookup identifier provided by the caller.

For more information about lookup identifiers, see Navigating with Lookup Identifiers.

To navigate a queue using lookup identifiers

  1. Define the maximum number of queue properties to be specified and the queue property counter.

  2. Define an MQMSGPROPS structure.

  3. Specify the message properties to retrieve. This example retrieves PROPID_M_LABEL and PROPID_M_LABEL_LEN. The maximum label length, including the string-terminating character, is MQ_MAX_MSG_LABEL_LEN (250 Unicode characters).

  4. Initialize the MQMSGPROPS structure.

  5. Call MQOpenQueue to open the queue with receive access. Receive access allows the application to peek at or remove the messages in the queue.

  6. Call MQReceiveMessageByLookupId using the lookup identifier provided by the caller.

  7. Call MQCloseQueue to free the resources.

Code Example

The following code example requires MSMQ 3.0.

HRESULT ReadByLookupId(  
                       LPCWSTR wszQueueFormatName,  
                       ULONGLONG ullLookupId  
                       )  
{  
  
  // Validate the input string.  
  if (prc == wszQueueFormatName)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define maximum property number and property count.  
  const int NUMBEROFPROPERTIES = 2;                   // Number of properties  
  DWORD cPropId = 0;                                  // Properties counter  
  
  HRESULT hr = MQ_OK;                                 // Return code  
  HANDLE hQueue = NULL;                               // Queue handle  
  WCHAR wszLabelBuffer[MQ_MAX_MSG_LABEL_LEN];  
  
  // Define an MQMSGPROPS structure.  
  MQMSGPROPS msgProps;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];  
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  
  // Specify the message properties to retrieve.  
  aMsgPropId[cPropId] = PROPID_M_LABEL_LEN;           // Property ID  
  aMsgPropVar[cPropId].vt = VT_UI4;                   // Type indicator  
  aMsgPropVar[cPropId].ulVal = MQ_MAX_MSG_LABEL_LEN;  // Label buffer size  
  cPropId++;  
  
  aMsgPropId[cPropId] = PROPID_M_LABEL;               // Property ID  
  aMsgPropVar[cPropId].vt = VT_LPWSTR;                // Type indicator  
  aMsgPropVar[cPropId].pwszVal = wszLabelBuffer;      // Label buffer  
  cPropId++;  
  
  // Initialize the MQMSGPROPS structure.  
  msgProps.cProp = cPropId;                           // Number of message properties  
  msgProps.aPropID = aMsgPropId;                      // IDs of the message properties  
  msgProps.aPropVar = aMsgPropVar;                    // Values of the message properties  
  msgProps.aStatus = aMsgStatus;                      // Error reports  
  
  // Open the queue to read message.  
  hr = MQOpenQueue(  
                   wszQueueFormatName,                // Format name of the queue  
                   MQ_RECEIVE_ACCESS,                 // Access mode  
                   MQ_DENY_RECEIVE_SHARE,             // Share mode  
                   &hQueue                            // OUT: Handle of queue  
                   );  
  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  
  // Peek at message with the given lookup identifier.  
  hr = MQReceiveMessageByLookupId(  
                                  hQueue,                // Handle of queue  
                                  ullLookupId,           // Lookup identifier  
                                  MQ_LOOKUP_PEEK_CURRENT // Access mode  
                                  &msgProps,             // Message property structure  
                                  NULL,                  // No OVERLAPPED structure  
                                  NULL,                  // No callback function  
                                  MQ_NO_TRANSACTION      // Not in a transaction  
                                  );  
  
  if (FAILED(hr))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Print the label of the message.  
  wprintf(L"%s\n", msgProps.aPropVar[1].pwszVal);  
  
  // Close the queue.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}