Delen via


C-C++ Code Example: Sending Messages Using Distribution Lists

 

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 specified in a distribution list provided by the caller.

  1. Define the required constants and variables.

  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 distribution list format name using the distribution list identifier (GUID) provided as a string by the caller. For information about obtaining a distribution list GUID, see Distribution List Format Names.

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 distribution lists 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.

#include "windows.h"  
#include "mq.h"  
#include "tchar.h"  
  
HRESULT SendDL(  
               WCHAR * wszDL  
               )  
{  
  
  // Validate the input string.  
  if (wszDL == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define 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 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  
  
  // Create a distribution list format name for opening all   
  // the destination queues.  
  WCHAR * wszFormatName = NULL;  
  DWORD dwFormatNameLength = 0;  
  dwFormatNameLength = wcslen(wszDL) + 4;  
  wszFormatName = new WCHAR[dwFormatNameLength];  
  if (wszFormatName == NULL)  
  {  
    return MQ_ERROR_INSUFFICIENT_RESOURCES;  
  }  
  memset(wszFormatName, 0, dwFormatNameLength*sizeof(WCHAR));  
  
  if (_snwprintf_s(  
                 wszFormatName,  
                 dwFormatNameLength,  
                 dwFormatNameLength - 1,  
                 L"DL=%s",  
                 wszDL  
                 ) < 0)   
  {  
    wprintf(L"The format name is too long for the buffer specified.\n");  
    return FALSE;  
  }  
  else  
  {  
    wszFormatName[dwFormatNameLength - 1] = L'\0';  
  }  
  
  // Call MQOpenQueue to open the queues referenced in the   
  // distribution list.  
  hr = MQOpenQueue(  
                   wszFormatName,                     // Distribution list 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;  
  }  
  
  // Call MQSendMessage to send the message to the queues referenced  
  // in the distribution list.  
  hr = MQSendMessage(  
                     hQueue,                          // Queue handle  
                     &msgProps,                       // Message property structure  
                     MQ_NO_TRANSACTION                // Not in a transaction  
                     );  
  if (FAILED(hr))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Call MQCloseQueue to close the queues associated with the   
  // distribution list.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}