共用方式為


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

 

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

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.

Note

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)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // 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;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];  
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  
  // 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  
  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  
  
  // 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)  
  {  
    return MQ_ERROR_INSUFFICIENT_RESOURCES;  
  }  
  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))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Close the queue.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}