Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

C/C++ COM Code Example: Creating a Transactional Queue

This example provides an application-defined function that creates a public or private transactional queue using Message Queuing COM objects in C++ based on a given queue path name.

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.

The following procedure shows how the function creates a transactional queue.

To create a nontransactional queue

  1. Create a smart pointer to the 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 TRUE.

Example

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

HRESULT CreateXactQueue(
                        WCHAR *wszPathName
                        )
{
HRESULT hr = S_OK;
  
  
  // Validate the input string.
  if (wszPathName == NULL)
  {
    return MQ_ERROR_INVALID_PARAMETER;
  }
  
  try
  {
    IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");
    _variant_t varTrue(TRUE);
    
    // Set the path name and label of the queue.
    pInfo->PathName = wszPathName;   
    pInfo->Label = "TestQueue"; 
     
    // Create the queue, setting the transaction parameter.
    pInfo->Create(&varTrue);
    
    // 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 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;
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.