Sdílet prostřednictvím


C-C++ Code Example: Sending Messages Using Multiple-Element Format Names

 

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 three destination queues specified by the caller. The function combines the three queue format names (provided by the caller) into a single multiple-element format name so that the following operations can be made with single calls.

For information on other ways to send messages to multiple destinations, see Multiple-Destination Messaging.

To send a message using multiple-element format names

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

  2. Define the MQMSGPROPS structure.

  3. Create a multiple-element format name using format names provided by caller.

Note

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

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

  2. Initialize the MQMSGPROPS structure.

  3. Call MQOpenQueue to open the destination queues with send access.

  4. Call MQSendMessage to send a copy of the message to each opened queue.

Note

Note that the message identifier of each copy is the same.

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

Code Example

The following code example requires MSMQ 3.0.

HRESULT SendMultiple(  
                     WCHAR * wszFormatName1,   
                     WCHAR * wszFormatName2,  
                     WCHAR * wszFormatName3  
                     )  
{  
  
  // Validate the input strings.  
  if (wszFormatName1 == NULL || wszFormatName2 == NULL || wszFormatName3 == 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 MQMSGPROPS structure.  
  MQMSGPROPS msgProps;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];  
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  
  // Create a multiple-element format name from the given format names.  
  WCHAR * wszFormatNameList = NULL;  
  DWORD dwFormatNameLength = 0;  
  dwFormatNameLength = wcslen(wszFormatName1) +  
                        wcslen(wszFormatName2) +  
                        wcslen(wszFormatName3) + 3;  
  WCHAR * wszFormatNameList = new WCHAR[dwFormatNameLength];  
  if (wszFormatNameList == NULL) return MQ_ERROR_INSUFFICIENT_RESOURCES;  
  memset(wszFormatNameList, 0, dwFormatNameLength*sizeof(WCHAR);  
  // ************************************  
  // You must concatenate wszFormatName1, ",", wszFormatName2, ",", and   
  // wszFormatName3into the wszFormatNameList buffer.  
  // wszFormatNameList = wszFormatName1 + "," + wszFormatName2 + "," +   
  // wszFormatName3  
  // if the multiple-element format name is too long for the buffer,   
  // return FALSE;  
  // ************************************  
  
  // 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  
  
  // Open the queues with send access using the multiple-element   
  // format name.  
  hr = MQOpenQueue(  
                   wszFormatNameList,  
                   MQ_SEND_ACCESS,  
                   MQ_DENY_NONE,  
                   &hQueue  
                   );  
  // Free the memory that was allocated for the format name string.  
  delete [] wszFormatNameList;  
  
  // Handle any error returned by MQOpenQueue.  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  
  // Send the message to the destination queues.  
  hr = MQSendMessage(  
                     hQueue,  
                     &msgProps,  
                     MQ_NO_TRANSACTION  
                     );  
  if (FAILED(hr))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Close the destination queues.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}