共用方式為


C-C++ Code Example: Requesting Source Journaling

 

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 can request positive or negative source journaling based on the journaling level provided by the caller. This function sets the PROPID_M_JOURNAL property of the message using the provided journaling level and then sends the message to the destination queue provided by the caller. For positive source journaling, the value of the journaling level passed to the function must be MQMSG_JOURNAL, and for negative source journaling, the value must be MQMSG_DEADLETTER. Both positive and negative source journaling can be requested by passing MQMSG_JOURNAL | MQMSG_DEADLETTER.

For information on source journaling, see Source Journaling.

To request source journaling

  1. Define the message property structure.

  2. Specify the message properties. This example sets the following message 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 destination queue.

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

Code Example

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

HRESULT RequestJournaling(  
                          LPCWSTR wszQueueFormatName,  
                          unsigned char bJournalLevel  
                          )  
{  
  
  // Validate the input string.  
  if (wszQueueFormatName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define the maximum total number of message properties and a property counter.  
  const int NUMBEROFPROPERTIES = 5;  
  DWORD cPropId = 0;  
  
  // Define the message property structure.  
  MQMSGPROPS MsgProps;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];   
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  
  HRESULT hr = MQ_OK;  
  
  // Set the journal property PROPID_M_JOURNAL.  
  aMsgPropId[cPropId] = PROPID_M_JOURNAL;  
  aMsgPropVar[cPropId].vt = VT_UI1;  
  aMsgPropVar[cPropId].bVal = bJournalLevel;  
  cPropId++;  
  
  // Set additional message properties. This example also sets   
  // the message label property PROPID_M_LABEL.  
  aMsgPropId[cPropId] = PROPID_M_LABEL;  
  aMsgPropVar[cPropId].vt = VT_LPWSTR;  
  aMsgPropVar[cPropId].pwszVal = L"Test Message: Journaling";  
  cPropId++;  
  
  // Initialize the MQMSGPROPS structure.  
  MsgProps.cProp = cPropId;  
  MsgProps.aPropID = aMsgPropId;  
  MsgProps.aPropVar = aMsgPropVar;  
  MsgProps.aStatus = aMsgStatus;  
  
  HANDLE hQueue = NULL;  
  
  hr = MQOpenQueue(  
                   wszQueueFormatName,  
                   MQ_SEND_ACCESS,  
                   MQ_DENY_NONE,  
                   &hQueue  
                   );  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  
  // Send the message.  
  hr = MQSendMessage(  
                     hQueue,  
                     &MsgProps,  
                     MQ_NO_TRANSACTION  
                     );  
  if (FAILED(hr))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Close the queue.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}