Sending Messages that Request a Response

To request response messages, the sending application must supply a response queue for the returned messages. Response messages are application-defined, and generated by the application reading the message.

To send a message that returns a response message
  1. Call MQOpenQueue to open the queue with send access.
   //////////////////////////////
   // Open the destination queue
   // with send access.
   //////////////////////////////
   
   QUEUEHANDLE hQueue;
   hr = MQOpenQueue(szFormatName, MQ_SEND_ACCESS, 0, &hQueue);
   if (FAILED(hr))
   {
       fprintf(stderr, "Failed in MQOpenQueue, error = 0x%x\n", hr);
       return -1;
   }
    
  1. Set PROPID_M_RESP_QUEUE. This property specifies the response queue where the response messages will be sent.
    /////////////////////////////////////////
    // Set the PROPID_M_RESPONSE_QUEUE property.
    /////////////////////////////////////////
    aPropId[PropIdCount] = PROPID_M_RESPONSE_QUEUE;     //PropId
    aVariant[PropIdCount].vt = VT_LPWSTR;               //Type
    aVariant[PropIdCount].pwszVal = szwRespFormatName;  //An already obtained format name of the response queue.
     
    PropIdCount++;
     
  2. Set other message properties such as the message's body and its label.

  3. Set the MQMSGPROPS structure.
    ////////////////////////////////
    // Set the MQMSGPROPS structure.
    ////////////////////////////////
    MsgProps.cProp = PropIdCount;       //Number of properties.
    MsgProps.aPropID = aPropId;         //Ids of properties.
    MsgProps.aPropVar = aVariant;       //Values of properties.
    MsgProps.aStatus  = NULL;           //No Error report.
     
  4. Call MQSendMessage to send the message to the queue.
    /////////////////
    // Send message.
    /////////////////
    hr = MQSendMessage(
         hQueue,                  // handle to the Queue.
         &MsgProps,               // Message properties to be sent.
         MQ_NO_TRANSACTION        // No transaction
         );
     

Example

The following example sends a message requesting full receive acknowledgments.

MQMSGPROPS MsgProps;
PROPVARIANT aVariant[10];
MSGPROPID aPropId[10];
DWORD PropIdCount = 0;
 
HRESULT hr;
 
  //////////////////////////////
  // Open the destination queue
  // with send access.
  //////////////////////////////
  
  QUEUEHANDLE hQueue;
  hr = MQOpenQueue(szFormatName, MQ_SEND_ACCESS, 0, &hQueue);
  if (FAILED(hr))
  {
      fprintf(stderr, "Failed in MQOpenQueue, error = 0x%x\n", hr);
      return -1;
  }
    
QUEUEHANDLE hQueue;
 
/////////////////////////////////////////
// Set the PROPID_M_RESP_QUEUE property.
/////////////////////////////////////////
aPropId[PropIdCount] = PROPID_M_RESP_QUEUE;         //PropId
aVariant[PropIdCount].vt = VT_LPWSTR;                //Type
aVariant[PropIdCount].pwszVal = szwRespFormatName;  //An already obtained format name of the response queue.
 
PropIdCount++;
 
////////////////////////////////////////
// Set other message properties, such
// as PROPID_M_BODY and PROPID_M_LABEL.
///////////////////////////////////////
 
////////////////////////////////
// Set the MQMSGPROPS structure.
////////////////////////////////
MsgProps.cProp = PropIdCount;       //Number of properties.
MsgProps.aPropID = aPropId;         //Ids of properties.
MsgProps.aPropVar = aVariant;       //Values of properties.
MsgProps.aStatus  = NULL;           //No Error report.
 
/////////////////
// Send message.
/////////////////
hr = MQSendMessage(
     hQueue,                  // handle to the Queue.
     &MsgProps,               // Message properties to be sent.
     MQ_NO_TRANSACTION        // No transaction
     );
 
if (FAILED(hr))
   {
    //
    // Handle error condition
    //
    }
 

© 1997 by Microsoft Corporation. All rights reserved.