C-C++ COM Code Example: Creating a Queue


Updated: July 19, 2016

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 creates a public or private queue using Message Queuing COM objects in C++ based on a given queue path name. For creating transactional queues, see C/C++ COM Code Example: Creating a Transactional Queue.

This example uses a smart pointer to the MSMQQueueInfo interface.

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.

System_CAPS_ICON_note.jpg Note

Public queues cannot be created if there is no connection to the directory service. This restriction applies to dependent client computers, independent client computers that are working offline, and Message Queuing servers that have routing services enabled (for MSMQ 1.0, these servers are referred to as FRS servers).

The following procedure shows how the function is used to create a queue.

To create a queue

  1. Create a smart pointer to an MSMQQueueInfo interface.

  2. Set the queue path name and label.

  3. (Optional.) Set additional queue properties.

  4. Call MSMQQueueInfo.Create with its IsTransactional parameter set to FALSE (the default).

The following code example contains no version-specific Message Queuing calls.

HRESULT CreateQueue(  
                    WCHAR *wszPathName  
  HRESULT hr = S_OK;  
  // Validate the input string.  
  if (wszPathName == NULL)  
    IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");  
    // Set the queue's path name and label.  
    pInfo->PathName = wszPathName;     
    pInfo->Label = "TestQueue";  
    // Create the queue.  
    // Optional. Display the success message.  
    WCHAR wszMessage[1024] = {0};  
  // ************************************  
  // You must concatenate "The queue",  wszPathName, and "was created   
  // successfully." into the wszMessage buffer.  
  // wszMessage = "The queue " + wszPathName + " was created   
  // successfully."  
  // If the message is too long for the buffer, return FALSE.  
  // Otherwise, show the message   
  // MessageBoxW(NULL, wszMessage, NULL, MB_OK);  
  // ************************************     
  catch (const _com_error& comerr)   
     hr = comerr.Error();  
     // Display the error message.  
     WCHAR wszMessage[2048] = {0};  
     // ************************************  
     // You must concatenate "Error Code: ", hr, "\nError   
     // Description: ", (WCHAR *)comerr.Description(), and "\n"   
     // into the wszMessage buffer.  
     // wszMessage = "Error Code: " + hr + "\nError   
     // Description: "  + (WCHAR *)comerr.Description()+ "\n"  
     // If the message is too long for the buffer, return FALSE.  
     // Otherwise, show the message   
     // MessageBoxW(NULL, wszMessage, NULL, MB_OK);  
     // ************************************  
  return hr;