C-C++ Code Example: Opening a Queue for Direct Messaging

 

Updated: July 19, 2016

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 direct format name, access mode, and share mode.

Direct format names can be used to open any private or public destination queue on the local computer or a remote computer. Direct format names use one of the following types of syntax:

DIRECT=AddressSpecification\QueueName           // For public queues  
DIRECT=AddressSpecification\PRIVATE$\QueueName  // For private queues  

The following procedure shows how the function opens the queue based on the information provided by its parameters and returns the queue handle to the caller.

To open a queue for direct messaging

  1. Create a buffer for the format name.
System_CAPS_ICON_note.jpg Note

wcslen properly handles only null-terminated strings. This code example does not verify that the string passed to it is null-terminated. It is the responsibility of the caller to ensure that the string passed is null-terminated.

  1. Create the direct format name of the queue from the path name provided by the caller.

  2. Call MQOpenQueue to open the queue with the access and share mode provided by the caller.

  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.

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

int OpenMyQueue(  
                LPWSTR wszPathName,  
                DWORD dwAccess,  
                DWORD dwShareMode,  
                QUEUEHANDLE *phQueue  
                )  
{  
  
  HRESULT hr = S_FALSE;   
  DWORD dwFormatNameLength = 0;  
  WCHAR *wszFormatName = NULL;  
  
  // Validate the input parameters.  
  if ((wszPathName == NULL) || (phQueue == NULL))  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Create a buffer for the direct format name.  
  dwFormatNameLength = wcslen(wszPathName) + 11;  
  wszFormatName = new WCHAR[dwFormatNameLength];  
  
  if (wszFormatName)  
  {  
  
    // Create the direct format name of the queue.  
    memset(wszFormatName, 0, dwFormatNameLength*sizeof(WCHAR));  
  // ************************************  
  // You must concatenate "DIRECT=OS:" and wszPathName into the   
  // wszFormatName buffer.  
  // wszFormatName = DIRECT=OS:" + wszPathName  
  // If the format name is too long for the buffer, return FALSE.  
  // ************************************  
  
    // Call MQOpenQueue to open the queue with the access and  
    // share mode provided by the caller.  
    hr = MQOpenQueue(  
                     wszFormatName,           // Direct format name of the queue  
                     dwAccess,                // Access mode  
                     dwShareMode,             // Share mode  
                     phQueue                  // OUT: Queue handle  
                     );  
    delete [] wszFormatName;  
    if (FAILED(hr))  
    {  
      fprintf(stderr, "An error occurred in MQOpenQueue (error: 0x%x.)\n",hr);  
      return hr;  
    }  
  }  
  return hr;  
}  

Community Additions

ADD
Show: