C-C++ COM 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.

This example uses smart pointers to the following Message Queuing interfaces.

  • MSMQDestination: Represents the destination queues listed in the multiple-element format name.

  • MSMQMessage3: Represents the message.

To use smart pointers, your application must import Mqoa.dll. You can import this DLL using the #import directive and specify the MSMQ namespace.

#import "mqoa.dll"  
using namespace MSMQ;  

Before using any smart pointer, your application must call CoInitialize or CoInitializeEx to initialize the COM library. After the COM library is no longer needed, your application must call CoUnitialize. For more information, see Using Message Queuing COM Components in Visual C++ and C.

To send a message to a destination queue

  1. Create smart pointers to MSMQDestination and MSMQMessage3 interfaces.

  2. Create a multiple-element format name from the format names passed to the function.


Note that 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. Set the format name of the MSMQDestination object to the multiple-element format name created.

  2. Set message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message Sent to Multiple Destination Queues."

  3. Call MSMQMessage.Send using the corresponding smart pointer to send a copy of the message to each destination queue referenced by the multiple-element format name. Note that when using an MSMQDestination object, Message Queuing implicitly opens the destination queues with send access.

  4. Call MSMQDestination.Close using the corresponding smart pointer to close the MSMQDestination object.

Code Example

The following code example requires MSMQ 3.0.

HRESULT SendToDest(  
                   WCHAR *wszFormatName1,  
                   WCHAR *wszFormatName2,  
                   WCHAR *wszFormatName3  
  // Validate the input strings.  
  if (wszFormatName1 == NULL || wszFormatName2 == NULL || wszFormatName3 == NULL)  
    // Create smart pointers to IMSMQDestination and IMSMQMessage3 interfaces.  
    IMSMQDestinationPtr pDest(L"MSMQ.MSMQDestination");  
    IMSMQMessage3Ptr pMsg(L"MSMQ.MSMQMessage");  
  // Create a multiple-element format name from the format names supplied.  
    WCHAR * wszMultElemFormatName = NULL;  
    DWORD dwMultElemFormatNameLength = 0;  
    dwMultElemFormatNameLength = wcslen(wszFormatName1) +  
                                 wcslen(wszFormatName2) +  
                                 wcslen(wszFormatName3) + 3;  
    WCHAR * wszMultElemFormatName = new WCHAR[dwMultElemFormatNameLength];  
    if (wszMultElemFormatName == NULL) return MQ_ERROR_INSUFFICIENT_RESOURCES;  
    memset(wszMultElemFormatName, 0, dwMultElemFormatNameLength*sizeof(WCHAR));  
  // ************************************  
  // You must concatenate wszFormatName1, ",", wszFormatName2, ",",     
  // and wszFormatName3 into the wszMultElemFormatName buffer.  
  // wszMultElemFormatName = szFormatName1 + "," + wszFormatName2 + ","   
  // + wszFormatName3   
  // If the format name is too long for the buffer, return FALSE.  
  // ************************************  
    // Set the format name of the MSMQDestination object to the multiple-element format name.  
    pDest->FormatName = wszMultElemFormatName;  
    // Set message properties, send the message, close the MSMQDestination object,  
    // and free the memory allocated for the multiple-element format name.  
    pMsg->Label = L"Test Message Sent to Multiple Destination Queues";  
    delete [] wszMultElemFormatName;  
  catch (const _com_error& comerr)   
    wprintf(L"Error Code = 0x%X\nError Description = %s\n",  
    comerr.Error(), (WCHAR *)comerr.Description() );  
    return comerr.Error();  
  return S_OK;