Partilhar via


Visual Basic Code Example: Requesting 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 requests encryption based on a given privacy level value. This function sets the MSMQMessage.PrivLevel property of the message using the privacy level supplied by the caller and then sends the message to a known destination queue.

Note

Direct format names cannot be used to request encryption because design direct does not access the directory service. Message Queuing requires access to the directory service to encrypt the message.

For information on how Message Queuing encrypts messages, see Message Encryption.

To request encryption

  1. Declare the objects needed to send the message that requests tracing. This procedure declares the following objects:

  2. Generate a path name using the computer name and queue name provided by the caller.

  3. Set the MSMQDestination.PathName property.

  4. Set the MSMQMessage.PrivLevel property using the privacy level provided but the caller.

  5. Optional. Set additional message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message: Encryption".

  6. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name.

Note

When using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access and then closes the destination queue when the object is released.

Code Example

The following code example requires MSMQ 3.0.

Sub RequestEncryption( _  
                      strComputerName As String, _  
                      strQueueName As String, _  
                      lPrivacyLevel As Long _  
                      )  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create the path name of the queue.  
  strPathName = strComputerName & "\" & strQueueName  
  
  ' Set the path name of the MSMQDestination object.  
  On Error GoTo ErrorHandler  
  dest.PathName = strPathName  
  
  ' Set MSMQMessage.PrivLevel to request encryption.  
  msg.PrivLevel = lPrivacyLevel  
  
  ' Set other message properties.  
  msg.Label = "Test Message: Encryption"  
  
  ' Send the message  
  msg.Send DestinationQueue:=dest  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub