RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB
Expertise: Intermediate
Dec 21, 2005

Retrieving a Domain Name

This VB function returns the DNS domain name (westcoast.mycorp.com) from the passed LDAP distinguishedName (CN=Administrator,DN=Users,DC=westcoast,DC=mycorp,DC=com).

Function DN2DomainName(ByVal strDN As String)
    On Error Resume Next

    Dim strDomainName As String
    Dim strDNParts() As String
    Dim intI As Integer
    strDomainName = ""
    strDNParts = Split(strDN, ",")
    ReDim Preserve strDNParts(UBound(strDNParts))
    ' Work backwords through DN to assemble domain name
    For intI = UBound(strDNParts) To 0 Step -1
        If (InStr(UCase(strDNParts(intI)), "DC=")) Then
            strDomainName = "." & Replace(strDNParts(intI), "DC=", "") & strDomainName
            Exit For ' Short circuit when we run out of "DC=" parts
        End If
    ' Chop off leading '.'
    strDomainName = Right$(strDomainName, Len(strDomainName) - 1)
    DN2DomainName = strDomainName

End Function
Editor's Note: This code does not check to make sure the strDomainName variable has a leading period before chopping it off, nor does it check whether the final result actually fits the form of a DNS domain name, but it will work if given proper data.

Ryan Amschel
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date