Partager via


Visual Basic Code Example: Creating a Queue Alias

 

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 Private Sub procedure that creates a queue alias in Active Directory Domain Services (AD DS) for a given private queue.

For information on how queue aliases are used, see Queue Alias.

This example uses the GetObject method to obtain an ADSI object that represents the root of the directory data tree on a directory server in the local domain (rootDSE). RootDSE is a unique entry that exists on every directory server. It enables you to obtain information about the server. In this example, it is used to obtain the distinguished name (DN) of the first domain (the root domain) in the forest that contains the domain to which the directory server belongs. This name is then bound to an ADSI container object, in which the queue alias is created.

Note

Queue aliases are AD DS objects that can be created only in the Computers and Domain Controllers containers of domain controllers. This example can be run only on a domain controller by a user having domain administrative permissions.

To run this procedure, you must include the Active DS Type Library as a reference in your Visual Basic® project.

To create a queue alias

  1. Declare the variables and objects needed.

  2. Call GetObject to obtain an ADSI object representing rootDSE.

  3. Call IADs.Get to retrieve the domain name from the ADSI object representing rootDSE.

  4. Bind the root domain name to the parent ADSI container.

  5. Initialize the MSMQQueueInfo object with the path name provided by the caller.

  6. Use the MSMQQueueInfo object to attempt to create the queue, ignoring any errors if the queue already exists.

  7. Call MSMQQueueInfo.Refresh to retrieve the queue properties registered on the local computer.

  8. Create the queue alias as an object in AD DS.

  9. Make the queue alias reference the private queue.

Code Example

The following code example requires MSMQ 3.0.

Private Sub CreateQueueAlias( _  
                             strPrivateQueuePathName As String, _  
                             strQueueAliasName As String _  
                             )  
  
  Dim strQueueAliasCNName As String  
  Dim strRootDomain As String  
  Dim iadsQueueAlias As IADs  
  Dim iadsRootDse As IADs  
  Dim iadscontDS As IADsContainer  
  Dim qinfo As New MSMQQueueInfo  
  
  ' Get the RootDSE object.  
  On Error GoTo ErrorHandler  
  Set iadsRootDse = GetObject("LDAP://RootDSE")  
  
  ' Retrieve the domain name from the RootDSE object.  
  strRootDomain = iadsRootDse.Get("RootDomainNamingContext")  
  
  ' Bind the root domain name to the parent ADSI container.  
  Set iadscontDS = GetObject("LDAP://" + strRootDomain)  
  
  '  Create the private queue if it does not exist.  
  qinfo.PathName = strPrivateQueuePathName  
  On Error Resume Next  
    qinfo.Create  
  
  ' Retrieve the registered property values.  
  On Error GoTo ErrorHandler  
  qinfo.Refresh  
  
  ' Create a queue alias (MSMQ-Custom-Recipient) object.  
  strQueueAliasCNName = "CN=" & strQueueAliasName  
  Set iadsQueueAlias = iadscontDS.Create("MSMQ-Custom-Recipient", strQueueAliasCNName)  
  
  ' Make the queue alias object reference the private queue:  
  iadsQueueAlias.Put "msMQ-Recipient-FormatName", qinfo.FormatName  
  iadsQueueAlias.SetInfo  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " returned." _  
          + Chr(13) + Err.Description  
  
End Sub