C-C++ Code Example: Opening a Queue Using a Queue Identifier

 

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 opens a queue based on a given queue identifier, access mode, and share mode.

This function calls the MQInstanceToFormatName function to translate the queue identifier into a format name for opening the queue. This function can be called while you are operating offline. Message Queuing does not use information stored in the directory service to generate a format name from the queue identifier.

The following procedure shows how the function opens the queue based on the information provided by the caller.

To open a queue using a queue identifier

  1. Call MQInstanceToFormatName to convert the queue identifier (provided by the caller) to a format name required to open the queue.

  2. Call MQOpenQueue to open the queue.

  3. Return the handle to the queue specified in the phQueue parameter of the MQOpenQueue function. If MQOpenQueue fails, Message Queuing returns a NULL pointer.

Code Example

The following code example contains no version-specific Message Queuing calls.

HRESULT OpenMyQueue(  
                    DWORD dwAccess,  
                    DWORD dwShareMode,  
                    CLSID InstanceBuffer,  
                    QUEUEHANDLE *phQueue  
                    )  
{  
  HRESULT hr = MQ_OK;                     // Return code  
  
  // Validate the input parameter.  
  if (phQueue == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Call MQInstanceToFormatName to convert the queue identifier  
  //(GUID) to a format name.  
  DWORD dwFormatNameBufferLength = 256;   // Format Name buffer length  
  WCHAR wszFormatNameBuffer[256];         // Format name buffer  
  hr = MQInstanceToFormatName(  
                              &InstanceBuffer,  
                              wszFormatNameBuffer,   
                              &dwFormatNameBufferLength  
                              );  
  if (FAILED(hr))  
  {  
     return hr;  
  }  
  
  // Call MQOpenQueue to open the queue.  
  hr = MQOpenQueue(  
                   wszFormatNameBuffer,        // Format name of the queue  
                   dwAccess,                   // Access mode  
                   dwShareMode,                // Share mode  
                   phQueue                     // OUT: Queue handle  
                   );  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  puts("The queue is opened.");  
  return hr;  
}