HOWTO: Get Windows NT DOMAIN\UserName of Exchange Mailbox Using CDO/VB

ID: Q244670


The information in this article applies to:
  • Collaboration Data Objects (CDO), version 1.21
    on the following platforms: NT


SUMMARY

This article provides a CDO 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 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 Microsoft CDO 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
    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
      objSession.Logon
      
      '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!"
        objSession.Logoff
        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))
      Next
      
      'Allocate space for the strings so the API won't GPF
      strName = Space(64)
      strDomain = Space(64)
      
      'Get the NT Domain and UserName
      '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
      Else
        MsgBox "Error calling LookupAccountSID: " & ret
      End If
      
    Finish:
      objSession.Logoff
      Set objSession = Nothing
    End Sub 


Additional query words:

Keywords : kbCDO kbCDO121 kbMsg kbNTOS400 kbVBp kbGrpMsg kbDSupport
Version : WINDOWS:1.21
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.