Compartir a través de


C-C++ Code Example: Requesting Response Messages

 

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 requests response messages based on a given response queue format name. This function sets the PROPID_M_RESP_FORMAT_NAME property of the message using the supplied response queue format name and then sends the message to the specified destination queue.

For information on responding to sent messages, see Response Messages.

This example uses the PROPID_M_RESP_FORMAT_NAME property (introduced in MSMQ 3.0) to specify the format name of the response queue. This property accepts the following format name types.

  • A single public, private, or direct format name. These format names are used to specify a single response queue.

    For information about these format names, see Public Format Names, Private Format Names, and Direct Format Names.

  • A multiple-element format name. This format name is used to specify multiple response queues.

    For information about multiple-element format names, see Multiple-Element Format Names.

  • A distribution list format name. This format name is used to specify multiple response queues.

    For information about distribution list format names, see Distribution List Format Names.

  • A multicast address format name. This format name is used to specify multiple response queues.

    For information about multicast address format names, see Multicast Address Format Names.

To request response messages

  1. Define needed constants, variables, and structures.

  2. Specify message properties. This example sets the following properties.

  3. Initialize the MQMSGPROPS structure.

  4. Call MQOpenQueue to open the destination queue with send access.

  5. Call MQSendMessage to send the message to the opened queue.

  6. Call MQCloseQueue to close the destination queue and free resources.

Code Example

The following code example requires MSMQ 3.0.

HRESULT RequestingResponse(  
                           LPCWSTR wszDestQueueFormatName,  
                           LPCWSTR wszResponseQueueFormatName  
                           )  
{  
  
  // Validate the input strings.  
  if (wszDestQueueFormatName == NULL || wszResponseQueueFormatName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define constants, variables, and a message property structure.  
  
  const int NUMBEROFPROPERTIES = 2;  
  DWORD cPropId = 0;  
  HRESULT hr = MQ_OK;  
  
  MQMSGPROPS MsgProps;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];   
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  
  // Set message properties.  
  aMsgPropId[cPropId] = PROPID_M_RESP_FORMAT_NAME;  
  aMsgPropVar[cPropId].vt = VT_LPWSTR;  
  aMsgPropVar[cPropId].pwszVal = const_cast<WCHAR*>(wszResponseQueueFormatName);  
  cPropId++;  
  
  aMsgPropId[cPropId] = PROPID_M_LABEL;  
  aMsgPropVar[cPropId].vt = VT_LPWSTR;  
  aMsgPropVar[cPropId].pwszVal = L"Test Message: Response";  
  cPropId++;  
  
  // Initialize the MQMSGPROPS structure.  
  MsgProps.cProp = cPropId;  
  MsgProps.aPropID = aMsgPropId;  
  MsgProps.aPropVar = aMsgPropVar;  
  MsgProps.aStatus = aMsgStatus;  
  
  // Call MQOpenQueue to open the destination queue.  
  HANDLE hQueue = NULL;  
  hr = MQOpenQueue(  
                   wszDestQueueFormatName,  
                   MQ_SEND_ACCESS,  
                   MQ_DENY_NONE,  
                   &hQueue  
                   );  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  
  // Call MQSendMessage to send the message.  
  hr = MQSendMessage(  
                     hQueue,  
                     &MsgProps,  
                     MQ_NO_TRANSACTION  
                     );  
  if (FAILED(hr))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Call MQCloseQueue to close the destination queue and free resources.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}