다음을 통해 공유


C-C++ Code Example: Enforcing Authentication

 

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 which creates a queue that enforces authentication. Authentication is enforced by setting the PROPID_Q_AUTHENTICATE property of the destination queue to MQ_AUTHENTICATE.

Note

The PROPID_Q_AUTHENTICATE property can be set when the queue is created or later by calling MQSetQueueProperties. If this property is set after the queue is created, the new setting only applies to those messages sent after the queue property is changed.

To enforce authentication

  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 EnforceAuthentication(  
                              LPCWSTR wszPathName  
                              )  
{  
  
  // Validate the input string.  
  if (wszPathName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define the maximum total number of message properties and a property counter.  
  const int NUMBEROFPROPERTIES = 5;              // Number of properties  
  DWORD cPropId = 0;                             // Define the 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;     // Property ID  
  aQueuePropVar[cPropId].vt = VT_LPWSTR;         // Type indicator  
  aQueuePropVar[cPropId].pwszVal = const_cast<WCHAR*>(wszPathName);  // Pathname of the queue  
  cPropId++;  
  
  aQueuePropId[cPropId] = PROPID_Q_AUTHENTICATE;      // Property ID  
  aQueuePropVar[cPropId].vt = VT_UI1;                 // Type indicator  
  aQueuePropVar[cPropId].bVal = MQ_AUTHENTICATE;      // Authentication level  
  cPropId++;  
  
  aQueuePropId[cPropId] = PROPID_Q_LABEL;             // Property ID  
  aQueuePropVar[cPropId].vt = VT_LPWSTR;              // Type indicator  
  aQueuePropVar[cPropId].pwszVal = L"TestQueue";      // Queue label  
  cPropId++;  
  
  // Initialize the MQQUEUEPROPS structure.  
  QueueProps.cProp = cPropId;                         // Number of properties  
  QueueProps.aPropID = aQueuePropId;                  // IDs of the properties  
  QueueProps.aPropVar = aQueuePropVar;                // Values of the properties  
  QueueProps.aStatus = aQueueStatus;                  // Error reports  
  
  // Call MQCreateQueue to create the queue.  
  DWORD dwFormatNameBufferLength = 256;               // Length of the format name buffer  
  WCHAR wszFormatNameBuffer[256];                     // Format name buffer  
  
  HRESULT hr = MQ_OK;  
  hr = MQCreateQueue(  
                     NULL,                            // Default security descriptor  
                     &QueueProps,                     // Queue properties  
                     wszFormatNameBuffer,             // [out] Format name of the queue  
                     &dwFormatNameBufferLength);      // [out] Format name length  
  return hr;  
}