다음을 통해 공유


C-C++ Code Example: Obtaining a List of Private Queues

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides an application-defined function that retrieves the list of path names of all the private queues residing on the computer whose name is passed to the function as a Unicode string.

In this example, the input string is not validated. If NULL is passed as the input string, a list of private queues on the local computer is obtained.

To retrieve a list of all the private queues residing on a specific computer

  1. Define the required constants and variables.

  2. Define an MQMGMTPROPS structure.

  3. Specify PROPID_MGMT_MSMQ_PRIVATEQ as the property to be retrieved.

  4. Initialize the MQMGMTPROPS structure.

  5. Call MQMgmtGetInfo with MO_MACHINE_TOKEN as the object name to retrieve the list of path names of all the private queues on the computer specified.

  6. Display the path names of the private queues found or process them as required.

  7. Call MQFreeMemory to free the memory allocated by Message Queuing for the array of strings to store the path names.

Code Example

The following code example requires MSMQ 3.0.

HRESULT GetPrivateQs(  
                     WCHAR * wszComputerName  
                     )  
{  
  
  // Define the required constants and variables.  
  const int NUMBEROFPROPERTIES = 1;                  // Number of properties  
  DWORD cPropId = 0;                                 // Property counter  
  DWORD cQ = 0;                                      // Queue counter  
  HRESULT hr = MQ_OK;                                // Return code  
  
  // Define an MQMGMTROPS structure.  
  MQMGMTPROPS mgmtprops;  
  MGMTPROPID aMgmtPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMgmtPropVar[NUMBEROFPROPERTIES];  
  
  // Specify PROPID_MGMT_MSMQ_PRIVATEQ as a property to be retrieved.  
  aMgmtPropId[cPropId] = PROPID_MGMT_MSMQ_PRIVATEQ;  // Property ID  
  aMgmtPropVar[cPropId].vt = VT_NULL;                // Type indicator  
  cPropId++;  
  
  // Initialize the MQMGMTPROPS structure.  
  mgmtprops.cProp = cPropId;                         // Number of management properties  
  mgmtprops.aPropID = aMgmtPropId;                   // IDs of the management properties  
  mgmtprops.aPropVar = aMgmtPropVar;                 // Values of management properties  
  mgmtprops.aStatus  = NULL;                         // No storage for error codes  
  
  // Call MQMgmtGetInfo to retrieve the information.  
  hr = MQMgmtGetInfo(wszComputerName,               // Name of the computer  
                     MO_MACHINE_TOKEN,              // Object name  
                     &mgmtprops);                   // Management properties structure  
  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  
  // Display the path names of the private queues found.  
  if (aMgmtPropVar[0].calpwstr.cElems > 0)  
  {  
    for (cQ = 0; cQ < aMgmtPropVar[0].calpwstr.cElems; cQ++)  
    {  
      wprintf(L"%d. %s\n", cQ + 1, aMgmtPropVar[0].calpwstr.pElems[cQ]);  
    }  
  }  
  
  // Free the memory allocated to store the path names.  
  for (cQ = 0; cQ < aMgmtPropVar[0].calpwstr.cElems; cQ++)  
  {  
    MQFreeMemory(aMgmtPropVar[0].calpwstr.pElems[cQ]);  
  }  
  MQFreeMemory(aMgmtPropVar[0].calpwstr.pElems);  
  
  return hr;  
}