How to get Windows NT DOMAIN\UserName of Exchange mailbox using CDO

Exclusion de responsabilité du contenu obsolète de la base de connaissances

Cet article a été rédigé sur les produits pour lesquels Microsoft n’offre plus aucune prise en charge. Par conséquent, cet article est proposé « en l’état » et ne sera plus mis à jour.


This article provides CDO (1.2, 1.21) sample code written in Microsoft Visual Basic demonstrating how to obtain the Windows NT Account associated with an Exchange mailbox in the form DOMAIN\UserName.

More Information

The value returned by CDO (1.2, 1.21) for the PR_EMS_AB_ASSOC_NT_ACCOUNT property of an AddressEntry is a hexadecimal representation of the SID for the Windows NT account associated with the Exchange mailbox. Briefly, this sample obtains the PR_EMS_AB_ASSOC_NT_ACCOUNT property from the Fields collection of the AddressEntry object for the mailbox, converts the hexadecimal representation of the SID into a Byte array, then calls the LookupAccountSid API.

Please note that the LookupAccountSid API is only supported on the Windows NT platform.

Please see the following steps to run the sample code:
  1. In Microsoft Visual Basic program, start a new Standard EXE project, and add a command button to the default form.
  2. From your project, make a reference to the Microsoft CDO 1.2 or 1.21 Library.
  3. Paste the following code into the General Declarations section of the default form:
    Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias "LookupAccountSidA" ( _
    ByVal lpSystemName As String, _
    Sid As Any, _
    ByVal name As String, _
    cbName As Long, _
    ByVal ReferencedDomainName As String, _
    cbReferencedDomainName As Long, _
    peUse As Integer _
    ) As Long

    'This constant is not defined by CDO (1.2, 1.21)
    Const CdoPR_EMS_AB_ASSOC_NT_ACCOUNT = &H80270102

    Private Sub Command1_Click()
    Dim objSession As New MAPI.Session
    Dim objMessage As MAPI.Message
    Dim objRecip As MAPI.Recipient
    Dim bByte() As Byte
    Dim tmp As Integer
    Dim i As Integer
    Dim ret As Boolean
    Dim strSID As String
    Dim strName As String
    Dim strDomain As String

    'Logon to Session object

    'Get a recipient object
    Set objMessage = objSession.Outbox.Messages.Add
    Set objMessage.Recipients = objSession.AddressBook(OneAddress:=True)
    If objMessage.Recipients Is Nothing Then
    MsgBox "No recipient has been chosen!"
    Exit Sub
    End If
    Set objRecip = objMessage.Recipients(1)

    'Make sure it's a mailbox
    If Not objRecip.DisplayType = CdoUser Then
    MsgBox "Selection is not a mailbox owner"
    GoTo Finish
    End If

    'Get the PR_EMS_AB_ASSOC_NT_ACCOUNT (&H80270102) field
    strSID = objRecip.AddressEntry.Fields(CdoPR_EMS_AB_ASSOC_NT_ACCOUNT).Value

    'The SID is stored in a hexadecimal representation of the binary SID
    'so we convert it and store it in a byte array
    tmp = Len(strSID) / 2 - 1
    ReDim bByte(tmp) As Byte
    For i = 0 To tmp - 1
    bByte(i) = CInt("&h" & Mid(strSID, (i * 2) + 1, 2))

    'Allocate space for the strings so the API won't GPF
    strName = Space(64)
    strDomain = Space(64)

    'Get the NT Domain and UserName
    'Note: This can't be done from VBScript directly
    'because VBScript doesn't support making the
    'LookupAccountSid API call
    ret = LookupAccountSid(vbNullString, bByte(0), strName, Len(strName), strDomain, Len(strDomain), iType)

    If ret Then 'Strip the Null characters from the returned strings
    strDomain = Left(strDomain, InStr(strDomain, Chr(0)) - 1)
    strName = Left(strName, InStr(strName, Chr(0)) - 1)
    MsgBox "NT Account: " & strDomain & "\" & strName
    MsgBox "Error calling LookupAccountSID: " & ret
    End If

    Set objSession = Nothing
    End Sub

ID d'article : 244670 - Dernière mise à jour : 20 mai 2005 - Révision : 1