Partilhar via


C-C++ Code Example: Enforcing Encryption

 

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

The following example provides an application-defined function that creates a queue that accepts private encrypted messages only.

  1. Define the required constants and variables.

  2. Define the MQQUEUEPROPS structure.

  3. Specify the queue properties used to create the queue. This example sets the following properties.

  4. Initialize the MQQUEUEPROPS structure.

  5. Call MQCreateQueue to create the queue. The format name returned by this queue can be used later to open the queue.

Code Example

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

HRESULT EnforceEncryption(  
                          LPCWSTR wszPathName _  
                          )  
{  
  
  // Validate the input string.  
  if (wszPathName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define the maximum number of message properties and a property counter.  
  const int NUMBEROFPROPERTIES = 3                    // Maximum number of properties to specify  
  DWORD cPropId = 0;                                  // Property counter  
  
  //Define the MQQUEUPROPS structure.  
  MQQUEUEPROPS QueueProps;  
  MQPROPVARIANT aQueuePropVar[NUMBEROFPROPERTIES];  
  QUEUEPROPID aQueuePropId[NUMBEROFPROPERTIES];  
  HRESULT aQueueStatus[NUMBEROFPROPERTIES];  
  
  // Specify queue properties.  
  aQueuePropId[cPropId] = PROPID_Q_PATHNAME;  
  aQueuePropVar[cPropId].vt = VT_LPWSTR;  
  aQueuePropVar[cPropId].pwszVal = const_cast<WCHAR*>(wszPathName);  
  cPropId++;  
  
  aQueuePropId[cPropId] = PROPID_Q_PRIV_LEVEL;  
  aQueuePropVar[cPropId].vt = VT_UI4;  
  aQueuePropVar[cPropId].bVal = MQ_PRIV_LEVEL_BODY;  
  cPropId++;  
  
  aQueuePropId[cPropId] = PROPID_Q_LABEL;  
  aQueuePropVar[cPropId].vt = VT_LPWSTR;  
  aQueuePropVar[cPropId].pwszVal = L"TestQueue";  
  cPropId++;  
  
  // Initialize the MQQUEUEPROPS structure.  
  QueueProps.cProp = cPropId;                         // Number of properties  
  QueueProps.aPropID = aQueuePropId;                  // IDs of properties  
  QueueProps.aPropVar = aQueuePropVar;                // Values of properties  
  QueueProps.aStatus = aQueueStatus;                  // Error reports  
  
  // Call MQCreateQueue to create the queue.  
  HRESULT hr = MQ_OK;  
  DWORD dwFormatNameBufferLength = 256;  
  WCHAR wszFormatNameBuffer[256];  
  
  hr = MQCreateQueue(  
                     NULL,                            // Default security descriptor  
                     &QueueProps,                     // Queue properties  
                     wszFormatNameBuffer,             // OUT: Format name of queue  
                     &dwFormatNameBufferLength);      // OUT: Format name length  
  return hr;  
}