The queue's pathname indicates the name of the computer where the queue's messages are stored, if the queue is public or private, and the name of the queue.
When PROPID_Q_PATHNAME is retrieved, the type indicator in the MQQUEUEPROPS structure must be set to VT_NULL, and MQFreeMemory must be called after the return value is used. The VT_NULL setting tells MSMQ to allocate the memory needed for the returned pathname, and MQFreeMemory frees the MSMQ allocated memory.
// Define number of properties to be retrieved.
#define NumberOfProperties 1
// Define property counter.
DWORD PropIdCount = 0;
// Define the MQQUEUPROPS structure.
MQQUEUEPROPS QueueProps;
PROPVARIANT aQueuePropVar[NumberOfProperties];
QUEUEPROPID aQueuePropId[NumberOfProperties];
HRESULT aQueueStatus[NumberOfProperties];
// Define results.
HRESULT hr;
// Define format name buffer.
DWORD dwFormatNameBufferLength = 256;
WCHAR szFormatNameBuffer[256];
aQueuePropId[PropIdCount] = PROPID_Q_PATHNAME; //Property ID
aQueuePropVar[PropIdCount].vt = VT_NULL; //Type indicator
PropIdCount++;
QueueProps.cProp = PropIdCount; // Number of properties
QueueProps.aPropID = aQueuePropId; // Ids of properties
QueueProps.aPropVar = aQueuePropVar; // Values of properties
QueueProps.aStatus = aQueueStatus; // Error reports
hr = MQPathNameToFormatName(L"machinename\\queuename",
szFormatNameBuffer,
&dwFormatNameBufferLength);
if (FAILED(hr))
{
fprintf(stderr, "Failed in MQPathNameToFormatName, error = 0x%x\n",hr);
return -1;
}
hr = MQGetQueueProperties(szFormatNameBuffer, &QueueProps);
if (FAILED(hr))
{
fprintf(stderr, "Failed in MQGetQueueProperties, error = 0x%x\n",hr);
return -1;
}
wprintf(L"The label of the queue is :");
wprintf(L" %s.\n", aQueuePropVar[0].pwszVal);
MQFreeMemory(aQueuePropVar[0].pwszVal);
if (FAILED(hr))
{
fprintf(stderr, "Failed in MQFreeMemory, error = 0x%x\n",hr);
return -1;
}
The following example retrieves the PROPID_Q_PATHNAME property for a known queue and then prints the returned value to the screen. Note that the memory (used to store the label) allocated by MSMQ is freed using MQFreeMemory.
#include <windows.h>
#include <stdio.h>
#include <mq.h> // MSMQ header file
int main(int arg, char *argv[])
{
////////////////////////////
// Define structures.
////////////////////////////
// Define the number of properties
#define NumberOfProperties 1
// Define the property counter.
DWORD PropIdCount = 0;
//Define the MQQUEUPROPS structure.
MQQUEUEPROPS QueueProps;
PROPVARIANT aQueuePropVar[NumberOfProperties];
QUEUEPROPID aQueuePropId[NumberOfProperties];
HRESULT aQueueStatus[NumberOfProperties];
//Define results.
HRESULT hr;
// Define format name buffer.
DWORD dwFormatNameBufferLength = 256;
WCHAR szFormatNameBuffer[256];
///////////////////////////////////
// Specify PROPID_Q_PATHNAME. The type
// indicator must be set to VT_NULL.
///////////////////////////////////
aQueuePropId[PropIdCount] = PROPID_Q_PATHNAME; // Property ID
aQueuePropVar[PropIdCount].vt = VT_NULL; // Type indicator
PropIdCount++;
///////////////////////////////////////////////////////
// Add additional queue properties here. When adding
// properties, increment NumberOfProperties to
// reflect total number of properties.
///////////////////////////////////////////////////////
////////////////////////////////
// Set the MQQUEUEPROPS structure.
/////////////////////////////////
QueueProps.cProp = PropIdCount; // Number of properties
QueueProps.aPropID = aQueuePropId; // Ids of properties
QueueProps.aPropVar = aQueuePropVar; // Values of properties
QueueProps.aStatus = aQueueStatus; // Error reports
////////////////////////////
//Get format name of queue.
////////////////////////////
hr = MQPathNameToFormatName(L"machinename\\queuename",
szFormatNameBuffer,
&dwFormatNameBufferLength);
if (FAILED(hr))
{
fprintf(stderr, "Failed in MQPathNameToFormatName, error = 0x%x\n",hr);
return -1;
}
////////////////////////////
// Get queue property.
////////////////////////////
hr = MQGetQueueProperties(szFormatNameBuffer, &QueueProps);
if (FAILED(hr))
{
fprintf(stderr, "Failed in MQGetQueueProperties, error = 0x%x\n",hr);
return -1;
}
///////////////////////////////////////////////
// Review returned value. This example prints
// out the pathname of the queue.
//////////////////////////////////////////////
wprintf(L"The label of the queue is :");
wprintf(L" %s.\n", aQueuePropVar[0].pwszVal);
/////////////////////////////////
// Free memory allocated by MSMQ.
/////////////////////////////////
MQFreeMemory(aQueuePropVar[0].pwszVal);
if (FAILED(hr))
{
fprintf(stderr, "Failed in MQFreeMemory, error = 0x%x\n",hr);
return -1;
}
return 0;
}