Creating a Queue

All queues, both public and private, are created by calling MQCreateQueue. For a description of public and private queues, see Message Queues.

The only property required to create a queue is PROPID_Q_PATHNAME. This property tells MSMQ where to store the queue's messages, if the queue is public or private, and the name of the queue. Once the queue is created, the format name returned in the lpwcsFormatName parameter is used to open the queue. For a description of MSMQ pathnames and queue format names, see Referencing a Queue.

To create a queue
  1. Determine which computer will hold the messages for the queue. The computer's machine name is part of the queue's MSMQ pathname (PROPID_Q_PATHNAME). For private queues, the local computer must be specified.

  2. Determine whether the queue should be public or private. This tells MSMQ where to register the queue: public queues are registered in the MQIS and private queues are registered on the local machine (private queues can only be registered on the local machine). This information is part of the queue's MSMQ pathname (PROPID_Q_PATHNAME).

  3. Determine the name for the queue. The queue's name is part of the queue's MSMQ pathname (PROPID_Q_PATHNAME).

    Note The MSMQ pathname must be unique in the MSMQ enterprise. This applies to public and private queues.

  4. Determine what queue properties must be set. If a queue property is not specified when calling MQCreateQueue, its default value is used. For a complete list of the queue properties that can be set when a queue is created, see the following Queue Properties section.

  5. Specify the MQQUEUEPROPS structure.
    MQQUEUEPROPS QueueProps;
    PROPVARIANT aVariant[2];
    QUEUEPROPID aPropId[2];
    DWORD PropIdCount = 0;
    HRESULT hr;
    DWORD dwFormatNameBufferLength = 256;
    WCHAR szFormatNameBuffer[256];
    PSECURITY_DESCRIPTOR pSecurityDescriptor;
     
  6. Fill in the MQQUEUEPROPS structure. PROPID_Q_PATHNAME is required; it indicates if the queue is public or private.
    //Set the PROPID_Q_PATHNAME property.
    aPropId[PropIdCount] = PROPID_Q_PATHNAME;    //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;        //Type
    aVariant[PropIdCount].pwszVal = L".\\MyPublicQueue";
        
    PropIdCount++;
        
    //Set the PROPID_Q_LABEL property.
    aPropId[PropIdCount] = PROPID_Q_LABEL;    //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;     //Type
    aVariant[PropIdCount].pwszVal = L"MyPublicQueue";
        
    PropIdCount++;
        
        
    //Set the MQQUEUEPROPS structure.
    QueueProps.cProp = PropIdCount;           //No of properties
    QueueProps.aPropID = aPropId;             //Ids of properties
    QueueProps.aPropVar = aVariant;           //Values of properties
    QueueProps.aStatus = NULL;                //No error reports
        
  7. Set the queue's security descriptor. The following line of code specifies the default security descriptor.
    pSecurityDescriptor = NULL;
        
  8. Call MQCreateQueue.
    hr = MQCreateQueue(
         pSecurityDescriptor,            //Security
         &QueueProps,                    //Queue properties
         szFormatNameBuffer,             //Output: Format Name
         &dwFormatNameBufferLength       //Output: Format Name len
         );
     
     

Examples

The following two examples show the code used to specify the MSMQ pathname and label for a public queue and a private queue, plus a call to MQCreateQueue to create the queue.

Note In these examples, a "." is used to indicate the local machine in PROPID_Q_PATHNAME. For MSMQ servers and independent clients, the local machine is the local computer. However, for MSMQ-dependent clients the local machine is the client's MSMQ server.

