Delen via


C-C++ Code Example: Sending Messages to a Destination Queue

 

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 a single private or public destination queue on the local computer or a remote computer.

Note

Remote queues can be opened to send messages while offline. To do this, your application must either obtain and cache the format name of a remote queue while online or have the information needed to construct a direct format name for the queue. This example creates a direct format name from the computer name and queue name provided by the caller.

For information on the different ways Message Queuing sends messages, see Message Queuing Messages.

To Send a message

  1. Define the required constants and variables.

  2. Define the MQMSGPROPS structure.

  3. Specify the properties of the message. This example defines the PROPID_M_LABEL property.

  4. Initialize the MQMSGPROPS structure.

  5. Create a format name for opening the queue. This example creates a direct format name.

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. Call MQOpenQueue to open the queue with send access.

  2. Call MQSendMessage to send the message.

  3. Call MQCloseQueue to close the opened queue and free resources.

The following code example can be run on all versions of Message Queuing.

#include "windows.h"  
#include "mq.h"  
#include "tchar.h"  
  
HRESULT SendMessage(  
                    WCHAR * wszQueueName,  
                    WCHAR * wszComputerName  
                    )  
{  
  
  // Validate the input strings.  
  if (wszQueueName == NULL || wszComputerName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define the required constants and variables.  
  const int NUMBEROFPROPERTIES = 5;                   // Number of properties  
  DWORD cPropId = 0;                                  // Property 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;  
  msgProps.aPropID = aMsgPropId;  
  msgProps.aPropVar = aMsgPropVar;  
  msgProps.aStatus = aMsgStatus;  
  
  // Create a direct format name for the queue.  
  WCHAR * wszFormatName = NULL;  
  DWORD dwBufferLength = 0;  
  const WCHAR * wszFormatStr = L"DIRECT=OS:%s\\%s" ;  
  dwBufferLength = wcslen(wszQueueName) + wcslen(wszComputerName) +   
                                 wcslen(wszFormatStr) - 4;  
  
  wszFormatName = new WCHAR[dwBufferLength];  
  if (wszFormatName == NULL)  
  {  
    return MQ_ERROR_INSUFFICIENT_RESOURCES;  
  }  
  memset(wszFormatName, 0, dwBufferLength*sizeof(WCHAR));  
  
  if (_snwprintf_s(  
                 wszFormatName,  
                 dwBufferLength,  
                 dwBufferLength - 1,  
                 L"DIRECT=OS:%s\\%s",  
                 wszComputerName,  
                 wszQueueName  
                ) < 0)  
  {  
    wprintf(L"The format name is too long for the buffer specified.\n");  
    return FALSE;  
  }  
  else  
  {  
    wszFormatName[dwBufferLength - 1] = L'\0';  
  }  
  
  // Call MQOpenQueue to open the queue with send access.  
  hr = MQOpenQueue(  
                   wszFormatName,                     // Format name of the queue  
                   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 queue.  
  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 queue.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}