Export (0) Print
Expand All

C/C++ Code Example: Creating a Queue

This example provides an application-defined function that creates a public or private queue based on the queue path name provided. The function returns the public or private format name of the queue created and the length of the format name including the null-terminating character. For an example of creating transactional queues, see C/C++ Code Example: Creating a Transactional Queue.

If a NULL pointer to a SECURITY_DESCRIPTOR structure is passed to this function in pSecurityDescriptor, the queue is created with the default security descriptor. For an example of creating a security descriptor, see C/C++ Code Example: Creating a Security Descriptor.

It is the responsibility of the calling function that pdwOutFormatNameLength points to the number of WCHARs in the buffer supplied to receive the format name of the queue.

ms711416.note(en-us,VS.85).gifNote
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 creates the queue.

To create a queue

  1. Define the MQQUEUEPROPS structure.

  2. Set the queue properties. This example sets the following properties. The PROPID_Q_PATHNAME property to the

    ms711416.note(en-us,VS.85).gifNote
    The PROPID_Q_PATHNAME property is the only required queue property.

  3. Initialize the MQQUEUEPROPS structure.

  4. Call MQCreateQueue to create the queue.

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

#include "windows.h"
#include "mq.h"
#include "tchar.h"
#define BUFLEN = 256 ;

HRESULT CreateMSMQQueue(
                        LPWSTR wszPathName, 
                        PSECURITY_DESCRIPTOR pSecurityDescriptor,
                        LPWSTR wszOutFormatName,
                        DWORD *pdwOutFormatNameLength
                        )
{

  // Define the maximum number of queue properties.
  const int NUMBEROFPROPERTIES = 2;


  // Define a queue property structure and the structures needed to initialize it.
  MQQUEUEPROPS   QueueProps;
  MQPROPVARIANT  aQueuePropVar[NUMBEROFPROPERTIES];
  QUEUEPROPID    aQueuePropId[NUMBEROFPROPERTIES];
  HRESULT        aQueueStatus[NUMBEROFPROPERTIES];
  HRESULT        hr = MQ_OK;
  
  
  // Validate the input parameters.
  if (wszPathName == NULL || wszOutFormatName == NULL || pdwOutFormatNameLength == NULL)
  {
    return MQ_ERROR_INVALID_PARAMETER;
  }
  
  
  // Set queue properties.
  DWORD cPropId = 0;
  aQueuePropId[cPropId] = PROPID_Q_PATHNAME;
  aQueuePropVar[cPropId].vt = VT_LPWSTR;
  aQueuePropVar[cPropId].pwszVal = wszPathName;
  cPropId++;

  WCHAR wszLabel[MQ_MAX_Q_LABEL_LEN] = L"Test Queue";
  aQueuePropId[cPropId] = PROPID_Q_LABEL;
  aQueuePropVar[cPropId].vt = VT_LPWSTR;
  aQueuePropVar[cPropId].pwszVal = wszLabel;
  cPropId++;


  // Initialize the MQQUEUEPROPS structure.
  QueueProps.cProp = cPropId;               // Number of properties
  QueueProps.aPropID = aQueuePropId;        // IDs of the queue properties
  QueueProps.aPropVar = aQueuePropVar;      // Values of the queue properties
  QueueProps.aStatus = aQueueStatus;        // Pointer to the return status


  // Call MQCreateQueue to create the queue.
  WCHAR wszFormatNameBuffer[BUFLEN];
  DWORD dwFormatNameBufferLength = BUFLEN;
  hr = MQCreateQueue(pSecurityDescriptor,         // Security descriptor
                     &QueueProps,                 // Address of queue property structure
                     wszFormatNameBuffer,         // Pointer to format name buffer
                     &dwFormatNameBufferLength);  // Pointer to receive the queue's format name length in Unicode characters not bytes.


  // Return the format name if the queue is created successfully.
  if (hr == MQ_OK || hr == MQ_INFORMATION_PROPERTY)
  {
    if (*pdwOutFormatNameLength >= dwFormatNameBufferLength)
    {
      wcsncpy_s(wszOutFormatName, *pdwOutFormatNameLength - 1, wszFormatNameBuffer, _TRUNCATE);
      // ************************************
      // You must copy wszFormatNameBuffer into the 
      // wszOutFormatName buffer.
      // ************************************
      wszOutFormatName[*pdwOutFormatNameLength - 1] = L'\0';
      *pdwOutFormatNameLength = dwFormatNameBufferLength;
    }
    else
    {
      wprintf(L"The queue was created, but its format name cannot be returned.\n");
    }
  }
  return hr;
}
Show:
© 2014 Microsoft