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.
Note The MSMQ pathname must be unique in the MSMQ enterprise. This applies to public and private queues.
MQQUEUEPROPS QueueProps; PROPVARIANT aVariant[2]; QUEUEPROPID aPropId[2]; DWORD PropIdCount = 0; HRESULT hr; DWORD dwFormatNameBufferLength = 256; WCHAR szFormatNameBuffer[256]; PSECURITY_DESCRIPTOR pSecurityDescriptor;
//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
pSecurityDescriptor = NULL;
hr = MQCreateQueue( pSecurityDescriptor, //Security &QueueProps, //Queue properties szFormatNameBuffer, //Output: Format Name &dwFormatNameBufferLength //Output: Format Name len );
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 );
The following optional queue properties can be set by the application when creating the queue:
The following properties are set by MSMQ when it creates the queue:
PROPID_Q_INSTANCE (for public queues)