Visual Basic 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

This example provides a Sub procedure that creates a queue that enforces encryption. Encryption is enforced by setting the MSMQQueueInfo.PrivLevel property of the destination queue to MQ_PRIV_LEVEL_BODY.

Note

The MSMQQueueInfo.PrivLevel property can be set when the queue is created or later by calling MSMQQueueInfo.Update. 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 encryption when creating a queue

  1. Declare the MSMQQueueInfo variable for the queue.

  2. Create a new MSMQQueueInfo object and assign it to the variable.

  3. Set the MSMQQueueInfo.PathName property to the UNC or DNS path name of the queue.

  4. Set optional queue properties. This example sets the following optional properties.

    MSMQQueueInfo.PrivLevel: Enforces encryption.

    MSMQQueueInfo.Label: Specifies label for queue.

  5. Call MSMQQueueInfo.Create to create the queue.

Code Example

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

Sub EnforceEncryption( _  
                      strPathName As String, _  
                      lPrivLevel As Long _  
                      )  
  Dim qinfo As MSMQQueueInfo  
  
  ' Create a new MSMQQueueInfo object.  
  Set qinfo = New MSMQQueueInfo  
  
  ' Specify the path name of the queue.  
  qinfo.PathName = strPathName  
  
  ' Set optional queue properties.  
  qinfo.Label = "TestQueue"  
  qinfo.PrivLevel = lPrivLevel  
  
  ' Create a queue.  
  On Error GoTo ErrorHandler  
  qinfo.Create  
  
  MsgBox "Created queue that accepts private messages only."  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub