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
Else
Exit For ' Short circuit when we run out of "DC=" parts
End If
Next
' 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.