Compartir a través de


C-C++ Code Example: Enforcing Target 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 creates a queue which enforces target journaling. Target journaling is enforced by setting the PROPID_Q_JOURNAL property of the destination queue to MQ_JOURNAL.

Note

The PROPID_Q_JOURNAL property can be set when the queue is created or later. For an example of setting the journaling level of an existing destination queue, see C/C++ Code Example: Setting PROPID_Q_JOURNAL.

In addition to setting the journaling level of the destination queue, this function also sets the PROPID_Q_JOURNAL_QUOTA property to specify the maximum size of the journal queue.

To enforce target journaling

  1. Define an MQQUEUEPROPS structure.

  2. Specify the queue properties. This example specifies PROPID_Q_PATHNAME, which is required to create the queue, PROPID_Q_LABEL, PROPID_Q_JOURNAL, which is required to set the journaling level, and PROPID_Q_JOURNAL_QUOTA, which sets the maximum size of the queue journal.

  3. Initialize the MQQUEUEPROPS structure.

  4. Call MQCreateQueue to create the queue.

Code Example

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

HRESULT EnforceTargetJournaling(  
                                LPWSTR wszPathName,   
                                ULONG ulJournalQuota  
                                )  
{  
  
  // Define the maximum number of queue properties.  
  const int NUMBEROFPROPERTIES = 4;  
  
  // Define a queue property structure and the structures needed to initialize it.  
  MQQUEUEPROPS   QueueProps;  
  MQPROPVARIANT  aQueuePropVar[NUMBEROFPROPERTIES];  
  QUEUEPROPID    aQueuePropId[NUMBEROFPROPERTIES];  
  HRESULT        aQueueStatus[NUMBEROFPROPERTIES];  
  
  HRESULT        hr = MQ_OK;  
  
  // Validate the input string.  
  if (wszPathName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Set queue properties.  
  DWORD cPropId = 0;  
  aQueuePropId[cPropId] = PROPID_Q_PATHNAME;  
  aQueuePropVar[cPropId].vt = VT_LPWSTR;  
  aQueuePropVar[cPropId].pwszVal = wszPathName;  
  cPropId++;  
  
  WCHAR wszLabel[MQ_MAX_Q_LABEL_LEN] = L"Journaling enforced";  
  aQueuePropId[cPropId] = PROPID_Q_LABEL;  
  aQueuePropVar[cPropId].vt = VT_LPWSTR;  
  aQueuePropVar[cPropId].pwszVal = wszLabel;  
  cPropId++;  
  
  aQueuePropId[cPropId] = PROPID_Q_JOURNAL;  
  aQueuePropVar[cPropId].vt = VT_UI1;  
  aQueuePropVar[cPropId].bVal = MQ_JOURNAL;  
  cPropId++;  
  
  aQueuePropId[cPropId] = PROPID_Q_JOURNAL_QUOTA;  
  aQueuePropVar[cPropId].vt = VT_UI4;  
  aQueuePropVar[cPropId].ulVal = ulJournalQuota;  
  cPropId++;  
  
  // Initialize the MQQUEUEPROPS structure.  
  QueueProps.cProp = cPropId;                     //Number of properties  
  QueueProps.aPropID = aQueuePropId;              //IDs of the queue properties  
  QueueProps.aPropVar = aQueuePropVar;            //Values of the queue properties  
  QueueProps.aStatus = aQueueStatus;              //Pointer to the return status  
  
  // Call MQCreateQueue to create the queue.  
  WCHAR wszFormatNameBuffer[256];  
  DWORD dwFormatNameBufferLength = sizeof(wszFormatNameBuffer)/sizeof(wszFormatNameBuffer[0]);  
  hr = MQCreateQueue(NULL,                        // Default security descriptor  
                     &QueueProps,                 // Address of queue property structure  
                     wszFormatNameBuffer,         // Pointer to format name buffer  
                     &dwFormatNameBufferLength);  // Pointer to receive the queue's format name length  
  
  if (FAILED (hr))  
  {  
    return hr;  
  }  
  
  wprintf(L"The queue %s, which saves a copy of all messages removed from it, was created.\n", wszPathName);  
  
  return hr;  
}