Windows Dev Center

C/C++ Code Example: Sending Messages Using Multicast Addresses

This example provides an application-defined function that sends a message to the destination queues associated with a multicast address provided by the caller.

  1. Define the maximum number of properties and property count variable.

  2. Define the MQMSGPROPS structure.

  3. Specify message properties. This example sets the PROPID_M_LABEL property to "test message".

  4. Initialize the MQMSGPROPS structure.

  5. Generate a multicast address format name using the multicast address provided by the caller.

wcslen properly handles only null-terminated strings. This code example does not verify that the string passed to it is null-terminated. It is the responsibility of the caller to ensure that the string passed is null-terminated

  1. Call MQOpenQueue to open the destination queues using the multicast address format name.

  2. Call MQSendMessage to send a copy of the message to each opened queue. Note that the message identifier is the same for each copy of the message.

  3. Call MQCloseQueue to free the resources used to open the queues.

Code Example

The following code example requires MSMQ 3.0.

HRESULT SendMulticast(
                      WCHAR * wszAddress

  // Validate the input string.
  if (wszAddress == NULL)

  // Define the required variables and constants.
  const int NUMBEROFPROPERTIES = 5;              // Number of properties
  DWORD cPropId = 0;                             // Properties counter

  HRESULT hr = MQ_OK;                            // Return code
  HANDLE hQueue = NULL;                          // Queue handle
  // Define an MQMSGPROPS structure.
  MQMSGPROPS msgProps;
  // Specify the message properties to be sent.
  aMsgPropId[cPropId] = PROPID_M_LABEL;               // Property ID
  aMsgPropVar[cPropId].vt = VT_LPWSTR;                // Type indicator
  aMsgPropVar[cPropId].pwszVal = L"test message";     // The message's label

  // 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
  // Generate a multicast address format name.
  WCHAR * wszFormatName = NULL;
  DWORD dwFormatNameLength = 0;
  dwFormatNameLength = wcslen(wszAddress) + 11;
  wszFormatName = new WCHAR[wcslen(wszAddress) + 11];
  if (wszFormatName == NULL)
  memset(wszFormatName, 0, dwFormatNameLength*sizeof(WCHAR));

  // ************************************
  // You must concatenate the string "MULTICAST=" and wszAddress into 
  // the wszFormatName buffer.
  // wszFormatName = "MULTICAST=" + wszAddress
  // If the format name is too long for the buffer, return 
  // FALSE.
  // ************************************

  // Open the queues using the multicast address format name.
  hr = MQOpenQueue(
                   wszFormatName,                     // Multicast address format name
                   MQ_SEND_ACCESS,                    // Access mode
                   MQ_DENY_NONE,                      // Share mode
                   &hQueue                            // OUT: queue handle
  // Free the memory that was allocated for the format name string.
  delete [] wszFormatName;

  // Handle any error returned by MQOpenQueue.
  if (FAILED(hr))
    return hr;
  // Send the message to the queues listening to the multicast
  // address.
  hr = MQSendMessage(
                     hQueue,                         // Queue handle
                     &msgProps,                      // Message property structure
                     MQ_NO_TRANSACTION               // No transaction

  if (FAILED(hr))
    return hr;
  // Close the queue.
  hr = MQCloseQueue(hQueue);
  return hr;
© 2015 Microsoft