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

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

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 MSMQQueueInfo and MSMQMessage interfaces and declare a smart pointer to an MSMQQueue interface.

  2. Create the direct format name for the queue.

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. Set the FormatName property of the MSMQQueueInfo object to the format name created.

  2. Call MSMQQueueInfo.Open to open the queue.

  3. Call MSMQMessage.Send to send the message to the destination queue.

  4. Call MSMQQueue.Close to close the queue.

Code Example

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

HRESULT SendMessage(  
                    WCHAR *wszComputerName,  
                    WCHAR *wszQueueName  
                    )  
{  
  HRESULT hr = S_OK;  
  WCHAR * wszFormatName = NULL;  
  
  // Validate the input strings.  
  if (wszComputerName == NULL || wszQueueName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  try  
  {  
    IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");  
    IMSMQQueuePtr     pQueue;  
    IMSMQMessagePtr   pMsg("MSMQ.MSMQMessage");  
    WCHAR   *wszFormatNamePrefix = L"DIRECT=OS:";  
    int     nFormatNamePrefixLen = 0;  
    int     nComputerNameLen = 0;  
    int     nQueueNameLen = 0;  
    int     nFormatNameLen = 0;  
  
    // Create a direct format name of the queue, and  
    // set the FormatName property of the MSMQQueueInfo object.  
    nFormatNamePrefixLen = wcslen(wszFormatNamePrefix);  
    nComputerNameLen = wcslen(wszComputerName);  
    nQueueNameLen = wcslen(wszQueueName);  
    nFormatNameLen = nFormatNamePrefixLen + nComputerNameLen + nQueueNameLen + 2;  
    wszFormatName = new WCHAR[nFormatNameLen];  
    if (wszFormatName)  
    {  
      memset(wszFormatName, 0, nFormatNameLen*sizeof(WCHAR));  
      // ************************************  
      // You must concatenate wszFormatNamePrefix, wszComputerName,   
      //"\", and wszQueueName into the wszFormatName buffer.  
      // wszFormatName = wszFormatNamePrefix + wszComputerName +  
      // "\" + wszQueueName  
      // ************************************  
      pInfo->FormatName = wszFormatName;     
    }  
  
    // Open the queue.  
    pQueue = pInfo->Open(MQ_SEND_ACCESS, MQ_DENY_NONE);  
  
    pMsg->Label = "Test Message";   
    pMsg->Send(pQueue);  
  
    pQueue->Close();  
  
    printf("The following message was sent successfully to queue %s\n", (TCHAR *)pInfo->PathName);  
  }  
  catch (const _com_error& comerr)   
  {  
    hr = comerr.Error();  
  
    wprintf(L"Error Code = 0x%X\nError Description = %s\n", hr, (WCHAR *)comerr.Description());  
  }  
  if (wszFormatName)  
  {  
    delete [] wszFormatName;  
  }  
  return hr;  
}