For a public queue
    ////////////////////////////
    //  Define the MQQUEUEPROPS
    //  structure.
    ////////////////////////////
    
    MQQUEUEPROPS QueueProps;
    PROPVARIANT aVariant[2];
    QUEUEPROPID aPropId[2];
    DWORD PropIdCount = 0;
    HRESULT hr;
    DWORD dwFormatNameBufferLength = 256;
    TCHAR szFormatNameBuffer[ 256];
    PSECURITY_DESCRIPTOR pSecurityDescriptor;
    
    
    //////////////////////////////////////////
    // Specify the queue properties. Add 
    // additional properties as needed
    //////////////////////////////////////////
    
    //Set the PROPID_Q_PATHNAME property.
    aPropId[PropIdCount] = PROPID_Q_PATHNAME;    //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;        //Type
    aVariant[PropIdCount].pwszVal = L".\\MyPublicQueue";
    
    PropIdCount++;
    
    //Set the PROPID_Q_LABEL property.
    aPropId[PropIdCount] = PROPID_Q_LABEL;    //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;     //Type
    aVariant[PropIdCount].pwszVal = L"MyPublicQueue";
    
    PropIdCount++;
    
    
    //Set the MQQUEUEPROPS structure.
    QueueProps.cProp = PropIdCount;           //No of properties
    QueueProps.aPropID = aPropId;             //Ids of properties
    QueueProps.aPropVar = aVariant;           //Values of properties
    QueueProps.aStatus = NULL;                //No error reports
    
    //Set security to default security descriptor
    pSecurityDescriptor = NULL;
    
    
    ////////////////////////////
    //Create the queue.
    ////////////////////////////
    hr = MQCreateQueue(
         pSecurityDescriptor,            //Security
         &QueueProps,                    //Queue properties
         szFormatNameBuffer,             //Output: Format Name
         &dwFormatNameBufferLength       //Output: Format Name len
         );
 
For a private queue
    MQQUEUEPROPS QueueProps;
    PROPVARIANT aVariant[2];
    QUEUEPROPID aPropId[2];
    DWORD PropIdCount = 0;
    HRESULT hr;
    DWORD dwFormatNameBufferLength = 256;
    WCHAR szFormatNameBuffer[ 256];
    PSECURITY_DESCRIPTOR pSecurityDescriptor;
    
    
    //////////////////////////////////////////
    // Specify the queue properties. Add 
    // additional properties as needed
    //////////////////////////////////////////
    
    //Set the PROPID_Q_PATHNAME property.
    aPropId[PropIdCount] = PROPID_Q_PATHNAME;    //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;        //Type
    aVariant[PropIdCount].pwszVal = L".\\private$\\MyPrivateQueue";
    
    PropIdCount++;
    
    //Set the PROPID_Q_LABEL property.
    aPropId[PropIdCount] = PROPID_Q_LABEL;    //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;     //Type
    aVariant[PropIdCount].pwszVal = L"MyPrivateQueue";
    
    PropIdCount++;
    
    //Set the MQQUEUEPROPS structure.
    QueueProps.cProp = PropIdCount;           //No of properties
    QueueProps.aPropID = aPropId;             //Ids of properties
    QueueProps.aPropVar = aVariant;           //Values of properties
    QueueProps.aStatus = NULL;                //No error reports
    
    //Set security to default security descriptor.
    pSecurityDescriptor = NULL;
    
    ///////////////////////    
    //Create the queue.
    ///////////////////////
    hr = MQCreateQueue(
         pSecurityDescriptor,            //Security
         &QueueProps,                    //Queue properties
         szFormatNameBuffer,             //Output: Format Name
         &dwFormatNameBufferLength       //Output: Format Name len
         );
 

Queue Properties

The following optional queue properties can be set by the application when creating the queue:

PROPID_Q_AUTHENTICATE

PROPID_Q_BASEPRIORITY

PROPID_Q_JOURNAL

PROPID_Q_JOURNAL_QUOTA

PROPID_Q_LABEL

PROPID_Q_PRIV_LEVEL

PROPID_Q_QUOTA

PROPID_Q_TRANSACTION

PROPID_Q_TYPE

The following properties are set by MSMQ when it creates the queue:

PROPID_Q_CREATE_TIME

PROPID_Q_INSTANCE (for public queues)

PROPID_Q_MODIFY_TIME


© 1997 by Microsoft Corporation. All rights reserved.