Visual Basic Code Example: Adding a Public Queue to a Distribution List


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 public queue with the path name supplied by the caller and adds it to the Active Directory Domain Services (AD DS) distribution list specified by the caller in the local domain.

For information on how distribution lists are used, see Distribution Lists.

This example uses the GetObject method to obtain an ADSI object 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, from which the distribution list group object is retrieved.

Public queues are added to the distribution list using their ADs paths. In this example, the ADsPath property of the MSMQQueueInfo object used to create the queue is used.


Distribution Lists are AD DS objects that can be created and managed 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 resource in your Visual Basic® project.

To add a public queue to a distribution list

  1. Declare the variables and objects needed.

  2. Declare an MSMQQueueInfo variable for the queue.

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

  4. Set the MSMQQueueInfo.PathName property to the UNC path name of the queue.

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

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

  7. Bind the domain name to an ADSI container object that will serve as the parent ADSI container for the distribution list.

  8. Call GetObject to retrieve the distribution list (DL) with the name supplied by the caller from the parent ADSI container.

  9. Add the public queue to the distribution list using its ADsPath property.

Code Example

The following code example requires MSMQ 3.0.

Private Sub AddNewPublicQueueDL(  
                                strPublicQueuePathName As String, _  
                                strDLName As String _  
  Dim qinfo As MSMQQueueInfo  
  Dim iadsRootDse As IADs  
  Dim strRootDomain As String  
  Dim iadscontDS As IADsContainer  
  Dim iadsgroupDist As IADsGroup  
  ' Create a public queue with the path name supplied by the caller  
  Set qinfo = New MSMQQueueInfo  
  qinfo.PathName = strPublicQueuePathName  
  qinfo.Label = "TestQueue"  
  On Error GoTo ErrorHandler  
  ' Get an ADSI object for rootDSE.  
  Set iadsRootDse = GetObject("LDAP://RootDSE")  
  ' Retrieve the domain name from the object representing rootDSE.  
  strRootDomain = iadsRootDse.Get("RootDomainNamingContext")  
  ' Bind the domain name to the parent ADSI container.  
  Set iadscontDS = GetObject("LDAP://" + strRootDomain)  
  ' Retrieve the distribution list group object from the parent   
ADSI container object.  
  Set iadsgroupDist = iadscontDS.GetObject("group", strDLName)  
' Add the public queue to the distribution list using its ADsPath property.  
iadsgroupDist.Add qinfo.ADsPath  
  Exit Sub  
  MsgBox "Error " + Hex(Err.Number) + " returned." _  
          + Chr(13) + Err.Description  
End Sub