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 pathname of the queue (PROPID_Q_PATHNAME).
aPropId[PropIdCount] = PROPID_Q_PATHNAME; // Property identifier
aVariant[PropIdCount].vt = VT_LPWSTR; // Type indicator
aVariant[PropIdCount].pwszVal = L".\\MyPublicQueue";
PropIdCount++;
// Set the label of the queue (PROPID_Q_LABEL).
aPropId[PropIdCount] = PROPID_Q_LABEL; // Property identifier
aVariant[PropIdCount].vt = VT_LPWSTR; // Type indicator
aVariant[PropIdCount].pwszVal = L"Test Queue";
PropIdCount++;
QueueProps.cProp = PropIdCount; // Number 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 and a private queue, plus a call to MQCreateQueue to create the queue.
Note In these examples, "." 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]; // Modify for additional properties.
QUEUEPROPID aPropId[2]; // Modify for additional properties.
DWORD PropIdCount = 0;
HRESULT hr;
DWORD dwFormatNameBufferLength = 256;
TCHAR szFormatNameBuffer[ 256];
PSECURITY_DESCRIPTOR pSecurityDescriptor;
//////////////////////////////////////////
// Specify the queue's pathname and label
// properties. Note the pathname uses "."
// as shorthand for the local computer.
//////////////////////////////////////////
// Set the pathname of the queue (PROPID_Q_PATHNAME).
aPropId[PropIdCount] = PROPID_Q_PATHNAME; // Property identifier
aVariant[PropIdCount].vt = VT_LPWSTR; // Type indicator
aVariant[PropIdCount].pwszVal = L".\\MyPublicQueue"; // Pathname
PropIdCount++;
// Set the label of the queue (PROPID_Q_LABEL).
aPropId[PropIdCount] = PROPID_Q_LABEL; // Property identifier
aVariant[PropIdCount].vt = VT_LPWSTR; // Type indicator
aVariant[PropIdCount].pwszVal = L"Test Queue"; // Queue label
PropIdCount++;
////////////////////////////////////////////////////////
// Add additional properties here. When adding properties,
// increment the indexes for the aVariant and aPropId arrays
// in their respective declaration statements above.
////////////////////////////////////////////////////////
////////////////////////////////////
// Set the MQQUEUEPROPS structure.
/////////////////////////////////////
QueueProps.cProp = PropIdCount; // Number of properties
QueueProps.aPropID = aPropId; // Ids of properties
QueueProps.aPropVar = aVariant; // Values of properties
QueueProps.aStatus = NULL; // No error reports
pSecurityDescriptor = NULL; // Set default security descriptor
////////////////////////////
//Create the queue.
////////////////////////////
hr = MQCreateQueue(
pSecurityDescriptor, // Security
&QueueProps, // Queue properties
szFormatNameBuffer, // Output: Format Name
&dwFormatNameBufferLength // Output: Format Name length
);
For a private queue
MQQUEUEPROPS QueueProps;
PROPVARIANT aVariant[2];
PROPVARIANT aVariant[2]; // Modify for additional properties.
QUEUEPROPID aPropId[2]; // Modify for additional properties.
HRESULT hr;
DWORD dwFormatNameBufferLength = 256;
WCHAR szFormatNameBuffer[ 256];
PSECURITY_DESCRIPTOR pSecurityDescriptor;
//////////////////////////////////////////
// Specify the queue's pathname and label
// properties. Note the pathname uses "."
// as shorthand for the local computer.
//////////////////////////////////////////
// Set the pathname of the queue (PROPID_Q_PATHNAME).
aPropId[PropIdCount] = PROPID_Q_PATHNAME; // Property identifier
aVariant[PropIdCount].vt = VT_LPWSTR; // Type indicator
aVariant[PropIdCount].pwszVal = L".\\PRIVATE$\\MyPrivateQueue";
PropIdCount++;
// Set the label of the queue (PROPID_Q_LABEL).
aPropId[PropIdCount] = PROPID_Q_LABEL; // Property identifier
aVariant[PropIdCount].vt = VT_LPWSTR; // Type indicator
aVariant[PropIdCount].pwszVal = L"Test Queue"; // Queue label
PropIdCount++;
////////////////////////////////////////////////////////
// Add additional properties here. When adding properties,
// increment the indexes for the aVariant and aPropId arrays
// in their respective declaration statements above.
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Set the MQQUEUEPROPS structure.
////////////////////////////////////////////////////////
QueueProps.cProp = PropIdCount; // Number of properties
QueueProps.aPropID = aPropId; // Ids of properties
QueueProps.aPropVar = aVariant; // Values of properties
QueueProps.aStatus = NULL; // No error reports
pSecurityDescriptor = NULL; // Set default security descriptor
///////////////////////
//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)