Partager via


C++ COM Code Example: Locating a Queue

 

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 searches for all public queues with the label "Test Queue." This example uses smart pointers to the following Message Queuing interfaces.

  • MSMQQuery: Its MSMQQuery.LookupQueue method returns a smart pointer to an MSMQQueueInfos interface to a query for locating a collection of public queues based on any of the following queue properties: queue identifier, service type, label, creation time, modification time, and multicast address. In this example, the query is based only on the label.

  • MSMQQueueInfos: Represents a query and exposes methods that query the directory service and return an MSMQueueInfo interface for each queue found in the query.

  • MSMQQueueInfo: Represents a public queue that matches the search criteria of the query.

To use smart pointers, your application must import Mqoa.dll. You can import this DLL using the #import directive and specify the MSMQ namespace.

#import "mqoa.dll"  
using namespace MSMQ;  

Before using any smart pointer, your application must call CoInitialize or CoInitializeEx to initialize the COM library. After the COM library is no longer needed, your application must call CoUnitialize. For more information, see Using Message Queuing COM Components in Visual C++ and C.

Note

When using COM components to perform a query, the query results contain all the properties of the queues found that are stored in an MSMQQueueInfo COM component.

To run a query based on the label of the queue

  1. Create a smart pointer to an IMSMQQuery interface and declare smart pointers to IMSMQQueueInfos and IMSMQQueueInfo interfaces.

  2. Specify the search criteria for the query. In this case vtLabel is set to "Test Queue."

  3. Call MSMQQuery.LookupQueue to define the query and obtain the smart pointer to the IMSMQQueueInfos interface.

  4. Call MSMQQueueInfos.Next to start the query and return the first queue in the collection of public queues satisfying the search criteria of the query.

  5. In a loop structure, display the path name of the queue returned in the previous call to Next and then call MSMQQueueInfos.Next again to continue returning the remaining queues.

Code Example

The following code example can be run on all versions of Message Queuing.

HRESULT LocateQueue()  
{  
  HRESULT hr = S_OK;  
  try  
  {  
    IMSMQQueryPtr pQuery("MSMQ.MSMQQuery");   
    IMSMQQueueInfosPtr pInfos = NULL;  
    IMSMQQueueInfoPtr pInfoCurrent = NULL;  
    _variant_t vtLabel("Test Queue");  
  
    // Specify the queue label as the search criterion.  
    pInfos = pQuery->LookupQueue(&vtMissing, &vtMissing, &vtLabel);  
    pInfoCurrent = pInfos->Next();  
    while (pInfoCurrent)  
    {  
        wprintf(L"Found queue name: %s\n",  
          (WCHAR *)pInfoCurrent->PathName);  
        pInfoCurrent = pInfos->Next();  
    }    
  }  
  catch (const _com_error& comerr)   
  {  
    hr = comerr.Error();  
  
    wprintf(L"Error Code = 0x%X\nError Description = %s\n",  
      hr, (WCHAR *)comerr.Description());  
  }  
  
  return hr;  
}